Setting up a Windows/VS Jenkins VM Agent


Note – I am newbie to Jenkins. The steps below might not be best practice, but hopefully useful for someone looking to setup Jenkins Windows agent. It took me some time to figure this out so thought it might be useful to share.

I was following the really good lab at https://almvm.azurewebsites.net/labs/vstsextend/jenkins/ on how to use Jenkins. That scenario sets up a Jenkins infrastructure on Azure via https://portal.azure.com/#create/azure-oss.jenkinsjenkins. The lab then proceeds to build a maven based app on a Linux box.

As a prerequisite to below, you should probably follow the lab above first, especially the provisioning of Jenkins using the solution template.

The obvious follow-up for me was how to build a Windows based app (in this case an ASP.NET application using the full .NET Framework) in the same environment. I am a newbie to Jenkins but picked up the following:

1) Jenkins has a concept of master and slaves. You could look to set a Windows based slave manually

2) The Jenkins infrastructure that is provisioned automatically installs some plugins that allows you to scale or create new environments using Azure VM Agents plugin. More information ia available at https://docs.microsoft.com/en-us/azure/jenkins/jenkins-azure-vm-agents and https://plugins.jenkins.io/azure-vm-agents

I decided to go with the second option. As I was looking to build a .NET Framework ASP.NET app, I felt the easiest way to get all the dependencies on the machine was to have a VM that had a Visual Studio configured.

In summary : I wanted to setup a Jenkins agent running Windows and Visual Studio leveraging the Azure VM Agent task.

The intention is not to provide a step by step walkthrough. The general details of using the Azure VM agent plugins are provided in the URLs listed above.

Steps

1) I set the label to something that my build could use to select this agent – in the screenshot below it is “vs2”

clip_image002

2) You can specify the other usual Azure settings in terms of machine size, region etc as shown above

3) The important bit is the image reference where I specified

a. Use Advanced Image Configuration

b. I then specified the various details on the image. In this case

  • ImagePublisher=MicrosoftVisualStudio
  • ImageOffer=VisualStudio
  • ImageSKU=VS-2017-Ent-WS2016
  • ImageVersion=latest

c. I set the image type to Windows, Launch method SSH and ticked the box to pre-install SSH

clip_image004

Where did I get these settings from? - I went into the Azure portal and went through the process of creating a Visual Studio VM. Before you create a VM, you have the option to download the template. Open this template and look for the section around on StorageProfile. This is where you can get the values from (the values in the screenshot differ from above). Note – there is no need to create the VM from the portal. You can cancel it. The point is get the template so we can get easily derive the values. If you want a different VM then use the appropriate values. There might be an easier way to do this

clip_image006

clip_image008

4) IMPORTANT – I had to set up the initialization script under the section . If you hover over the information icon, it gives you the clue and advises you to use the script at https://raw.githubusercontent.com/Azure/jenkins/master/agents_scripts/Jenkins-Windows-Init-Script-Jnlp.ps1 for a Windows machine. This is what I used. The script is listed below as well (just in case!)

5) The Jenkins that was deployed sets up set of credentials, but I had no idea what the password was. So I setup a new set of credentials

clip_image010

6) When I setup a new build in Jenkins I set restrict where this project can be run – and set it to same label that was set in Step (1)

clip_image012




Script (should be same as https://raw.githubusercontent.com/Azure/jenkins/master/agents_scripts/Jenkins-Windows-Init-Script-Jnlp.ps1 and better to directly use that. Included below just in case the script is moved)

# Download and Install Java

Set-ExecutionPolicy Unrestricted

#Default workspace location

Set-Location C:\

$source = "http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-windows-x64.exe"

$destination = "C:\jdk-8u131-windows-x64.exe"

$client = new-object System.Net.WebClient

$cookie = "oraclelicense=accept-securebackup-cookie"

$client.Headers.Add([System.Net.HttpRequestHeader]::Cookie, $cookie)

$client.downloadFile($source, $destination)

$proc = Start-Process -FilePath $destination -ArgumentList "/s" -Wait -PassThru

$proc.WaitForExit()

[System.Environment]::SetEnvironmentVariable("JAVA_HOME", "c:\Program Files\Java\jdk1.8.0_131", "Machine")

[System.Environment]::SetEnvironmentVariable("PATH", $Env:Path + ";c:\Program Files\Java\jdk1.8.0_131\bin", "Machine")

$Env:Path += ";c:\Program Files\Java\jdk1.8.0_131\bin"

#Disable git credential manager, get more details in https://support.cloudbees.com/hc/en-us/articles/221046888-Build-Hang-or-Fail-with-Git-for-Windows

git config --system --unset credential.helper

# Install Maven

$source = "https://archive.apache.org/dist/maven/maven-3/3.5.2/binaries/apache-maven-3.5.2-bin.zip"

$destination = "C:\maven.zip"

$webClient = New-Object System.Net.WebClient

$webClient.DownloadFile($source, $destination)

$shell_app=new-object -com shell.application

$zip_file = $shell_app.namespace($destination)

mkdir 'C:\Program Files\apache-maven-3.5.2'

$destination = $shell_app.namespace('C:\Program Files')

$destination.Copyhere($zip_file.items(), 0x14)

[System.Environment]::SetEnvironmentVariable("PATH", $Env:Path + ";C:\Program Files\apache-maven-3.5.2\bin", "Machine")

$Env:Path += ";C:\Program Files\apache-maven-3.5.2\bin"

# Install Git

$source = "https://github.com/git-for-windows/git/releases/latest"

$latestRelease = Invoke-WebRequest -UseBasicParsing $source -Headers @{"Accept"="application/json"}

$json = $latestRelease.Content | ConvertFrom-Json

$latestVersion = $json.tag_name

$versionHead = $latestVersion.Substring(1, $latestVersion.IndexOf("windows")-2)

$source = "https://github.com/git-for-windows/git/releases/download/v${versionHead}.windows.1/Git-${versionHead}-64-bit.exe"

$destination = "C:\Git-${versionHead}-64-bit.exe"

$webClient = New-Object System.Net.WebClient

$webClient.DownloadFile($source, $destination)

$proc = Start-Process -FilePath $destination -ArgumentList "/VERYSILENT" -Wait -PassThru

$proc.WaitForExit()

$Env:Path += ";C:\Program Files\Git\cmd"


Comments (0)

Skip to main content