|
17 | 17 |
|
18 | 18 | mkdir -p "build/"
|
19 | 19 |
|
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") |
25 | 22 | 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" |
37 | 24 | local_output="packer-base-${os}-${arch}.output"
|
38 | 25 | else
|
39 |
| - packer_file="packer-${packer_hash}-${os}-${arch}.output" |
| 26 | + packer_file="packer-${os}-${arch}-${timestamp}.output" |
40 | 27 | local_output="packer-${os}-${arch}.output"
|
41 | 28 | fi
|
42 | 29 |
|
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)" |
71 | 35 |
|
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 |
73 | 39 | 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" |
78 | 42 | fi
|
79 | 43 |
|
| 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 | + |
80 | 48 | # Get the image id from the packer build output for later steps
|
81 | 49 | image_id=$(grep -Eo "${AWS_REGION}: (ami-.+)$" "$packer_file" | awk '{print $2}')
|
82 | 50 | echo "AMI for ${AWS_REGION} is $image_id"
|
|
0 commit comments