Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
Make stop action safer (#6)
Browse files Browse the repository at this point in the history
This might help in situations where the start job is cancelled, making the stop job more likely to successfully terminate the instance
  • Loading branch information
jpalomaki authored Sep 5, 2021
1 parent b43beb4 commit 1deeec2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ jobs:
- run: echo '${{ matrix.message }}'

stop-runner:
if: always()
needs: [start-runner, complex-build]
if: always() && needs.start-runner.result == 'success'
runs-on: ubuntu-20.04
steps:
- name: Stop runner
Expand Down
10 changes: 5 additions & 5 deletions start/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ outputs:
description: GitHub repository runner id
value: ${{ steps.main.outputs.runner-id }}
instance-id:
description: EC2 instance id
description: AWS EC2 instance id
value: ${{ steps.main.outputs.instance-id }}

runs:
Expand Down Expand Up @@ -78,6 +78,8 @@ runs:
instance_id="$(aws ec2 run-instances --launch-template "$LAUNCH_TEMPLATE" --user-data "$user_data" | jq -r .Instances[0].InstanceId)"
echo "::set-output name=instance-id::$instance_id"
echo "Started EC2 instance: $instance_id"
echo "Waiting for repository runner to be registered ..."
Expand All @@ -97,11 +99,9 @@ runs:
aws ec2 terminate-instances --instance-ids "$instance_id"
exit 1
else
echo "Repository runner registered with id: $runner_id"
echo "::set-output name=runner-id::$runner_id"
echo "Repository runner started (ID: $runner_id)"
fi
echo "::set-output name=runner-id::$runner_id"
echo "::set-output name=instance-id::$instance_id"
env:
GH_TOKEN: ${{ inputs.github-token }}
AWS_DEFAULT_REGION: ${{ inputs.aws-region }}
Expand Down
26 changes: 17 additions & 9 deletions stop/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,24 @@ runs:
- id: main
shell: bash
run: |
echo "Deregistering repository runner: $RUNNER_ID ..."
# Offline runners are automatically deleted after 30 days, so we emit a warning for runner deregistration failure
if [ -z "$RUNNER_ID" ]; then
echo "WARN: Unable to deregister GitHub repository runner (runner ID not available)"
else
echo "Deregistering GitHub repository runner: $RUNNER_ID ..."
gh api -X DELETE -H 'Accept: application/vnd.github.v3+json' \
"repos/$GITHUB_REPO/actions/runners/$RUNNER_ID" || echo "WARN: Failed to deregister GitHub repository runner (GitHub API call failed)"
fi
gh api -X DELETE -H 'Accept: application/vnd.github.v3+json' \
"repos/$GITHUB_REPO/actions/runners/$RUNNER_ID" || echo "WARN: Failed to deregister runner"
echo "Terminating EC2 instance: $INSTANCE_ID ..."
aws ec2 terminate-instances --instance-ids "$INSTANCE_ID" > /dev/null
echo "Repository runner stopped"
# Failure to terminate the EC2 instance will incur unnecessary costs, so we fail the job to ensure the user notices
if [ -z "$INSTANCE_ID" ]; then
echo "ERROR: Unable to terminate EC2 instance (instance ID not available)"
exit 1
else
echo "Terminating EC2 instance: $INSTANCE_ID ..."
aws ec2 terminate-instances --instance-ids "$INSTANCE_ID" > /dev/null
echo "Repository runner stopped"
fi
env:
GH_TOKEN: ${{ inputs.github-token }}
AWS_DEFAULT_REGION: ${{ inputs.aws-region }}
Expand Down

0 comments on commit 1deeec2

Please sign in to comment.