Last active
June 6, 2019 07:41
-
-
Save olblak/76d87072f01a3b362f90b3dba135b765 to your computer and use it in GitHub Desktop.
windows jenkins agent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Image publish: MicrosoftWindowsServer | |
Image offer: WindowsServer | |
Image Sku: 2012-R2-Datacenter | |
Init Script: | |
Set-ExecutionPolicy Unrestricted | |
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 | |
$jenkinsServerUrl = $args[0] | |
$vmName = $args[1] | |
$secret = $args[2] | |
$jenkinsSlaveJarUrl = $jenkinsServerUrl + "jnlpJars/slave.jar" | |
$jnlpUrl=$jenkinsServerUrl + 'computer/' + $vmName + '/slave-agent.jnlp' | |
$baseDir = 'c:\azurecsdir' | |
$JDKUrl = 'https://cdn.azul.com/zulu/bin/zulu11.31.11-ca-jre11.0.3-win_x64.zip?jenkins' | |
$destinationJDKZipPath = $baseDir + '\zuluJDK.zip' | |
$destinationSlaveJarPath = $baseDir + '\slave.jar' | |
$javaExe = $baseDir + '\zulu11.31.11-ca-jre11.0.3-win_x64\bin\java.exe' | |
$GITUrl = 'https://github.com/git-for-windows/git/releases/download/v2.10.0.windows.1/MinGit-2.10.0-64-bit.zip' | |
$destinationGITZipPath = $baseDir + '\MinGit.zip' | |
$GITPath = $baseDir + '\git\cmd\' | |
# Function to get path of script file | |
function Get-ScriptPath | |
{ | |
return $MyInvocation.ScriptName; | |
} | |
# Checking if this is first time script is getting executed, if yes then downloading JDK and Git | |
If(-not((Test-Path $destinationJDKZipPath))) | |
{ | |
md -Path $baseDir -Force | |
$wc = New-Object System.Net.WebClient | |
$wc.DownloadFile($JDKUrl, $destinationJDKZipPath) | |
$shell_app = new-object -com shell.application | |
$zip_file = $shell_app.namespace($destinationJDKZipPath) | |
$javaInstallDir = $shell_app.namespace($baseDir) | |
$javaInstallDir.Copyhere($zip_file.items()) | |
$wc = New-Object System.Net.WebClient | |
$wc.DownloadFile($GITUrl, $destinationGITZipPath) | |
md -Path "$baseDir\git" -Force | |
$zip_file = $shell_app.namespace($destinationGITZipPath) | |
$gitInstallDir = $shell_app.namespace($baseDir + '\git') | |
$gitInstallDir.Copyhere($zip_file.items()) | |
"& $GITPath\git config --system core.autocrlf false" | |
"& $GITPath\git config --system core.longpaths true" | |
$wc = New-Object System.Net.WebClient | |
$wc.DownloadFile($jenkinsSlaveJarUrl, $destinationSlaveJarPath) | |
$scriptPath = Get-ScriptPath | |
$content = 'powershell.exe -ExecutionPolicy Unrestricted -file' + ' '+ $scriptPath + ' '+ $jenkinsServerUrl + ' ' + $vmName + ' ' + $secret | |
$commandFile = $baseDir + '\slaveagenttask.cmd' | |
$content | Out-File $commandFile -Encoding ASCII -Append | |
schtasks /create /tn "Jenkins slave agent" /ru "SYSTEM" /sc onstart /rl HIGHEST /delay 0000:30 /tr $commandFile /f | |
} | |
# Launching jenkins slave agent | |
$process = New-Object System.Diagnostics.Process; | |
$process.StartInfo.FileName = $javaExe; | |
If($secret) | |
{ | |
$process.StartInfo.Arguments = "-jar $destinationSlaveJarPath -secret $secret -jnlpUrl $jnlpUrl" | |
} | |
else | |
{ | |
$process.StartInfo.Arguments = "-jar $destinationSlaveJarPath -jnlpUrl $jnlpUrl" | |
} | |
$process.StartInfo.RedirectStandardError = $true; | |
$process.StartInfo.RedirectStandardOutput = $true; | |
$process.StartInfo.UseShellExecute = $false; | |
$process.StartInfo.CreateNoWindow = $true; | |
$newPath = $env:Path + ';' + $GITPath | |
$process.StartInfo.EnvironmentVariables.Item('Path') = $newPath | |
$process.StartInfo; | |
$process.Start(); | |
Write-Host 'Done Init Script.'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment