Skip to content

Commit eb493c5

Browse files
committed
chore: terminate builder instance on job cancellation
1 parent c2c1b30 commit eb493c5

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

.github/workflows/ami-release.yml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- name: Build AMI
1919
run: |
2020
GIT_SHA=$(git rev-parse HEAD)
21-
packer build -var "git-head-version=${GIT_SHA}" -var-file="development-arm.vars.pkr.hcl" -var-file="common.vars.pkr.hcl" amazon-arm64.pkr.hcl
21+
packer build -var "git-head-version=${GIT_SHA}" -var "packer-execution-id=${GITHUB_RUN_ID}" -var-file="development-arm.vars.pkr.hcl" -var-file="common.vars.pkr.hcl" amazon-arm64.pkr.hcl
2222
2323
- name: Slack Notification on Failure
2424
if: ${{ failure() }}
@@ -30,12 +30,7 @@ jobs:
3030
SLACK_MESSAGE: 'Building Postgres AMI failed'
3131
SLACK_FOOTER: ''
3232

33-
- name: Slack Notification on Cancel
33+
- name: Cleanup resources on build cancellation
3434
if: ${{ cancelled() }}
35-
uses: rtCamp/action-slack-notify@v2
36-
env:
37-
SLACK_WEBHOOK: ${{ secrets.SLACK_NOTIFICATIONS_WEBHOOK }}
38-
SLACK_USERNAME: 'gha-failures-notifier'
39-
SLACK_COLOR: 'danger'
40-
SLACK_MESSAGE: 'Postgres AMI build cancelled. Reminder to clean up any Packer instances that may have been left running. @group-infra'
41-
SLACK_FOOTER: ''
35+
run: |
36+
aws ec2 describe-instances --filters "Name=tag:packerExecutionId,Values=${GITHUB_RUN_ID}" --query "Reservations[].Instances[].InstanceId" --output text | xargs -I {} aws ec2 terminate-instances --instance-ids {}

amazon-arm64.pkr.hcl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ variable "git-head-version" {
8282
default = "unknown"
8383
}
8484

85+
variable "packer-execution-id" {
86+
type = string
87+
default = "unknown"
88+
}
89+
8590
# source block
8691
source "amazon-ebssurrogate" "source" {
8792
profile = "${var.profile}"
@@ -129,8 +134,9 @@ source "amazon-ebssurrogate" "source" {
129134
}
130135

131136
run_tags = {
132-
creator = "packer"
133-
appType = "postgres"
137+
creator = "packer"
138+
appType = "postgres"
139+
packerExecutionId = "${var.packer-execution-id}"
134140
}
135141
run_volume_tags = {
136142
creator = "packer"

0 commit comments

Comments
 (0)