Skip to content

Commit f96fad6

Browse files
committed
Add ability to specify arbitrary agent env through the cfn stack
1 parent 0c94b92 commit f96fad6

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

packer/linux/conf/bin/bk-install-elastic-stack.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ disconnect-after-job=${BUILDKITE_TERMINATE_INSTANCE_AFTER_JOB}
185185
tracing-backend=${BUILDKITE_AGENT_TRACING_BACKEND}
186186
EOF
187187

188+
if [[ "${BUILDKITE_ENV_FILE_URL}" != "" ]]; then
189+
/usr/local/bin/bk-fetch.sh "${BUILDKITE_ENV_FILE_URL}" /var/lib/buildkite-agent/env
190+
fi
191+
188192
chown buildkite-agent: /etc/buildkite-agent/buildkite-agent.cfg
189193

190194
if [[ -n "${BUILDKITE_AUTHORIZED_USERS_URL}" ]] ; then

packer/linux/conf/buildkite-agent/systemd/buildkite-agent.service

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ User=buildkite-agent
1111
Environment="HOME=/var/lib/buildkite-agent"
1212
Environment="PATH=/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
1313
Environment="USER=buildkite-agent"
14+
# The =- (rather than just = ) in the line below means that systemd won't complain if it can't find the file
15+
EnvironmentFile=-/var/lib/buildkite-agent/env
1416
ExecStart=/usr/bin/buildkite-agent start
1517
ExecStopPost=/usr/local/bin/terminate-instance
1618
RestartSec=5

packer/windows/conf/bin/bk-install-elastic-stack.ps1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ If (![string]::IsNullOrEmpty($Env:BUILDKITE_ELASTIC_BOOTSTRAP_SCRIPT)) {
200200
Remove-Item -Path C:\buildkite-agent\elastic_bootstrap.ps1
201201
}
202202

203+
If (![string]::IsNullOrEmpty($Env:BUILDKITE_ENV_FILE_URL)) {
204+
C:\buildkite-agent\bin\bk-fetch.ps1 -From "$Env:BUILDKITE_ENV_FILE_URL" -To C:\buildkite-agent\env
205+
}
206+
203207
Write-Output "Starting the Buildkite Agent"
204208

205209
nssm install buildkite-agent C:\buildkite-agent\bin\buildkite-agent.exe start
@@ -211,6 +215,14 @@ If ($lastexitcode -ne 0) { Exit $lastexitcode }
211215
nssm set buildkite-agent AppStderr C:\buildkite-agent\buildkite-agent.log
212216
If ($lastexitcode -ne 0) { Exit $lastexitcode }
213217
nssm set buildkite-agent AppEnvironmentExtra :HOME=C:\buildkite-agent
218+
219+
If ((![string]::IsNullOrEmpty($Env:BUILDKITE_ENV_FILE_URL)) -And (Test-Path -Path C:\buildkite-agent\env -PathType leaf)) {
220+
foreach ($var in Get-Content C:\buildkite-agent\env) {
221+
nssm set buildkite-agent AppEnvironmentExtra $var
222+
If ($lastexitcode -ne 0) { Exit $lastexitcode }
223+
}
224+
}
225+
214226
If ($lastexitcode -ne 0) { Exit $lastexitcode }
215227
nssm set buildkite-agent AppExit Default Restart
216228
If ($lastexitcode -ne 0) { Exit $lastexitcode }

templates/aws-stack.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Metadata:
7070
- ArtifactsBucket
7171
- AuthorizedUsersUrl
7272
- BootstrapScriptUrl
73+
- AgentEnvFileUrl
7374
- RootVolumeSize
7475
- RootVolumeName
7576
- RootVolumeType
@@ -222,7 +223,12 @@ Parameters:
222223
Default: ""
223224

224225
BootstrapScriptUrl:
225-
Description: Optional - HTTPS or S3 URL to run on each instance during boot
226+
Description: Optional - HTTPS or S3 URL for a script to run on each instance during boot
227+
Type: String
228+
Default: ""
229+
230+
AgentEnvFileUrl:
231+
Description: Optional - HTTPS or S3 URL for a list of environment variables to propagate into the agent's execution environment. Note that these environment variables **will not** be passed into builds running on the agent, only to the agent process itself.
226232
Type: String
227233
Default: ""
228234

@@ -1008,6 +1014,7 @@ Resources:
10081014
$Env:BUILDKITE_QUEUE="${BuildkiteQueue}"
10091015
$Env:BUILDKITE_AGENT_ENABLE_GIT_MIRRORS_EXPERIMENT="${EnableAgentGitMirrorsExperiment}"
10101016
$Env:BUILDKITE_ELASTIC_BOOTSTRAP_SCRIPT="${BootstrapScriptUrl}"
1017+
$Env:BUILDKITE_ENV_FILE_URL="${AgentEnvFileUrl}"
10111018
$Env:BUILDKITE_AUTHORIZED_USERS_URL="${AuthorizedUsersUrl}"
10121019
$Env:BUILDKITE_ECR_POLICY="${ECRAccessPolicy}"
10131020
$Env:BUILDKITE_TERMINATE_INSTANCE_AFTER_JOB="${BuildkiteTerminateInstanceAfterJob}"
@@ -1057,6 +1064,7 @@ Resources:
10571064
BUILDKITE_QUEUE="${BuildkiteQueue}" \
10581065
BUILDKITE_AGENT_ENABLE_GIT_MIRRORS_EXPERIMENT="${EnableAgentGitMirrorsExperiment}" \
10591066
BUILDKITE_ELASTIC_BOOTSTRAP_SCRIPT="${BootstrapScriptUrl}" \
1067+
BUILDKITE_ENV_FILE_URL=${AgentEnvFileUrl} \
10601068
BUILDKITE_ENABLE_INSTANCE_STORAGE="${EnableInstanceStorage}" \
10611069
BUILDKITE_AUTHORIZED_USERS_URL="${AuthorizedUsersUrl}" \
10621070
BUILDKITE_ECR_POLICY="${ECRAccessPolicy}" \

0 commit comments

Comments
 (0)