Skip to content

Commit 968ab73

Browse files
authored
Merge pull request #1594 from buildkite/fix/packer_base_ami_lookup
2 parents 9db4e18 + 88d4609 commit 968ab73

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

.buildkite/steps/packer.sh

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,23 @@ fi
3030
if [[ "${variant}" == "base" ]]; then
3131
make "packer-base-${os}-${arch}.output"
3232
else
33-
# Require a golden base AMI from metadata
33+
# Try metadata first, then S3 fallback for base AMI ID
3434
base_ami_id="$(buildkite-agent meta-data get "${os}-base-${arch}-ami" || true)"
3535

3636
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
37+
echo "Base AMI ID not found in metadata, checking S3 for latest base image..."
38+
39+
# Try to fetch the latest base AMI output from S3
40+
latest_base_file="packer-base-${os}-${arch}-latest.output"
41+
if aws s3 cp "s3://${BUILDKITE_AWS_STACK_BUCKET}/${latest_base_file}" "/tmp/${latest_base_file}" 2>/dev/null; then
42+
base_ami_id=$(grep -Eo "${AWS_REGION}: (ami-.+)$" "/tmp/${latest_base_file}" | awk '{print $2}')
43+
echo "Found base AMI ID from S3: $base_ami_id"
44+
rm -f "/tmp/${latest_base_file}"
45+
fi
46+
fi
47+
48+
if [[ -z "$base_ami_id" ]]; then
49+
echo "ERROR: No golden base AMI found for ${os}/${arch}. Ensure a base image has been built from main branch." >&2
3850
exit 1
3951
fi
4052

@@ -43,6 +55,14 @@ fi
4355

4456
# Upload to S3 with timestamped filename
4557
aws s3 cp "${local_output}" "s3://${BUILDKITE_AWS_STACK_BUCKET}/${packer_file}"
58+
59+
# For base images on main branch, also upload as "latest"
60+
if [[ "${variant}" == "base" && "${BUILDKITE_BRANCH:-}" == "main" ]]; then
61+
latest_file="packer-base-${os}-${arch}-latest.output"
62+
aws s3 cp "${local_output}" "s3://${BUILDKITE_AWS_STACK_BUCKET}/${latest_file}"
63+
echo "Updated latest base AMI pointer for ${os}/${arch}"
64+
fi
65+
4666
mv "${local_output}" "${packer_file}"
4767

4868
# Get the image id from the packer build output for later steps

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-18 12:56:38 UTC
1+
# Last updated: 2025-09-19 07:10:59 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-18 12:56:38 UTC
1+
# Last updated: 2025-09-19 07:10:59 UTC

0 commit comments

Comments
 (0)