Skip to content

Commit 1a20795

Browse files
authored
chore: terminate builder instance on job cancellation (#512)
1 parent 57bb5bc commit 1a20795

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

.github/workflows/ami-release.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ 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
23-
- name: Slack Notification
23+
- name: Slack Notification on Failure
2424
if: ${{ failure() }}
2525
uses: rtCamp/action-slack-notify@v2
2626
env:
@@ -29,3 +29,8 @@ jobs:
2929
SLACK_COLOR: 'danger'
3030
SLACK_MESSAGE: 'Building Postgres AMI failed'
3131
SLACK_FOOTER: ''
32+
33+
- name: Cleanup resources on build cancellation
34+
if: ${{ cancelled() }}
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)