Skip to content

Commit 054687d

Browse files
authored
Merge pull request #892 from toothbrush/paul/no-clobber-env
Don't clobber user's env vars.
2 parents 368a03a + 58932ec commit 054687d

File tree

4 files changed

+94
-22
lines changed

4 files changed

+94
-22
lines changed

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

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,54 @@ PLUGINS_ENABLED=()
3737
[[ $DOCKER_LOGIN_PLUGIN_ENABLED == "true" ]] && PLUGINS_ENABLED+=("docker-login")
3838

3939
# cfn-env is sourced by the environment hook in builds
40-
cat << EOF > /var/lib/buildkite-agent/cfn-env
41-
export DOCKER_VERSION=$DOCKER_VERSION
42-
export BUILDKITE_STACK_NAME=$BUILDKITE_STACK_NAME
43-
export BUILDKITE_STACK_VERSION=$BUILDKITE_STACK_VERSION
44-
export BUILDKITE_AGENTS_PER_INSTANCE=$BUILDKITE_AGENTS_PER_INSTANCE
45-
export BUILDKITE_SECRETS_BUCKET=$BUILDKITE_SECRETS_BUCKET
46-
export AWS_DEFAULT_REGION=$AWS_REGION
47-
export AWS_REGION=$AWS_REGION
48-
export PLUGINS_ENABLED="${PLUGINS_ENABLED[*]-}"
49-
export BUILDKITE_ECR_POLICY=${BUILDKITE_ECR_POLICY:-none}
40+
41+
# We will create it in two steps so that we don't need to go crazy with quoting and escaping. The
42+
# first sets up a helper function, the second populates the default values for some environment
43+
# variables.
44+
45+
# Step 1: Helper function. Note that we clobber the target file and DO NOT apply variable
46+
# substitution, this is controlled by the double-quoted "EOF".
47+
cat <<- "EOF" > /var/lib/buildkite-agent/cfn-env
48+
# The Buildkite agent sets a number of variables such as AWS_DEFAULT_REGION to fixed values which
49+
# are determined at AMI-build-time. However, sometimes a user might want to override such variables
50+
# using an env: block in their pipeline.yml. This little helper is sets the environment variables
51+
# buildkite-agent and plugins expect, except if a user want to override them, for example to do a
52+
# deployment to a region other than where the Buildkite agent lives.
53+
function set_unless_present() {
54+
local target=$1
55+
local value=$2
56+
57+
if [[ -v "${target}" ]]; then
58+
echo "^^^ +++"
59+
echo "⚠️ ${target} already set, NOT overriding! (current value \"${!target}\" set by Buildkite step env configuration, or inherited from the buildkite-agent process environment)"
60+
else
61+
echo "export ${target}=\"${value}\""
62+
declare -gx "${target}=${value}"
63+
fi
64+
}
65+
66+
function set_always() {
67+
local target=$1
68+
local value=$2
69+
70+
echo "export ${target}=\"${value}\""
71+
declare -gx "${target}=${value}"
72+
}
73+
EOF
74+
75+
# Step 2: Populate the default variable values. This time, we append to the file, and allow
76+
# variable substitution.
77+
cat << EOF >> /var/lib/buildkite-agent/cfn-env
78+
79+
set_always "BUILDKITE_AGENTS_PER_INSTANCE" "$BUILDKITE_AGENTS_PER_INSTANCE"
80+
set_always "BUILDKITE_ECR_POLICY" "${BUILDKITE_ECR_POLICY:-none}"
81+
set_always "BUILDKITE_SECRETS_BUCKET" "$BUILDKITE_SECRETS_BUCKET"
82+
set_always "BUILDKITE_STACK_NAME" "$BUILDKITE_STACK_NAME"
83+
set_always "BUILDKITE_STACK_VERSION" "$BUILDKITE_STACK_VERSION"
84+
set_always "DOCKER_VERSION" "$DOCKER_VERSION"
85+
set_always "PLUGINS_ENABLED" "${PLUGINS_ENABLED[*]-}"
86+
set_unless_present "AWS_DEFAULT_REGION" "$AWS_REGION"
87+
set_unless_present "AWS_REGION" "$AWS_REGION"
5088
EOF
5189

5290
if [[ "${BUILDKITE_AGENT_RELEASE}" == "edge" ]] ; then

packer/linux/conf/buildkite-agent/hooks/environment

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/bin/bash
22
set -eu -o pipefail
33

4+
echo "~~~ :earth_asia: Setting up environment variables"
45
# shellcheck source=/dev/null
56
source ~/cfn-env
67

@@ -10,7 +11,6 @@ export BUILDKITE_DOCKER_CONFIG_TEMP_DIRECTORY
1011
export DOCKER_CONFIG="$BUILDKITE_DOCKER_CONFIG_TEMP_DIRECTORY"
1112

1213
echo "~~~ :llama: Setting up elastic stack environment ($BUILDKITE_STACK_VERSION)"
13-
cat ~/cfn-env
1414

