Skip to content

Instantly share code, notes, and snippets.

@fdstevex
Created May 31, 2016 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fdstevex/52da0d7e5892fe2965eae105b8cf3883 to your computer and use it in GitHub Desktop.
Save fdstevex/52da0d7e5892fe2965eae105b8cf3883 to your computer and use it in GitHub Desktop.
Running buildkite-agent as a Windows service
1. Download the Windows Service Wrapper binary from this project: https://github.com/kohsuke/winsw
2. Copy to your Buildkite directory and rename to "buildkite-service.exe"
3. Create buildkite-service.xml:
<service>
<id>buildkite-service</id>
<name>buildkite-service</name>
<description>The BuildKite build agent.</description>
<executable>buildkite-agent</executable>
<arguments>start</arguments>
<logmode>rotate</logmode>
</service>
4. Invoke "buildkite-service install" to register the service.
(This may show a dialog indicating that you need to install the .NET Framework 3.5 - you can do this in the Control Panel, search for Turn Windows Features On and Off and install it through the wizard).
5. Use the Services control panel to change the user that buildkite-service runs as, to an appropriate user.
6. "net start buildkite-agent"
Copy link

ghost commented May 11, 2017

Thanks for writing this up. I did some work automating this process, so I thought I'd just leave it here in case it's useful to anyone else. Something like this works for me:

Given there is the file C:\foo\templates\buildkite-agent.cfg:

token="BUILDKITE_AGENT_TOKEN"
name="%hostname-%n"
meta-data="queue=bar"
bootstrap-script="C:\ProgramData\chocolatey\lib\buildkite\tools\bootstrap.bat"
build-path="C:\Users\user\builds"
hooks-path="C:\ProgramData\chocolatey\lib\buildkite\tools\hooks"

And there is the file C:\foo\templates\buildkite-service.xml:

<service>
  <id>buildkite-service</id>
  <name>buildkite-service</name>
  <description>The BuildKite build agent.</description>
  <executable>buildkite-agent</executable>
  <arguments>start</arguments>
  <logmode>rotate</logmode>
    <serviceaccount>
       <domain>domain</domain>
       <user>user</user>
       <password>PASSWORD</password>
       <allowservicelogon>true</allowservicelogon>
    </serviceaccount>
</service>

And an encrypted file C:\foo\secrets\buildkite_agent_token:

YnVpbGRraXRlIGFnZW50IHRva2VuCg== (not a real token!)

And an encrypted file C:\foo\secrets\buildkite_agent_user_password:

YnVpbGRraXRlIGFnZW50IHVzZXIgcGFzc3dvcmQK (not a real password!)

Then I can install the buildkite agent by running this script as Administrator:

choco install -y buildkite nuget.commandline
nuget install WinSW -OutputDirectory C:\Temp
copy C:\Temp\WinSW.2.1.0\lib\net40-full\WinSW.NET4.exe C:\ProgramData\chocolatey\lib\buildkite\tools\buildkite-service.exe

# generate buildkite-agent.cfg
$buildkite_agent_token = type C:\foo\secrets\buildkite_agent_token | shush.exe decrypt | Out-String
$buildkite_agent_cfg_template = type C:\foo\templates\buildkite-agent.cfg
$buildkite_agent_cfg = $buildkite_agent_cfg_template -replace "BUILDKITE_AGENT_TOKEN", $buildkite_agent_token.Trim()
[System.IO.File]::WriteAllLines("C:\ProgramData\chocolatey\lib\buildkite\tools\buildkite-agent.cfg", $buildkite_agent_cfg);

# generate buildkite-service.xml
$buildkite_agent_user_password = type C:\foo\secrets\buildkite_agent_user_password | shush.exe decrypt | Out-String
$buildkite_service_template = type C:\foo\templates\buildkite-service.xml
$buildkite_service = $buildkite_service_template -replace "PASSWORD", $buildkite_agent_user_password.Trim()
[System.IO.File]::WriteAllLines("C:\ProgramData\chocolatey\lib\buildkite\tools\buildkite-service.xml", $buildkite_service);

# install the buildkite-service
C:\ProgramData\chocolatey\lib\buildkite\tools\buildkite-service.exe install

# stop and start to provision a user profile. (there must be a better way)
net start buildkite-service
net stop buildkite-service

# configure ssh
# put something in C:\Users\user\.ssh

net start buildkite-service

@lankaapura
Copy link

Awesome thanks both of you!! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment