|
| 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 | + RUNNER_REG_TOKEN=$(echo "$response" | jq -r .token) |
| 46 | + |
| 47 | + if [ $RUNNER_REG_TOKEN != "null" ]; then |
| 48 | + USER_DATA="#!/bin/bash |
| 49 | + |
| 50 | + # Make sure the VM doesn't run for more than an hour |
| 51 | + shutdown +60 |
| 52 | +
|
| 53 | + apt-get update -y |
| 54 | + apt-get dist-upgrade -f -y |
| 55 | + |
| 56 | + # Install GitHub Actions runner |
| 57 | + mkdir /home/ubuntu/actions-runner && cd /home/ubuntu/actions-runner |
| 58 | + 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 |
| 59 | + echo \"Github Runner Installed\" |
| 60 | + # Extract the installer |
| 61 | + tar xzf ./actions-runner-linux-x64-${RUNNER_VERSION}.tar.gz |
| 62 | + echo \"Github Runner Installer Extracted\" |
| 63 | + |
| 64 | + # Run GitHub Actions runner configuration |
| 65 | + yes '' | ./config.sh --url https://github.com/${GITHUB_REPOSITORY} --token $RUNNER_REG_TOKEN --labels ${{ inputs.architecture }}_${{github.sha}} |
| 66 | + echo \"Github Runner Configured\" |
| 67 | +
|
| 68 | + # Run GitHub Actions runner |
| 69 | + yes '' | ./run.sh |
| 70 | + echo \"Github Runner UP!\" |
| 71 | + " |
| 72 | + |
| 73 | + 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) |
| 74 | + echo "IMAGE_ID :: $IMAGE_ID" |
| 75 | +
|
| 76 | + ENCODED_USER_DATA=$(echo -n "$USER_DATA" | sed 's/^[[:space:]]*//' | base64 -w 0) |
| 77 | + echo "ENCODED_USER_DATA :: $ENCODED_USER_DATA" |
| 78 | + |
| 79 | + 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' --output text) |
| 80 | + echo "INSTANCE_ID :: $INSTANCE_ID" |
| 81 | +
|
| 82 | + aws ec2 wait instance-running --instance_ids $INSTANCE_ID |
| 83 | + HOSTNAME=$(aws ec2 describe-instances --instance-ids $INSTANCE_ID --query 'Reservations[0].Instances[0].PublicDnsName' --output text) |
| 84 | + echo "HOSTNAME :: $HOSTNAME" |
| 85 | + |
| 86 | + echo "EC2_HOST=$HOSTNAME" >> $GITHUB_ENV |
| 87 | + echo "EC2_INSTANCE_ID=$INSTANCE_ID" >> $GITHUB_ENV |
| 88 | + else |
| 89 | + echo "$response" |
| 90 | + exit 1 |
| 91 | + fi |
| 92 | +
|
| 93 | + build-and-test: |
| 94 | + name: Build And Test |
| 95 | + permissions: |
| 96 | + contents: read |
| 97 | + runs-on: |
| 98 | + labels: ${{ inputs.architecture }}_${{github.sha}} |
| 99 | + needs: |
| 100 | + - start-runner |
| 101 | + steps: |
| 102 | + - name: Start |
| 103 | + run: |2- |
| 104 | + echo " Starting GitHub Action!" && |
| 105 | + echo "STEPS_CAN_PROCEED=true" >> $GITHUB_ENV |
| 106 | +
|
| 107 | + - name: "Run :: Checkout repository" |
| 108 | + uses: actions/checkout@v2 |
| 109 | + |
| 110 | + - name: "Run :: Build" |
| 111 | + run: ./scripts/github-actions-build.sh |
| 112 | + |
| 113 | + - name: "Run :: Test" |
| 114 | + run: ./scripts/github-actions-test.sh |
| 115 | + |
| 116 | + stop-runner: |
| 117 | + name: Stop Runner |
| 118 | + permissions: write-all |
| 119 | + runs-on: ubuntu-latest |
| 120 | + needs: |
| 121 | + - start-runner |
| 122 | + - build-and-test |
| 123 | + if: ${{ always() }} |
| 124 | + steps: |
| 125 | + - name: "Run :: Configure AWS credentials" |
| 126 | + uses: aws-actions/configure-aws-credentials@v4 |
| 127 | + with: |
| 128 | + role-to-assume: ${{ env.AWS_ROLE_ARN }} |
| 129 | + role-session-name: pipeline-assume-role-session |
| 130 | + aws-region: ${{ env.AWS_REGION }} |
| 131 | + |
| 132 | + - name: "Run :: Terminate and unregister AWS spot instance" |
| 133 | + run: |2- |
| 134 | + aws ec2 terminate-instances --instance-ids ${{ needs.start-runner.outputs.EC2_INSTANCE_ID }} |
| 135 | + gh_runner_label=${{ needs.start-runner.outputs.EC2_RUNNER }} |
| 136 | + response=$(curl -X GET https://api.github.com/orgs/rr-debugger/actions/runners -H 'Authorization: Bearer ${{ secrets.ACTIONS_TOKEN }}' |
| 137 | + ) |
| 138 | + offline_runners=$(echo "$response" | jq '.runners | map(select((.labels? | any(.name == "${{ inputs.architecture }}_${{github.sha}}"))))') |
| 139 | + echo "Offline Runners :: $offline_runners" |
| 140 | + |
| 141 | + echo "Attempting to remove offline runners..." |
| 142 | + for runner in $(echo "$offline_runners" | jq -r '.[].id'); do |
| 143 | + echo "Triggering action for runner ID: $runner" |
| 144 | + curl -X DELETE https://api.github.com/orgs/rr-debugger/actions/runners/$runner -H 'Authorization: Bearer ${{ secrets.ACTIONS_TOKEN }}' |
| 145 | + done |
0 commit comments