# Setup Windows Slave node

## Use case
- Windows Application build pipeline (Visual Studio C++ / C#)

## Tools
- for installing jenkins agent as a windows service
  - [Windows Service Wrapper](https://github.com/winsw/winsw)
  ```sh (admin)
  C:/jenkins/> ren winsw.exe jenkins_agent.exe
  # Create jenkins_agent.xml for service configuration setup
  C:/jenkins/> jenkins_agent install  # to install the service
  C:/jenkins/> jenkins_agent start # to start the service
  C:/jenkins/> jenkins_agent stop  # to stop the serice
  ```
  - Alternative tool
  - [nssm - Non-Sucking Service Manager](https://nssm.cc/)  
  - [Package Manager - Chocolatey](https://chocolatey.org/)
  ```sh (admin)
  > choco install nssm
  > nssm install <service-name>
  > nssm remove <service-name>
  ```

## Windows
- Create a user 'jenkins'

## Steps
- Create a jenkins slave node setup on jenkins server
### Slave Machine
- Install [Git](https://git-scm.com)
- Install [AdoptOpenJDK](https://adoptopenjdk.net) 11 (Preferable the same version as Jenkins Master Version)
- Create a folder `C:\jenkins`
- Download `agent.jar` from jenkins server
- Run as Administrator a command prompt
- Install the agent service using the `Windows Service Wrapper` as a Windows Service and log on as `jenkins` user profile
```sh
$ jenkins-agent.exe install
$ jenkins-agent.exe start
$ jenkins-agent.exe stop
```
- Create a ssh key `ssh-keygen` and update `rsa_id.pub` to git server (eg: under jenkins user) to allow agent to clone from git repo

```xml
<service>
  <id>jenkins8080agent</id>
  <name>Jenkins Agent for jenkins:8080</name>
  <description>This service runs agent process connected to jenkins:8080</description>
  <env name="JENKINS_HOME" value="%BASE%" />
  <executable>C:\tools\jdk-11\bin\java.exe</executable>
  <arguments>-jar c:\jenkins\agent.jar -jnlpUrl http://jenkins:8080/jenkins-agent.jnlp -secret abcd -wordDir "c:\jenkins"</arguments>
  <log mode="roll" />
  <onfailure action="restart" />
  <serviceaccount>
    <domain>windows-pc</domain>
    <user>jenkins</user>
    <allowservicelogon>true</allowservicelogon>
  </serviceaccount>
</service>
```

# Setup Jenkins build to target windows slave node for build
- Create a build
- Set label to target the slave node
- Git clone to the node
- Run the windows build script on the node