|
30 | 30 | if [[ "${variant}" == "base" ]]; then
|
31 | 31 | make "packer-base-${os}-${arch}.output"
|
32 | 32 | else
|
33 |
| - # Require a golden base AMI from metadata |
| 33 | + # Try metadata first, then S3 fallback for base AMI ID |
34 | 34 | base_ami_id="$(buildkite-agent meta-data get "${os}-base-${arch}-ami" || true)"
|
35 | 35 |
|
36 | 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 |
| 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 |
38 | 50 | exit 1
|
39 | 51 | fi
|
40 | 52 |
|
|
43 | 55 |
|
44 | 56 | # Upload to S3 with timestamped filename
|
45 | 57 | 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 | + |
46 | 66 | mv "${local_output}" "${packer_file}"
|
47 | 67 |
|
48 | 68 | # Get the image id from the packer build output for later steps
|
|
0 commit comments