Skip to content

Instantly share code, notes, and snippets.

@lox
Last active February 23, 2018 09:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lox/e627d6749c8f212a5cf90158f0e8f9e3 to your computer and use it in GitHub Desktop.
Save lox/e627d6749c8f212a5cf90158f0e8f9e3 to your computer and use it in GitHub Desktop.
Install buildkite-agent in Windows as a Service
Param(
$agentVersion = "3.0-beta.39",
$agentToken = "xxx",
$agentTags = "windows",
$installDir = "C:\buildkite"
)
## Verify we are elevated
## https://superuser.com/questions/749243/detect-if-powershell-is-running-as-administrator
$elevated = [bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-32-544")
if($elevated -eq $false)
{
throw "In order to install services, please run this script elevated."
}
## Install choco and buildkite-agent dependencies
iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
choco install openssh -params '"/SSHAgentFeature" "/NoAutoCrlf"' -y
choco install git -params '"/GitAndUnixToolsOnPath"' -y
choco install docker docker-compose -y
choco install nssm -y
## Install buildkite-agent into $installDir\buildkite-agent.exe
$agentTag = "v${agentVersion}"
$agentDownloadUrl = "https://github.com/buildkite/agent/releases/download/${agentTag}/buildkite-agent-windows-amd64-${agentVersion}.zip"
Write-Host "Downloading ${agentDownloadUrl}"
Invoke-WebRequest -Uri $agentDownloadUrl -OutFile 'buildkite-agent.zip'
Write-Host 'Expanding ...'
Expand-Archive -Force -Path buildkite-agent.zip -DestinationPath $installDir
Write-Host 'Removing ...'
Remove-Item buildkite-agent.zip -Force
Write-Host 'Updating PATH ...'
$env:PATH = "${installDir};" + $env:PATH
[Environment]::SetEnvironmentVariable('PATH', $env:PATH, [EnvironmentVariableTarget]::Machine)
Write-Host 'Verifying install ...'
Write-Host 'buildkite-agent version'; buildkite-agent --version
Write-Host 'Complete.'
## Configure buildkite-agent
$buildkiteAgentCfgTemplate = type "${installDir}\buildkite-agent.cfg"
$buildkiteAgentCfgTemplate = $buildkiteAgentCfgTemplate -replace 'token="xxx"', ('token="{0}"' -f $agentToken.Trim())
$buildkiteAgentCfgTemplate = $buildkiteAgentCfgTemplate -replace '# tags="key1=val2,key2=val2"', ('tags="{0}"' -f $agentTags)
[System.IO.File]::WriteAllLines("${installDir}\buildkite-agent.cfg", $buildkiteAgentCfgTemplate);
## Install buildkite-agent as a windows service with nssm
$serviceName = "buildkite-agent"
$nssm = 'C:\ProgramData\chocolatey\bin\nssm.exe'
& $nssm install $serviceName "${installDir}\buildkite-agent.exe" "start"
& $nssm set $serviceName AppStdout "${installDir}\buildkite-agent.log"
& $nssm set $serviceName AppStderr "${installDir}\buildkite-agent.log"
Start-Sleep -Seconds .5
& $nssm status $serviceName
& $nssm start $serviceName
& $nssm status $serviceName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment