Skip to content

Commit fc95004

Browse files
committed
Implement build-and-test Github Actions CI using AWS spot instances
1 parent 2ba5d7e commit fc95004

File tree

4 files changed

+186
-0
lines changed

4 files changed

+186
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: Build And Test (x86-64)
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
build-and-test-x86-64:
7+
uses: ./.github/workflows/build-and-test.yml
8+
with:
9+
architecture: x86_64
10+
instance_type: c5.9xlarge
11+
secrets: inherit

.github/workflows/build-and-test.yml

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
name: Build And Test
2+
3+
# Based on
4+
# https://blog.devgenius.io/create-register-and-terminate-ec2-spot-instances-via-github-actions-aef473b8a00f
5+
6+
on:
7+
workflow_call:
8+
inputs:
9+
architecture:
10+
required: true
11+
type: string
12+
instance_type:
13+
required: true
14+
type: string
15+
secrets:
16+
ACTIONS_TOKEN:
17+
required: true
18+
19+
env:
20+
EC2_SECURITY_GROUP_ID: sg-072c6deccd49e89c8 # rr-testing
21+
AWS_ROLE_ARN: arn:aws:iam::019859450731:role/SpotInstanceManager
22+
AWS_REGION: us-east-2
23+
RUNNER_VERSION: "2.316.1"
24+
25+
jobs:
26+
start-runner:
27+
name: Start Runner
28+
permissions: write-all
29+
runs-on: ubuntu-latest
30+
outputs:
31+
EC2_HOST: ${{ env.EC2_HOST }}
32+
EC2_INSTANCE_ID: ${{ env.EC2_INSTANCE_ID }}
33+
steps:
34+
- name: "Run :: Configure AWS credentials"
35+
uses: aws-actions/configure-aws-credentials@v4
36+
with:
37+
role-to-assume: ${{ env.AWS_ROLE_ARN }}
38+
role-session-name: pipeline-assume-role-session
39+
aws-region: ${{ env.AWS_REGION }}
40+
41+
- name: "Run :: Create and register AWS spot instance"
42+
run: |2-
43+
# Get GitHub Actions runner registration token
44+
response=$(curl -s -X POST -H 'Accept: application/vnd.github+json' -H 'Authorization: Bearer ${{ secrets.ACTIONS_TOKEN }}' -H 'X-GitHub-Api-Version: 2022-11-28' https://api.github.com/orgs/rr-debugger/actions/runners/registration-token)
45+
echo "response :: $response"
46+
RUNNER_REG_TOKEN=$(echo "$response" | jq -r .token)
47+
echo "RUNNER_REG_TOKEN :: $RUNNER_REG_TOKEN"
48+
49+
if [ $RUNNER_REG_TOKEN != "null" ]; then
50+
USER_DATA="#!/bin/bash
51+
52+
# Make sure the VM doesn't run for more than an hour
53+
shutdown +60
54+
55+
apt-get update -y
56+
apt-get dist-upgrade -f -y
57+
58+
sudo -u ubuntu --login <<EOF
59+
# Install GitHub Actions runner
60+
mkdir /home/ubuntu/actions-runner && cd /home/ubuntu/actions-runner
61+
curl -o actions-runner-linux-x64-${RUNNER_VERSION}.tar.gz -L https://github.com/actions/runner/releases/download/v${RUNNER_VERSION}/actions-runner-linux-x64-${RUNNER_VERSION}.tar.gz
62+
echo \"Github Runner Installed\"
63+
# Extract the installer
64+
tar xzf ./actions-runner-linux-x64-${RUNNER_VERSION}.tar.gz
65+
echo \"Github Runner Installer Extracted\"
66+
67+
# Run GitHub Actions runner configuration
68+
yes '' | ./config.sh --url https://github.com/${GITHUB_REPOSITORY} --token $RUNNER_REG_TOKEN --labels ${{ inputs.architecture }}_${{github.sha}}
69+
echo \"Github Runner Configured\"
70+
71+
# Run GitHub Actions runner
72+
yes '' | ./run.sh
73+
echo \"Github Runner UP!\"
74+
EOF
75+
"
76+
77+
IMAGE_ID=$(aws ec2 describe-images --owners 099720109477 --filters 'Name=architecture,Values=${{ inputs.architecture }}' 'Name=name,Values=ubuntu/images/hvm-ssd-gp3/ubuntu-noble-24.04-*' | jq --raw-output .Images[0].ImageId)
78+
echo "IMAGE_ID :: $IMAGE_ID"
79+
80+
ENCODED_USER_DATA=$(echo -n "$USER_DATA" | sed 's/^[[:space:]]*//' | base64 -w 0)
81+
echo "ENCODED_USER_DATA :: $ENCODED_USER_DATA"
82+
83+
INSTANCE_ID=$(aws ec2 run-instances --image-id $IMAGE_ID --instance-type ${{ inputs.instance_type }} --user-data $ENCODED_USER_DATA --security-group-ids ${{ env.EC2_SECURITY_GROUP_ID }} --instance-market-options MarketType=spot --instance-initiated-shutdown-behavior terminate --query 'Instances[0].InstanceId' --key-name rr-testing --output text)
84+
echo "INSTANCE_ID :: $INSTANCE_ID"
85+
aws ec2 wait instance-running --instance-ids $INSTANCE_ID
86+
87+
HOSTNAME=$(aws ec2 describe-instances --instance-ids $INSTANCE_ID --query 'Reservations[0].Instances[0].PublicDnsName' --output text)
88+
echo "HOSTNAME :: $HOSTNAME"
89+
90+
echo "EC2_HOST=$HOSTNAME" >> $GITHUB_ENV
91+
echo "EC2_INSTANCE_ID=$INSTANCE_ID" >> $GITHUB_ENV
92+
else
93+
echo "$response"
94+
exit 1
95+
fi
96+
97+
build-and-test:
98+
name: Build And Test
99+
permissions:
100+
contents: read
101+
runs-on:
102+
labels: ${{ inputs.architecture }}_${{github.sha}}
103+
needs:
104+
- start-runner
105+
steps:
106+
- name: Start
107+
run: |2-
108+
echo " Starting GitHub Action!" &&
109+
echo "STEPS_CAN_PROCEED=true" >> $GITHUB_ENV
110+
111+
- name: "Run :: Checkout repository"
112+
uses: actions/checkout@v2
113+
114+
- name: "Run :: Build"
115+
run: ./scripts/github-actions-build.sh
116+
117+
- name: "Run :: Test"
118+
run: ./scripts/github-actions-test.sh
119+
120+
stop-runner:
121+
name: Stop Runner
122+
permissions: write-all
123+
runs-on: ubuntu-latest
124+
needs:
125+
- start-runner
126+
- build-and-test
127+
if: ${{ always() }}
128+
steps:
129+
- name: "Run :: Configure AWS credentials"
130+
uses: aws-actions/configure-aws-credentials@v4
131+
with:
132+
role-to-assume: ${{ env.AWS_ROLE_ARN }}
133+
role-session-name: pipeline-assume-role-session
134+
aws-region: ${{ env.AWS_REGION }}
135+
136+
- name: "Run :: Terminate and unregister AWS spot instance"
137+
run: |2-
138+
aws ec2 terminate-instances --instance-ids ${{ needs.start-runner.outputs.EC2_INSTANCE_ID }}
139+
gh_runner_label=${{ needs.start-runner.outputs.EC2_RUNNER }}
140+
response=$(curl -X GET https://api.github.com/orgs/rr-debugger/actions/runners -H 'Authorization: Bearer ${{ secrets.ACTIONS_TOKEN }}'
141+
)
142+
offline_runners=$(echo "$response" | jq '.runners | map(select((.labels? | any(.name == "${{ inputs.architecture }}_${{github.sha}}"))))')
143+
echo "Offline Runners :: $offline_runners"
144+
145+
echo "Attempting to remove offline runners..."
146+
for runner in $(echo "$offline_runners" | jq -r '.[].id'); do
147+
echo "Triggering action for runner ID: $runner"
148+
curl -X DELETE https://api.github.com/orgs/rr-debugger/actions/runners/$runner -H 'Authorization: Bearer ${{ secrets.ACTIONS_TOKEN }}'
149+
done

scripts/github-actions-build.sh

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
set +x # echo commands
4+
set -e # default to exiting on error"
5+
6+
uname -a
7+
8+
sudo apt-get install -y rpm ccache cmake g++ pkg-config zlib1g-dev git python-dev-is-python3 libacl1-dev ninja-build manpages-dev capnproto libcapnp-dev gdb lldb python3-pexpect
9+
10+
mkdir obj
11+
cd obj
12+
cmake -G Ninja -DCMAKE_BUILD_TYPE=DEBUG -Dstaticlibs=FALSE ..
13+
ninja

scripts/github-actions-test.sh

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
set +x # echo commands
4+
set -e # default to exiting on error"
5+
6+
# Enable perf events for rr
7+
echo 0 | sudo tee /proc/sys/kernel/perf_event_paranoid
8+
# Enable ptrace-attach to any process. This lets us get more data when tests fail.
9+
echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
10+
# Disable AppArmor restrictions on user namespaces, which our tests need to use
11+
(echo 0 | sudo tee /proc/sys/kernel/apparmor_restrict_unprivileged_userns) || true
12+
let halfproc=`nproc`/2
13+
ctest -j$halfproc --verbose

0 commit comments

Comments
 (0)