Skip to content

chore: terminate packer instance on workflow cancellation #512

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions .github/workflows/ami-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ jobs:
- name: Build AMI
run: |
GIT_SHA=$(git rev-parse HEAD)
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
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

- name: Slack Notification
- name: Slack Notification on Failure
if: ${{ failure() }}
uses: rtCamp/action-slack-notify@v2
env:
Expand All @@ -29,3 +29,8 @@ jobs:
SLACK_COLOR: 'danger'
SLACK_MESSAGE: 'Building Postgres AMI failed'
SLACK_FOOTER: ''

- name: Cleanup resources on build cancellation
if: ${{ cancelled() }}
run: |
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 {}
10 changes: 8 additions & 2 deletions amazon-arm64.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ variable "git-head-version" {
default = "unknown"
}

variable "packer-execution-id" {
type = string
default = "unknown"
}

# source block
source "amazon-ebssurrogate" "source" {
profile = "${var.profile}"
Expand Down Expand Up @@ -129,8 +134,9 @@ source "amazon-ebssurrogate" "source" {
}

run_tags = {
creator = "packer"
appType = "postgres"
creator = "packer"
appType = "postgres"
packerExecutionId = "${var.packer-execution-id}"
}
run_volume_tags = {
creator = "packer"
Expand Down