Created
October 6, 2016 23:50
-
-
Save rtyler/ad1e3331900dd1da4421dfd3f6f5c85b to your computer and use it in GitHub Desktop.
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
Set-ExecutionPolicy Unrestricted | |
$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 = 'http://cdn.azul.com/zulu/bin/zulu8.17.0.3-jdk8.0.102-win_x64.zip?jenkins' | |
$destinationJDKZipPath = $baseDir + '\zuluJDK.zip' | |
$destinationSlaveJarPath = $baseDir + '\slave.jar' | |
$javaExe = $baseDir + '\zulu8.17.0.3-jdk8.0.102-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