Skip to content

Commit 00c45ab

Browse files
author
Lachlan Donald
authored
Merge pull request #569 from buildkite/get-windows-changes-working-in-ci
Get windows changes working in ci
2 parents b229b06 + 0abe805 commit 00c45ab

File tree

14 files changed

+320
-148
lines changed

14 files changed

+320
-148
lines changed

.buildkite/pipeline.yml

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
env:
2-
AWS_STACK_NAME: "buildkite-aws-stack-test-${BUILDKITE_BUILD_NUMBER}"
3-
AWS_STACK_QUEUE_NAME: "testqueue-${BUILDKITE_BUILD_NUMBER}"
4-
51
steps:
62
- name: ":bash: Lint"
73
command: .buildkite/steps/lint.sh
@@ -17,25 +13,46 @@ steps:
1713
config: docker-compose.unit-tests.yml
1814

1915
- wait
20-
- name: ":packer: Build"
21-
command: .buildkite/steps/packer.sh
16+
- name: ":packer: :windows:"
17+
command: .buildkite/steps/packer.sh windows
18+
agents:
19+
queue: "${BUILDKITE_AGENT_META_DATA_QUEUE}"
20+
21+
- wait
22+
- name: ":cloudformation: :windows: Launch"
23+
command: .buildkite/steps/launch.sh windows
24+
agents:
25+
queue: "${BUILDKITE_AGENT_META_DATA_QUEUE}"
26+
artifact_paths: "build/aws-stack.yml"
27+
28+
- wait
29+
- name: ":cloudformation: :windows: Test"
30+
command: "docker info"
31+
timeout_in_minutes: 5
32+
agents:
33+
stack: "buildkite-aws-stack-test-windows-${BUILDKITE_BUILD_NUMBER}"
34+
queue: "testqueue-windows-${BUILDKITE_BUILD_NUMBER}"
35+
36+
- wait
37+
- name: ":packer: :linux:"
38+
command: .buildkite/steps/packer.sh linux
2239
agents:
2340
queue: "${BUILDKITE_AGENT_META_DATA_QUEUE}"
2441

2542
- wait
26-
- name: ":cloudformation: Launch"
27-
command: .buildkite/steps/test.sh
43+
- name: ":cloudformation: :linux: Launch"
44+
command: .buildkite/steps/launch.sh linux
2845
agents:
2946
queue: "${BUILDKITE_AGENT_META_DATA_QUEUE}"
3047
artifact_paths: "build/aws-stack.yml"
3148

3249
- wait
33-
- name: ":buildkite: Server Specs"
50+
- name: ":cloudformation: :linux: Test"
3451
command: "goss validate --format documentation"
3552
timeout_in_minutes: 5
3653
agents:
37-
stack: "buildkite-aws-stack-test-${BUILDKITE_BUILD_NUMBER}"
38-
queue: "testqueue-${BUILDKITE_BUILD_NUMBER}"
54+
stack: "buildkite-aws-stack-test-linux-${BUILDKITE_BUILD_NUMBER}"
55+
queue: "testqueue-linux-${BUILDKITE_BUILD_NUMBER}"
3956

4057
- wait
4158
- name: ":cloudformation: 🚚 🌎"

.buildkite/steps/cleanup.sh

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,31 @@
22
# shellcheck disable=SC2016
33
set -uxo pipefail
44

5-
if [[ -n "${AWS_STACK_NAME:-}" ]] ; then
5+
delete_test_stack() {
6+
local os="$1"
7+
local stack_name; stack_name="buildkite-aws-stack-test-${os}-${BUILDKITE_BUILD_NUMBER}"
8+
69
secrets_bucket=$(aws cloudformation describe-stacks \
7-
--stack-name "${AWS_STACK_NAME}" \
10+
--stack-name "${stack_name}" \
811
--query 'Stacks[0].Outputs[?OutputKey==`ManagedSecretsBucket`].OutputValue' \
912
--output text)
1013

1114
secrets_logging_bucket=$(aws cloudformation describe-stacks \
12-
--stack-name "${AWS_STACK_NAME}" \
15+
--stack-name "${stack_name}" \
1316
--query 'Stacks[0].Outputs[?OutputKey==`ManagedSecretsLoggingBucket`].OutputValue' \
1417
--output text)
1518

16-
echo "--- Deleting stack $AWS_STACK_NAME"
17-
aws cloudformation delete-stack --stack-name "$AWS_STACK_NAME"
19+
echo "--- Deleting stack $stack_name"
20+
aws cloudformation delete-stack --stack-name "$stack_name"
1821

19-
echo "--- Deleting buckets for $AWS_STACK_NAME"
22+
echo "--- Deleting buckets for $stack_name"
2023
aws s3 rb "s3://${secrets_bucket}" --force
2124
aws s3 rb "s3://${secrets_logging_bucket}" --force
25+
}
26+
27+
if [[ -n "${BUILDKITE_BUILD_NUMBER:-}" ]] ; then
28+
delete_test_stack "windows"
29+
delete_test_stack "linux"
2230
fi
2331

