Skip to content

Commit 8431dfb

Browse files
committed
Switch AMI build back to simple approach with runner cleanup
- Revert from Packer to simple create-image approach - Build jobs now run on runs-on instances directly - Added runner state cleanup before AMI creation to fix registration issues - Removes .runner, .credentials files so new instances register fresh Signed-off-by: Joe Isaacs <[email protected]>
1 parent 54c4b07 commit 8431dfb

File tree

2 files changed

+159
-78
lines changed

2 files changed

+159
-78
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
name: "Build Vortex CI AMI"
2+
description: "Build a custom Amazon Machine Image for Vortex CI runners"
3+
4+
inputs:
5+
arch:
6+
description: "Target architecture: x64 or arm64"
7+
required: true
8+
ami-prefix:
9+
description: "Prefix for AMI name"
10+
required: false
11+
default: "vortex-ci"
12+
retention-days:
13+
description: "Number of days before AMI is deprecated"
14+
required: false
15+
default: "30"
16+
17+
outputs:
18+
ami-id:
19+
description: "The ID of the built AMI"
20+
value: ${{ steps.create-ami.outputs.ami_id }}
21+
ami-name:
22+
description: "The name of the built AMI"
23+
value: ${{ steps.create-ami.outputs.ami_name }}
24+
25+
runs:
26+
using: "composite"
27+
steps:
28+
- name: Setup Rust
29+
uses: ./.github/actions/setup-rust
30+
31+
- name: Setup flatc
32+
uses: ./.github/actions/setup-flatc
33+
34+
- name: Install extra dependencies
35+
shell: bash
36+
run: |
37+
sudo apt-get update
38+
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
39+
cmake \
40+
ninja-build \
41+
clang \
42+
lld \
43+
llvm
44+
45+
- name: Install nightly toolchain
46+
shell: bash
47+
run: |
48+
rustup toolchain install nightly
49+
rustup component add --toolchain nightly rustfmt clippy rust-src miri llvm-tools-preview
50+
51+
- name: Install cargo tools
52+
shell: bash
53+
run: |
54+
cargo install cargo-nextest --locked
55+
cargo install cargo-hack --locked
56+
cargo install grcov --locked
57+
58+
- name: Clean runner for AMI
59+
shell: bash
60+
run: |
61+
echo "=== Cleaning runner state for clean AMI ==="
62+
63+
# The runner service name
64+
RUNNER_SVC=$(systemctl list-units --type=service | grep actions.runner | awk '{print $1}' | head -1)
65+
66+
if [ -n "$RUNNER_SVC" ]; then
67+
echo "Stopping runner service: $RUNNER_SVC"
68+
sudo systemctl stop "$RUNNER_SVC" || true
69+
fi
70+
71+
# Remove runner registration (this is the key!)
72+
RUNNER_DIR="/home/runner/actions-runner"
73+
if [ -d "$RUNNER_DIR" ]; then
74+
echo "Cleaning runner directory..."
75+
sudo rm -f "$RUNNER_DIR/.runner" || true
76+
sudo rm -f "$RUNNER_DIR/.credentials" || true
77+
sudo rm -f "$RUNNER_DIR/.credentials_rsaparams" || true
78+
sudo rm -rf "$RUNNER_DIR/_diag" || true
79+
sudo rm -rf "$RUNNER_DIR/_work" || true
80+
fi
81+
82+
# Clear temp files
83+
sudo rm -rf /tmp/* || true
84+
sudo rm -rf /var/tmp/* || true
85+
86+
# Clear apt cache to reduce AMI size
87+
sudo apt-get clean
88+
sudo rm -rf /var/lib/apt/lists/*
89+
90+
echo "=== Runner state cleaned ==="
91+
92+
- name: Create AMI
93+
id: create-ami
94+
shell: bash
95+
env:
96+
AMI_PREFIX: ${{ inputs.ami-prefix }}
97+
ARCH: ${{ inputs.arch }}
98+
RETENTION_DAYS: ${{ inputs.retention-days }}
99+
run: |
100+
export AWS_REGION="$RUNS_ON_AWS_REGION"
101+
export AWS_DEFAULT_REGION="$RUNS_ON_AWS_REGION"
102+
103+
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
104+
AMI_NAME="${AMI_PREFIX}-${ARCH}-${TIMESTAMP}"
105+
DEPRECATION_TIME=$(date -u -d "+${RETENTION_DAYS} days" +%Y-%m-%dT%H:%M:%SZ)
106+
107+
echo "Creating AMI: $AMI_NAME from instance $RUNS_ON_INSTANCE_ID in region $AWS_REGION"
108+
109+
AMI_ID=$(aws ec2 create-image \
110+
--instance-id "$RUNS_ON_INSTANCE_ID" \
111+
--name "$AMI_NAME" \
112+
--description "Vortex CI runner image for ${ARCH}" \
113+
--no-reboot \
114+
--tag-specifications "ResourceType=image,Tags=[{Key=Name,Value=$AMI_NAME},{Key=Environment,Value=ci},{Key=Arch,Value=$ARCH},{Key=ManagedBy,Value=github-actions}]" \
115+
--query 'ImageId' \
116+
--output text)
117+
118+
echo "Waiting for AMI to be available..."
119+
aws ec2 wait image-available --image-ids "$AMI_ID"
120+
121+
echo "Setting deprecation time to $DEPRECATION_TIME"
122+
aws ec2 enable-image-deprecation \
123+
--image-id "$AMI_ID" \
124+
--deprecate-at "$DEPRECATION_TIME"
125+
126+
echo "ami_id=$AMI_ID" >> $GITHUB_OUTPUT
127+
echo "ami_name=$AMI_NAME" >> $GITHUB_OUTPUT
128+
echo "AMI created: $AMI_ID ($AMI_NAME)"

.github/workflows/ami-prebuild.yml

Lines changed: 31 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,19 @@ env:
4040
AWS_REGION: eu-west-1
4141

4242
jobs:
43+
# Build AMI by running on a runs-on instance and creating an image from it
4344
build-x64:
4445
name: "Build AMI (x64)"
4546
if: ${{ github.event_name != 'workflow_dispatch' || github.event.inputs.arch == '' || github.event.inputs.arch == 'x64' }}
46-
runs-on: ubuntu-latest
47+
runs-on:
48+
- runs-on=${{ github.run_id }}
49+
- runner=2cpu-linux-x64
50+
- family=m7i+m7i-flex+m7a
51+
- tag=ami-build-x64
4752
timeout-minutes: 60
4853
outputs:
49-
ami-id: ${{ steps.build.outputs.ami_id }}
50-
ami-name: ${{ steps.build.outputs.ami_name }}
54+
ami-id: ${{ steps.build-ami.outputs.ami-id }}
55+
ami-name: ${{ steps.build-ami.outputs.ami-name }}
5156

5257
steps:
5358
- name: Checkout
@@ -59,58 +64,34 @@ jobs:
5964
role-to-assume: arn:aws:iam::375504701696:role/GitHubBenchmarkRole
6065
aws-region: ${{ env.AWS_REGION }}
6166

62-
- name: Setup Packer
63-
uses: hashicorp/setup-packer@main
67+
- name: Build AMI
68+
id: build-ami
69+
uses: ./.github/actions/build-ami
6470
with:
65-
version: "1.11.2"
66-
67-
- name: Packer Init
68-
working-directory: .github/packer
69-
run: packer init vortex-ci.pkr.hcl
70-
71-
- name: Packer Build
72-
id: build
73-
working-directory: .github/packer
74-
run: |
75-
packer build \
76-
-var "arch=x64" \
77-
-var "aws_region=${{ env.AWS_REGION }}" \
78-
-var "subnet_id=${{ secrets.AWS_SUBNET_ID }}" \
79-
-machine-readable \
80-
vortex-ci.pkr.hcl | tee packer-output.log
81-
82-
# Extract AMI ID from Packer output
83-
AMI_ID=$(grep 'artifact,0,id' packer-output.log | tail -1 | cut -d',' -f6 | cut -d':' -f2)
84-
AMI_NAME=$(grep 'artifact,0,string' packer-output.log | tail -1 | grep -oP 'AMIs were created:.*' | sed 's/AMIs were created:\\n\\n//' | head -1 || echo "vortex-ci-x64")
85-
86-
echo "ami_id=$AMI_ID" >> $GITHUB_OUTPUT
87-
echo "ami_name=$AMI_NAME" >> $GITHUB_OUTPUT
88-
echo "Built AMI: $AMI_ID"
89-
90-
- name: Set AMI Deprecation
91-
run: |
92-
RETENTION_DAYS=${{ inputs.retention-days || '30' }}
93-
DEPRECATION_TIME=$(date -u -d "+${RETENTION_DAYS} days" +%Y-%m-%dT%H:%M:%SZ)
94-
aws ec2 enable-image-deprecation \
95-
--image-id "${{ steps.build.outputs.ami_id }}" \
96-
--deprecate-at "$DEPRECATION_TIME"
97-
echo "AMI will be deprecated at $DEPRECATION_TIME"
71+
arch: x64
72+
ami-prefix: vortex-ci
73+
retention-days: ${{ inputs.retention-days || '30' }}
9874

9975
- name: Summary
10076
run: |
10177
echo "## AMI Build Complete (x64)" >> $GITHUB_STEP_SUMMARY
10278
echo "" >> $GITHUB_STEP_SUMMARY
103-
echo "- **AMI ID:** ${{ steps.build.outputs.ami_id }}" >> $GITHUB_STEP_SUMMARY
79+
echo "- **AMI ID:** ${{ steps.build-ami.outputs.ami-id }}" >> $GITHUB_STEP_SUMMARY
80+
echo "- **AMI Name:** ${{ steps.build-ami.outputs.ami-name }}" >> $GITHUB_STEP_SUMMARY
10481
echo "- **Deprecation:** ${{ inputs.retention-days || '30' }} days" >> $GITHUB_STEP_SUMMARY
10582
10683
build-arm64:
10784
name: "Build AMI (arm64)"
10885
if: ${{ github.event_name != 'workflow_dispatch' || github.event.inputs.arch == '' || github.event.inputs.arch == 'arm64' }}
109-
runs-on: ubuntu-latest
86+
runs-on:
87+
- runs-on=${{ github.run_id }}
88+
- runner=2cpu-linux-arm64
89+
- family=m7g
90+
- tag=ami-build-arm64
11091
timeout-minutes: 60
11192
outputs:
112-
ami-id: ${{ steps.build.outputs.ami_id }}
113-
ami-name: ${{ steps.build.outputs.ami_name }}
93+
ami-id: ${{ steps.build-ami.outputs.ami-id }}
94+
ami-name: ${{ steps.build-ami.outputs.ami-name }}
11495

11596
steps:
11697
- name: Checkout
@@ -122,48 +103,20 @@ jobs:
122103
role-to-assume: arn:aws:iam::375504701696:role/GitHubBenchmarkRole
123104
aws-region: ${{ env.AWS_REGION }}
124105

125-
- name: Setup Packer
126-
uses: hashicorp/setup-packer@main
106+
- name: Build AMI
107+
id: build-ami
108+
uses: ./.github/actions/build-ami
127109
with:
128-
version: "1.11.2"
129-
130-
- name: Packer Init
131-
working-directory: .github/packer
132-
run: packer init vortex-ci.pkr.hcl
133-
134-
- name: Packer Build
135-
id: build
136-
working-directory: .github/packer
137-
run: |
138-
packer build \
139-
-var "arch=arm64" \
140-
-var "aws_region=${{ env.AWS_REGION }}" \
141-
-var "subnet_id=${{ secrets.AWS_SUBNET_ID }}" \
142-
-machine-readable \
143-
vortex-ci.pkr.hcl | tee packer-output.log
144-
145-
# Extract AMI ID from Packer output
146-
AMI_ID=$(grep 'artifact,0,id' packer-output.log | tail -1 | cut -d',' -f6 | cut -d':' -f2)
147-
AMI_NAME=$(grep 'artifact,0,string' packer-output.log | tail -1 | grep -oP 'AMIs were created:.*' | sed 's/AMIs were created:\\n\\n//' | head -1 || echo "vortex-ci-arm64")
148-
149-
echo "ami_id=$AMI_ID" >> $GITHUB_OUTPUT
150-
echo "ami_name=$AMI_NAME" >> $GITHUB_OUTPUT
151-
echo "Built AMI: $AMI_ID"
152-
153-
- name: Set AMI Deprecation
154-
run: |
155-
RETENTION_DAYS=${{ inputs.retention-days || '30' }}
156-
DEPRECATION_TIME=$(date -u -d "+${RETENTION_DAYS} days" +%Y-%m-%dT%H:%M:%SZ)
157-
aws ec2 enable-image-deprecation \
158-
--image-id "${{ steps.build.outputs.ami_id }}" \
159-
--deprecate-at "$DEPRECATION_TIME"
160-
echo "AMI will be deprecated at $DEPRECATION_TIME"
110+
arch: arm64
111+
ami-prefix: vortex-ci
112+
retention-days: ${{ inputs.retention-days || '30' }}
161113

162114
- name: Summary
163115
run: |
164116
echo "## AMI Build Complete (arm64)" >> $GITHUB_STEP_SUMMARY
165117
echo "" >> $GITHUB_STEP_SUMMARY
166-
echo "- **AMI ID:** ${{ steps.build.outputs.ami_id }}" >> $GITHUB_STEP_SUMMARY
118+
echo "- **AMI ID:** ${{ steps.build-ami.outputs.ami-id }}" >> $GITHUB_STEP_SUMMARY
119+
echo "- **AMI Name:** ${{ steps.build-ami.outputs.ami-name }}" >> $GITHUB_STEP_SUMMARY
167120
echo "- **Deprecation:** ${{ inputs.retention-days || '30' }} days" >> $GITHUB_STEP_SUMMARY
168121
169122
# Test the newly built AMI

0 commit comments

Comments
 (0)