Skip to content

Commit 9db4e18

Browse files
authored
Merge pull request #1593 from buildkite/base_ami_rebuild_if_changed
Simplify base AMI build logic
2 parents 06ad08e + 8133a64 commit 9db4e18

File tree

4 files changed

+23
-55
lines changed

4 files changed

+23
-55
lines changed

.buildkite/pipeline.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ steps:
6969
depends_on:
7070
- "linting"
7171
- "fixperms-tests"
72-
if_changed: "packer/windows/**"
72+
if_changed: '{packer/windows/.trigger-base-build,packer/windows/base.pkr.hcl,packer/windows/conf/**,packer/windows/scripts/enable-containers.ps1,packer/windows/scripts/install-cloudwatch-agent.ps1,packer/windows/scripts/install-docker.ps1,packer/windows/scripts/install-lifecycled.ps1,packer/windows/scripts/install-session-manager-plugin.ps1,packer/windows/scripts/install-utils.ps1}'
7373
plugins:
7474
- *aws_role_plugin
7575

@@ -142,7 +142,7 @@ steps:
142142
depends_on:
143143
- "linting"
144144
- "fixperms-tests"
145-
if_changed: "packer/linux/**"
145+
if_changed: '{packer/linux/.trigger-base-build,packer/linux/base.pkr.hcl,packer/linux/conf/**,packer/linux/scripts/cleanup.sh,packer/linux/scripts/install-cloudwatch-agent.sh,packer/linux/scripts/install-docker.sh,packer/linux/scripts/install-session-manager-plugin.sh,packer/linux/scripts/install-utils.sh}'
146146
plugins:
147147
- *aws_role_plugin
148148

@@ -216,7 +216,7 @@ steps:
216216
depends_on:
217217
- "linting"
218218
- "fixperms-tests"
219-
if_changed: "packer/linux/**"
219+
if_changed: '{packer/linux/.trigger-base-build,packer/linux/base.pkr.hcl,packer/linux/conf/**,packer/linux/scripts/cleanup.sh,packer/linux/scripts/install-cloudwatch-agent.sh,packer/linux/scripts/install-docker.sh,packer/linux/scripts/install-session-manager-plugin.sh,packer/linux/scripts/install-utils.sh}'
220220
plugins:
221221
- *aws_role_plugin
222222

.buildkite/steps/packer.sh

Lines changed: 18 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,66 +17,34 @@ fi
1717

1818
mkdir -p "build/"
1919

20-
# Build a hash of packer files and the agent versions
21-
packer_files_sha=$(find Makefile "packer/${os}" plugins/ -type f -print0 | xargs -0 sha256sum | awk '{print $1}' | sort | sha256sum | awk '{print $1}')
22-
internal_files_sha=$(find go.mod go.sum internal/ -type f -print0 | xargs -0 sha256sum | awk '{print $1}' | sort | sha256sum | awk '{print $1}')
23-
stable_agent_sha=$(curl -Lfs "https://download.buildkite.com/agent/stable/latest/${agent_binary}.sha256")
24-
unstable_agent_sha=$(curl -Lfs "https://download.buildkite.com/agent/unstable/latest/${agent_binary}.sha256")
20+
# Generate timestamped output filenames
21+
timestamp=$(date -u +"%Y%m%d-%H%M%S")
2522
if [[ "${variant}" == "base" ]]; then
26-
# Base AMI doesn't use Go dependencies
27-
packer_hash=$(echo "$packer_files_sha" "$arch" "$stable_agent_sha" "$unstable_agent_sha" "$variant" | sha256sum | awk '{print $1}')
28-
else
29-
# Full AMI includes Go dependencies for fix-perms binary
30-
packer_hash=$(echo "$packer_files_sha" "$internal_files_sha" "$arch" "$stable_agent_sha" "$unstable_agent_sha" "$variant" | sha256sum | awk '{print $1}')
31-
fi
32-
33-
# Include variant in the hash so base and full images don’t clash
34-
echo "Packer image hash for ${os}/${arch} (${variant}) is ${packer_hash}"
35-
if [[ "${variant}" == "base" ]]; then
36-
packer_file="packer-${packer_hash}-${os}-${arch}-base.output"
23+
packer_file="packer-base-${os}-${arch}-${timestamp}.output"
3724
local_output="packer-base-${os}-${arch}.output"
3825
else
39-
packer_file="packer-${packer_hash}-${os}-${arch}.output"
26+
packer_file="packer-${os}-${arch}-${timestamp}.output"
4027
local_output="packer-${os}-${arch}.output"
4128
fi
4229