2432
if [[ $OSTYPE =~ ^darwin ]] ; then

.buildkite/steps/copy.sh

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
set -euo pipefail
33

44
# Copies an AMI to all other regions and outputs a build/mappings.yml file
5-
# Local Usage: .buildkite/steps/copy.sh <ami_id>
5+
# Local Usage: .buildkite/steps/copy.sh <linux_ami_id> <windows_ami_id>
66

77
copy_ami_to_region() {
88
local source_image_id="$1"
@@ -38,6 +38,17 @@ wait_for_ami_to_be_available() {
3838
done
3939
}
4040

41+
get_image_name() {
42+
local image_id="$1"
43+
local region="$2"
44+
45+
aws ec2 describe-images \
46+
--image-ids "$image_id" \
47+
--output text \
48+
--region "$region" \
49+
--query 'Images[*].Name'
50+
}
51+
4152
make_ami_public() {
4253
local image_id="$1"
4354
local region="$2"
@@ -76,13 +87,16 @@ IMAGES=(
7687
)
7788

7889
# Configuration
79-
source_image_id="${1:-}"
90+
linux_source_image_id="${1:-}"
91+
windows_source_image_id="${2:-}"
92+
8093
source_region="${AWS_REGION}"
8194
mapping_file="build/mappings.yml"
8295

83-
# Read the source_image_id from meta-data if empty
84-
if [[ -z "$source_image_id" ]] ; then
85-
source_image_id=$(buildkite-agent meta-data get image_id)
96+
# Read the source images from meta-data if no arguments are provided
97+
if [ $# -eq 0 ] ; then
98+
linux_source_image_id=$(buildkite-agent meta-data get "linux_image_id")
99+
windows_source_image_id=$(buildkite-agent meta-data get "windows_image_id")
86100
fi
87101

88102
# If we're not on the master branch or a tag build skip the copy
@@ -92,33 +106,37 @@ if [[ $BUILDKITE_BRANCH != "master" ]] && [[ "$BUILDKITE_TAG" != "$BUILDKITE_BRA
92106
cat << EOF > "$mapping_file"
93107
Mappings:
94108
AWSRegion2AMI:
95-
${AWS_REGION} : { AMI: $source_image_id }
109+
${AWS_REGION} : { linux: $linux_source_image_id, windows: $windows_source_image_id }
96110
EOF
97111
exit 0
98112
fi
99113

100-
s3_mappings_cache="s3://${BUILDKITE_AWS_STACK_BUCKET}/mappings-${source_image_id}-${BUILDKITE_BRANCH}.yml"
114+
s3_mappings_cache=$(printf "s3://%s/mappings-%s-%s-%s.yml" \
115+
"${BUILDKITE_AWS_STACK_BUCKET}" \
116+
"${linux_source_image_id}" \
117+
"${windows_source_image_id}" \
118+
"${BUILDKITE_BRANCH}")
101119

102120
# Check if there is a previously copy in the cache bucket
103121
if aws s3 cp "${s3_mappings_cache}" "$mapping_file" ; then
104122
echo "--- Skipping AMI copy, was previously copied"
105123
exit 0
106124
fi
107125

108-
# Get the image name to copy to other amis
109-
source_image_name=$(aws ec2 describe-images \
110-
--image-ids "$source_image_id" \
111-
--output text \
112-
--region "$source_region" \
113-
--query 'Images[*].Name')
126+
# Get the image names to copy to other regions
127+
linux_source_image_name=$(get_image_name "$linux_source_image_id" "$source_region")
128+
windows_source_image_name=$(get_image_name "$windows_source_image_id" "$source_region")
114129

115130
# Copy to all other regions
116131
for region in ${ALL_REGIONS[*]}; do
117132
if [[ $region != "$source_region" ]] ; then
118-
echo "--- Copying $source_image_id to $region" >&2
119-
IMAGES+=("$(copy_ami_to_region "$source_image_id" "$source_region" "$region" "${source_image_name}-${region}")")
133+
echo "--- Copying :linux: $linux_source_image_id to $region" >&2
134+
IMAGES+=("$(copy_ami_to_region "$linux_source_image_id" "$source_region" "$region" "${linux_source_image_name}-${region}")")
135+
136+
echo "--- Copying :windows: $windows_source_image_id to $region" >&2
137+
IMAGES+=("$(copy_ami_to_region "$windows_source_image_id" "$source_region" "$region" "${windows_source_image_name}-${region}")")
120138
else
121-
IMAGES+=("$source_image_id")
139+
IMAGES+=("$linux_source_image_id" "$windows_source_image_id")
122140
fi
123141
done
124142

@@ -130,20 +148,32 @@ Mappings:
130148
EOF
131149

132150
echo "--- Waiting for AMIs to become available" >&2
133-
for ((i=0; i<${#IMAGES[*]}; i++)); do
134-
region="${ALL_REGIONS[i]}"
135-
image_id="${IMAGES[i]}"
136151

137-
wait_for_ami_to_be_available "$image_id" "$region" >&2
152+
for region in ${ALL_REGIONS[*]}; do
153+
linux_image_id="${IMAGES[0]}"
154+
windows_image_id="${IMAGES[1]}"
155+
156+
wait_for_ami_to_be_available "$linux_image_id" "$region" >&2
138157

139-
# Make the AMI public if it's not the source image
140-
if [[ $image_id != "$source_image_id" ]] ; then
141-
echo "Making ${image_id} public" >&2
142-
make_ami_public "$image_id" "$region"
158+
# Make the linux AMI public if it's not the source image
159+
if [[ $linux_image_id != "$linux_source_image_id" ]] ; then
160+
echo "Making :linux: ${linux_image_id} public" >&2
161+
make_ami_public "$linux_image_id" "$region"
162+
fi
163+
164+
wait_for_ami_to_be_available "$windows_image_id" "$region" >&2
165+
166+
# Make the windows AMI public if it's not the source image
167+
if [[ $windows_image_id != "$windows_source_image_id" ]] ; then
168+
echo "Making :windows: ${windows_image_id} public" >&2
169+
make_ami_public "$windows_image_id" "$region"
143170
fi
144171

145172
# Write yaml to file
146-
echo " $region : { AMI: $image_id }" >> "$mapping_file"
173+
echo " $region : { linux: $linux_image_id, windows: $windows_image_id }" >> "$mapping_file"
174+
175+
# Shift off the processed images
176+
IMAGES=("${IMAGES[@]:2}")
147177
done
148178

149179
echo "--- Uploading mapping to s3 cache"

.buildkite/steps/test.sh renamed to .buildkite/steps/launch.sh

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
# shellcheck disable=SC1117
33
set -eu
44

5+
os="${1:-linux}"
6+
stack_name="buildkite-aws-stack-test-${os}-${BUILDKITE_BUILD_NUMBER}"
7+
stack_queue_name="testqueue-${os}-${BUILDKITE_BUILD_NUMBER}"
8+
59
# download parfait binary
610
wget -N https://github.com/lox/parfait/releases/download/v1.1.3/parfait_linux_amd64
711
mv parfait_linux_amd64 parfait
@@ -12,8 +16,16 @@ subnets=$(aws ec2 describe-subnets --filters "Name=vpc-id,Values=$vpc_id" --quer
1216
subnet_ids=$(awk '{print $1}' <<< "$subnets" | tr ' ' ',' | tr '\n' ',' | sed 's/,$//')
1317
az_ids=$(awk '{print $2}' <<< "$subnets" | tr ' ' ',' | tr '\n' ',' | sed 's/,$//')
1418

15-
image_id=$(buildkite-agent meta-data get image_id)
16-
echo "Using AMI $image_id"
19+
image_id=$(buildkite-agent meta-data get "${os}_image_id")
20+
echo "Using AMI $image_id for $os"
21+
22+
instance_type="t3.nano"
23+
instance_disk="10"
24+
25+
if [[ "$os" == "windows" ]] ; then
26+
instance_type="m5.large"
27+
instance_disk="100"
28+
fi
1729

1830
cat << EOF > config.json
1931
[
@@ -23,15 +35,19 @@ cat << EOF > config.json
2335
},
2436
{
2537
"ParameterKey": "BuildkiteQueue",
26-
"ParameterValue": "${AWS_STACK_QUEUE_NAME}"
38+
"ParameterValue": "${stack_queue_name}"
2739
},
2840
{
2941
"ParameterKey": "KeyName",
30-
"ParameterValue": "${AWS_KEYPAIR:-default}"
42+
"ParameterValue": "${AWS_KEYPAIR:-aws-stack-test}"
3143
},
3244
{
3345
"ParameterKey": "InstanceType",
34-
"ParameterValue": "t2.nano"
46+
"ParameterValue": "${instance_type}"
47+
},
48+
{
49+
"ParameterKey": "InstanceOperatingSystem",
50+
"ParameterValue": "${os}"
3551
},
3652
{
3753
"ParameterKey": "VpcId",
@@ -59,7 +75,7 @@ cat << EOF > config.json
5975
},
6076
{
6177
"ParameterKey": "RootVolumeSize",
62-
"ParameterValue": "10"
78+
"ParameterValue": "${instance_disk}"
6379
},
6480
{
6581
"ParameterKey": "EnableDockerUserNamespaceRemap",
@@ -77,13 +93,13 @@ cat << EOF > config.json
7793
EOF
7894

7995
echo "--- Building templates"
80-
make mappings-for-image build "IMAGE_ID=$image_id"
96+
make "mappings-for-${os}-image" build/aws-stack.yml "IMAGE_ID=$image_id"
8197

8298
echo "--- Validating templates"
8399
make validate
84100

85-
echo "--- Creating stack ${AWS_STACK_NAME}"
86-
make create-stack "STACK_NAME=$AWS_STACK_NAME"
101+
echo "--- Creating stack ${stack_name}"
102+
make create-stack "STACK_NAME=$stack_name"
87103

88104
echo "+++ ⌛️ Waiting for update to complete"
89-
./parfait watch-stack "${AWS_STACK_NAME}"
105+
./parfait watch-stack "${stack_name}"

.buildkite/steps/packer.sh

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,29 @@ if [[ -z "${BUILDKITE_AWS_STACK_BUCKET}" ]] ; then
66
exit 1
77
fi
88

9-
mkdir -p build
9+
os="${1:-linux}"
10+
agent_binary="buildkite-agent-${os}-amd64"
11+
12+
if [[ "$os" == "windows" ]] ; then
13+
agent_binary+=".exe"
14+
fi
15+
16+
mkdir -p "build/"
1017

1118
# Build a hash of packer files and the agent versions
12-
packer_files_sha=$(find packer/ plugins/ -type f -print0 | xargs -0 sha1sum | awk '{print $1}' | sort | sha1sum | awk '{print $1}')
13-
stable_agent_sha=$(curl -Lfs https://download.buildkite.com/agent/stable/latest/buildkite-agent-linux-amd64.sha256)
14-
unstable_agent_sha=$(curl -Lfs https://download.buildkite.com/agent/unstable/latest/buildkite-agent-linux-amd64.sha256)
19+
packer_files_sha=$(find "packer/${os}" plugins/ -type f -print0 | xargs -0 sha1sum | awk '{print $1}' | sort | sha1sum | awk '{print $1}')
20+
stable_agent_sha=$(curl -Lfs "https://download.buildkite.com/agent/stable/latest/${agent_binary}.sha256")
21+
unstable_agent_sha=$(curl -Lfs "https://download.buildkite.com/agent/unstable/latest/${agent_binary}.sha256")
1522
packer_hash=$(echo "$packer_files_sha" "$stable_agent_sha" "$unstable_agent_sha" | sha1sum | awk '{print $1}')
1623

17-
echo "Packer image hash is $packer_hash"
18-
packer_file="${packer_hash}.packer"
24+
echo "Packer image hash for ${os} is ${packer_hash}"
25+
packer_file="packer-${packer_hash}-${os}.output"
1926

2027
# Only build packer image if one with the same hash doesn't exist
2128
if ! aws s3 cp "s3://${BUILDKITE_AWS_STACK_BUCKET}/${packer_file}" . ; then
22-
make packer
23-
aws s3 cp packer.output "s3://${BUILDKITE_AWS_STACK_BUCKET}/${packer_file}"
24-
mv packer.output "${packer_file}"
29+
make "packer-${os}.output"
30+
aws s3 cp "packer-${os}.output" "s3://${BUILDKITE_AWS_STACK_BUCKET}/${packer_file}"
31+
mv "packer-${os}.output" "${packer_file}"
2532
else
2633
echo "Skipping packer build, no changes"
2734
fi
@@ -30,4 +37,4 @@ fi
3037
image_id=$(grep -Eo "${AWS_REGION}: (ami-.+)$" "$packer_file" | awk '{print $2}')
3138
echo "AMI for ${AWS_REGION} is $image_id"
3239

33-
buildkite-agent meta-data set image_id "$image_id"
40+
buildkite-agent meta-data set "${os}_image_id" "$image_id"

.buildkite/steps/publish.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ echo "--- Fetching latest git tags"
2929
git fetch --tags
3030

3131
echo "--- Building :cloudformation: templates"
32-
make build
32+
make build/aws-stack.yml
3333

3434
# Publish the top-level mappings only on when we see the most recent tag on master
3535
if is_latest_tag ; then

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
build/
2-
packer.output
2+
packer*.output
33
config.json
44
vendor/
55
node_modules/

0 commit comments

Comments
 (0)