1515
echo "Checking docker"
1616
if ! docker ps ; then

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

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,50 @@ If ($Env:ECR_PLUGIN_ENABLED -eq "true") { $PLUGINS_ENABLED += "ecr" }
3636
If ($Env:DOCKER_LOGIN_PLUGIN_ENABLED -eq "true") { $PLUGINS_ENABLED += "docker-login" }
3737

3838
# cfn-env is sourced by the environment hook in builds
39-
Set-Content -Path C:\buildkite-agent\cfn-env -Value @"
40-
export DOCKER_VERSION=$DOCKER_VERSION
41-
export BUILDKITE_STACK_NAME=$Env:BUILDKITE_STACK_NAME
42-
export BUILDKITE_STACK_VERSION=$Env:BUILDKITE_STACK_VERSION
43-
export BUILDKITE_AGENTS_PER_INSTANCE=$Env:BUILDKITE_AGENTS_PER_INSTANCE
44-
export BUILDKITE_SECRETS_BUCKET=$Env:BUILDKITE_SECRETS_BUCKET
45-
export AWS_DEFAULT_REGION=$Env:AWS_REGION
46-
export AWS_REGION=$Env:AWS_REGION
47-
export PLUGINS_ENABLED="$PLUGINS_ENABLED"
48-
export BUILDKITE_ECR_POLICY=$Env:BUILDKITE_ECR_POLICY
39+
40+
# There's a confusing situation here, because this is PowerShell, writing out a script which will be
41+
# evaluated in Bash. So take note of the mixed export / $Env:.. idioms. This code mirrors the same
42+
# behaviour of the script in /packer/linux/conf/bin/bk-install-elastic-stack.sh.
43+
44+
Set-Content -Path C:\buildkite-agent\cfn-env -Value @'
45+
# The Buildkite agent sets a number of variables such as AWS_DEFAULT_REGION to fixed values which
46+
# are determined at AMI-build-time. However, sometimes a user might want to override such variables
47+
# using an env: block in their pipeline.yml. This little helper is sets the environment variables
48+
# buildkite-agent and plugins expect, except if a user want to override them, for example to do a
49+
# deployment to a region other than where the Buildkite agent lives.
50+
function set_unless_present() {
51+
local target=$1
52+
local value=$2
53+
54+
if [[ -v "${target}" ]]; then
55+
echo "^^^ +++"
56+
echo "⚠️ ${target} already set, NOT overriding! (current value \"${!target}\" set by Buildkite step env configuration, or inherited from the buildkite-agent process environment)"
57+
else
58+
echo "export ${target}=\"${value}\""
59+
declare -gx "${target}=${value}"
60+
fi
61+
}
62+
63+
function set_always() {
64+
local target=$1
65+
local value=$2
66+
67+
echo "export ${target}=\"${value}\""
68+
declare -gx "${target}=${value}"
69+
}
70+
'@
71+
72+
Add-Content -Path C:\buildkite-agent\cfn-env -Value @"
73+
74+
set_always "BUILDKITE_AGENTS_PER_INSTANCE" "$Env:BUILDKITE_AGENTS_PER_INSTANCE"
75+
set_always "BUILDKITE_ECR_POLICY" "$Env:BUILDKITE_ECR_POLICY"
76+
set_always "BUILDKITE_SECRETS_BUCKET" "$Env:BUILDKITE_SECRETS_BUCKET"
77+
set_always "BUILDKITE_STACK_NAME" "$Env:BUILDKITE_STACK_NAME"
78+
set_always "BUILDKITE_STACK_VERSION" "$Env:BUILDKITE_STACK_VERSION"
79+
set_always "DOCKER_VERSION" "$DOCKER_VERSION"
80+
set_always "PLUGINS_ENABLED" "$PLUGINS_ENABLED"
81+
set_unless_present "AWS_DEFAULT_REGION" "$Env:AWS_REGION"
82+
set_unless_present "AWS_REGION" "$Env:AWS_REGION"
4983
"@
5084

5185
If ($Env:BUILDKITE_AGENT_RELEASE -eq "edge") {

packer/windows/conf/buildkite-agent/hooks/environment

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/bin/bash
22
set -eu -o pipefail
33

4+
echo "~~~ :earth_asia: Setting up environment variables"
45
# shellcheck source=/dev/null
56
source ~/cfn-env
67

@@ -10,7 +11,6 @@ export BUILDKITE_DOCKER_CONFIG_TEMP_DIRECTORY
1011
export DOCKER_CONFIG="$BUILDKITE_DOCKER_CONFIG_TEMP_DIRECTORY"
1112

1213
echo "~~~ :llama: Setting up elastic stack environment ($BUILDKITE_STACK_VERSION)"
13-
cat ~/cfn-env
1414

1515
echo "Configuring built-in plugins"
1616

0 commit comments

Comments
 (0)