43-
# Only build packer image if one with the same hash doesn't exist, and we're not being forced
44-
if [[ -n "${PACKER_REBUILD:-}" ]] || ! aws s3 cp "s3://${BUILDKITE_AWS_STACK_BUCKET}/${packer_file}" .; then
45-
if [[ "${variant}" == "base" ]]; then
46-
make "packer-base-${os}-${arch}.output"
47-
else
48-
# Require a golden base AMI. Try metadata first, then S3 as fallback.
49-
base_ami_id="$(buildkite-agent meta-data get "${os}-base-${arch}-ami" || true)"
50-
51-
if [[ -z "$base_ami_id" ]]; then
52-
echo "Base AMI ID not found in metadata, checking S3 for latest base image..."
53-
54-
# Calculate hash for base image to find the S3 file
55-
# Base AMI doesn't include Go dependencies, so exclude them from hash
56-
base_packer_hash=$(echo "$packer_files_sha" "$arch" "$stable_agent_sha" "$unstable_agent_sha" "base" | sha256sum | awk '{print $1}')
57-
base_packer_file="packer-${base_packer_hash}-${os}-${arch}-base.output"
58-
59-
# Try to download and extract AMI ID from the base image packer output
60-
if aws s3 cp "s3://${BUILDKITE_AWS_STACK_BUCKET}/${base_packer_file}" "/tmp/${base_packer_file}" 2>/dev/null; then
61-
base_ami_id=$(grep -Eo "${AWS_REGION}: (ami-.+)$" "/tmp/${base_packer_file}" | awk '{print $2}')
62-
echo "Found base AMI ID from S3: $base_ami_id"
63-
rm -f "/tmp/${base_packer_file}"
64-
fi
65-
fi
66-
67-
if [[ -z "$base_ami_id" ]]; then
68-
echo "ERROR: No golden base AMI found for ${os}/${arch}. Ensure the corresponding base image step ran and uploaded the AMI ID." >&2
69-
exit 1
70-
fi
30+
if [[ "${variant}" == "base" ]]; then
31+
make "packer-base-${os}-${arch}.output"
32+
else
33+
# Require a golden base AMI from metadata
34+
base_ami_id="$(buildkite-agent meta-data get "${os}-base-${arch}-ami" || true)"
7135

72-
make "packer-${os}-${arch}.output" BASE_AMI_ID="$base_ami_id"
36+
if [[ -z "$base_ami_id" ]]; then
37+
echo "ERROR: No golden base AMI found for ${os}/${arch}. Ensure the corresponding base image step ran and uploaded the AMI ID." >&2
38+
exit 1
7339
fi
74-
aws s3 cp "${local_output}" "s3://${BUILDKITE_AWS_STACK_BUCKET}/${packer_file}"
75-
mv "${local_output}" "${packer_file}"
76-
else
77-
echo "Skipping packer build, no changes"
40+
41+
make "packer-${os}-${arch}.output" BASE_AMI_ID="$base_ami_id"
7842
fi
7943

44+
# Upload to S3 with timestamped filename
45+
aws s3 cp "${local_output}" "s3://${BUILDKITE_AWS_STACK_BUCKET}/${packer_file}"
46+
mv "${local_output}" "${packer_file}"
47+
8048
# Get the image id from the packer build output for later steps
8149
image_id=$(grep -Eo "${AWS_REGION}: (ami-.+)$" "$packer_file" | awk '{print $2}')
8250
echo "AMI for ${AWS_REGION} is $image_id"

packer/linux/.trigger-base-build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# Last updated: 2025-09-10 14:03:43 UTC
1+
# Last updated: 2025-09-18 12:56:38 UTC

packer/windows/.trigger-base-build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# Last updated: 2025-09-10 14:03:43 UTC
1+
# Last updated: 2025-09-18 12:56:38 UTC

0 commit comments

Comments
 (0)