Skip to content

Commit

Permalink
Delete stacks job
Browse files Browse the repository at this point in the history
  • Loading branch information
ViveakV committed Jun 7, 2024
1 parent 519217d commit 7cad9cf
Show file tree
Hide file tree
Showing 3 changed files with 361 additions and 81 deletions.
114 changes: 114 additions & 0 deletions .github/workflows/delete-stacks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
name: Delete CloudFormation Stacks

on:
# Triggers the workflow on push or pull request events but only for the "main" or "develop" branch
pull_request:
branches:
- main
types:
- opened
- reopened
- ready_for_review
- synchronize
paths-ignore:
- 'infra-l2-dynamo/**'
- 'infra-l2-kms/**'
- 'infra-l2-outbound-proxy/**'
- 'bav-ipv-stub/**'
- 'test-harness/**'

workflow_dispatch:
inputs:
stacks:
description: 'Comma-separated list of CloudFormation stack names'
required: true
type: string

env: # Only adding the variables in that are required for
AWS_REGION: eu-west-2

jobs:
delete-stacks:
runs-on: ubuntu-latest

env:
AWS_REGION: eu-west-2

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Setup SAM CLI
uses: aws-actions/setup-sam@v2
with:
use-installer: true

- name: Assume temporary AWS role
uses: aws-actions/configure-aws-credentials@v1-node16
with:
role-to-assume: ${{ secrets.CRI_BAV_GH_VALIDATE_ROLE_ARN }}
aws-region: ${{ env.AWS_REGION }}

- name: Ensure stack names are provided
run: |
if [ -z "${{ github.event.inputs.stacks }}" ]; then
echo "Error: No stack names provided."
exit 1
fi
- name: Delete CloudFormation stacks
run: |
STACK_NAMES=${{ github.event.inputs.stacks }}
delete_stack() {
local STACK_NAME=$1
local LOG_FILE="delete_${STACK_NAME}.log"
echo "Processing stack: $STACK_NAME" | tee -a $LOG_FILE
# Query CloudFormation stack for all resources
echo "Querying CloudFormation stack for resources..." | tee -a $LOG_FILE
RESOURCES=$(aws cloudformation describe-stack-resources --stack-name $STACK_NAME --query "StackResources[?ResourceType=='AWS::S3::Bucket'].PhysicalResourceId" --output text)
# Check if there are any S3 buckets
if [ -z "$RESOURCES" ]; then
echo "No S3 buckets found in the stack." | tee -a $LOG_FILE
else
# Loop through each S3 bucket and delete it
for BUCKET in $RESOURCES; do
echo "Deleting S3 bucket: $BUCKET" | tee -a $LOG_FILE
./deleteBucket.sh $BUCKET | tee -a $LOG_FILE
if [ $? -ne 0 ]; then
echo "Failed to delete bucket: $BUCKET" | tee -a $LOG_FILE
return 1
fi
echo "Successfully deleted bucket: $BUCKET" | tee -a $LOG_FILE
done
fi
# Delete the CloudFormation stack
echo "Deleting CloudFormation stack: $STACK_NAME" | tee -a $LOG_FILE
aws cloudformation delete-stack --stack-name $STACK_NAME | tee -a $LOG_FILE
# Wait for the stack to be deleted
echo "Waiting for stack to be deleted..." | tee -a $LOG_FILE
aws cloudformation wait stack-delete-complete --stack-name $STACK_NAME | tee -a $LOG_FILE
if [ $? -eq 0 ]; then
echo "Successfully deleted CloudFormation stack: $STACK_NAME" | tee -a $LOG_FILE
else
echo "Failed to delete CloudFormation stack: $STACK_NAME" | tee -a $LOG_FILE
return 1
fi
}
export -f delete_stack
echo "Starting deletion of CloudFormation stacks in parallel..."
echo $STACK_NAMES | xargs -n 1 -P 0 bash -c 'delete_stack "$@"' _
if [ $? -eq 0 ]; then
echo "Successfully deleted all specified CloudFormation stacks."
else
echo "Failed to delete one or more CloudFormation stacks."
exit 1
fi
251 changes: 170 additions & 81 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ on:
- 'bav-ipv-stub/**'
- 'test-harness/**'

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
inputs:
stacks:
description: 'Comma-separated list of CloudFormation stack names'
required: true
type: string

# Required for sam-validate step
permissions:
Expand All @@ -38,76 +42,15 @@ env: # Only adding the variables in that are required for
PACT_PROVIDER_VERSION: ${{ secrets.PACT_PROVIDER_VERSION }}

jobs:
run-code-check:
name: Code Checks
delete-stacks:
runs-on: ubuntu-latest
defaults:
run:
shell: bash
working-directory: ./src/
steps:
- name: Check out repository code
uses: actions/checkout@v4
with:
submodules: true

- name: Checks if commiting secrets to repo
uses: pre-commit/action@v3.0.1
with:
extra_args: "detect-secrets --all-files"

- name: Setup nodeJS v18
uses: actions/setup-node@v4
with:
node-version: 18

- name: Set offline mirror path
id: offline-mirror-path
run: echo "mirror-dir=${GITHUB_WORKSPACE}/npm-packages-offline-cache" >> $GITHUB_OUTPUT
env:
AWS_REGION: eu-west-2

- name: Cache npm offline-mirror
uses: actions/cache@v4
with:
path: ${{ steps.offline-mirror-path.outputs.mirror-dir }}
key: offline-mirror-oauth

- name: Install dependencies
run: npm install

- name: Run lint
run: npm run lint

- name: Archive lint results
if: always()
uses: actions/upload-artifact@v4
with:
name: lint-report
path: src/reports/eslint/reportFile.html

- name: Run Tests
run: npm run test:unit

- name: Run Infra checks
run: npm run test:infra

- name: Run Contract tests
run: npm run test:contract:ci

- name: Kill DynamoDB
run: npm run kill:dynamodblocal

sam-validate:
name: SAM Validate
runs-on: ubuntu-latest
defaults:
run:
shell: bash
working-directory: ./deploy/
steps:
- name: Check out repository code
uses: actions/checkout@v4
with:
submodules: true
- name: Checkout repository
uses: actions/checkout@v2

- name: Setup SAM CLI
uses: aws-actions/setup-sam@v2
Expand All @@ -120,17 +63,163 @@ jobs:
role-to-assume: ${{ secrets.CRI_BAV_GH_VALIDATE_ROLE_ARN }}
aws-region: ${{ env.AWS_REGION }}

- name: Cache SAM builds
uses: actions/cache@v4
with:
path: |
./di-ipv-cri-bav-api/.aws-sam/cache
./di-ipv-cri-bav-api/.aws-sam/deps
./di-ipv-cri-bav-api/.aws-sam/build.toml
key: sam-oauth

- name: SAM Validate
run: sam validate --region ${{ env.AWS_REGION }} -t template.yaml

- name: SAM Build
run: sam build -t template.yaml
- name: Ensure stack names are provided
run: |
if [ -z "${{ github.event.inputs.stacks }}" ]; then
echo "Error: No stack names provided."
exit 1
fi
- name: Delete CloudFormation stacks
run: |
STACK_NAMES=${{ github.event.inputs.stacks }}
delete_stack() {
local STACK_NAME=$1
local LOG_FILE="delete_${STACK_NAME}.log"
echo "Processing stack: $STACK_NAME" | tee -a $LOG_FILE
# Query CloudFormation stack for all resources
echo "Querying CloudFormation stack for resources..." | tee -a $LOG_FILE
RESOURCES=$(aws cloudformation describe-stack-resources --stack-name $STACK_NAME --query "StackResources[?ResourceType=='AWS::S3::Bucket'].PhysicalResourceId" --output text)
# Check if there are any S3 buckets
if [ -z "$RESOURCES" ]; then
echo "No S3 buckets found in the stack." | tee -a $LOG_FILE
else
# Loop through each S3 bucket and delete it
for BUCKET in $RESOURCES; do
echo "Deleting S3 bucket: $BUCKET" | tee -a $LOG_FILE
./deleteBucket.sh $BUCKET | tee -a $LOG_FILE
if [ $? -ne 0 ]; then
echo "Failed to delete bucket: $BUCKET" | tee -a $LOG_FILE
return 1
fi
echo "Successfully deleted bucket: $BUCKET" | tee -a $LOG_FILE
done
fi
# Delete the CloudFormation stack
echo "Deleting CloudFormation stack: $STACK_NAME" | tee -a $LOG_FILE
aws cloudformation delete-stack --stack-name $STACK_NAME | tee -a $LOG_FILE
# Wait for the stack to be deleted
echo "Waiting for stack to be deleted..." | tee -a $LOG_FILE
aws cloudformation wait stack-delete-complete --stack-name $STACK_NAME | tee -a $LOG_FILE
if [ $? -eq 0 ]; then
echo "Successfully deleted CloudFormation stack: $STACK_NAME" | tee -a $LOG_FILE
else
echo "Failed to delete CloudFormation stack: $STACK_NAME" | tee -a $LOG_FILE
return 1
fi
}
export -f delete_stack
echo "Starting deletion of CloudFormation stacks in parallel..."
echo $STACK_NAMES | xargs -n 1 -P 0 bash -c 'delete_stack "$@"' _
if [ $? -eq 0 ]; then
echo "Successfully deleted all specified CloudFormation stacks."
else
echo "Failed to delete one or more CloudFormation stacks."
exit 1
fi
# run-code-check:
# name: Code Checks
# runs-on: ubuntu-latest
# defaults:
# run:
# shell: bash
# working-directory: ./src/
# steps:
# - name: Check out repository code
# uses: actions/checkout@v4
# with:
# submodules: true

# - name: Checks if commiting secrets to repo
# uses: pre-commit/action@v3.0.1
# with:
# extra_args: "detect-secrets --all-files"

# - name: Setup nodeJS v18
# uses: actions/setup-node@v4
# with:
# node-version: 18

# - name: Set offline mirror path
# id: offline-mirror-path
# run: echo "mirror-dir=${GITHUB_WORKSPACE}/npm-packages-offline-cache" >> $GITHUB_OUTPUT

# - name: Cache npm offline-mirror
# uses: actions/cache@v4
# with:
# path: ${{ steps.offline-mirror-path.outputs.mirror-dir }}
# key: offline-mirror-oauth

# - name: Install dependencies
# run: npm install

# - name: Run lint
# run: npm run lint

# - name: Archive lint results
# if: always()
# uses: actions/upload-artifact@v4
# with:
# name: lint-report
# path: src/reports/eslint/reportFile.html

# - name: Run Tests
# run: npm run test:unit

# - name: Run Infra checks
# run: npm run test:infra

# - name: Run Contract tests
# run: npm run test:contract:ci

# - name: Kill DynamoDB
# run: npm run kill:dynamodblocal

# sam-validate:
# name: SAM Validate
# runs-on: ubuntu-latest
# defaults:
# run:
# shell: bash
# working-directory: ./deploy/
# steps:
# - name: Check out repository code
# uses: actions/checkout@v4
# with:
# submodules: true

# - name: Setup SAM CLI
# uses: aws-actions/setup-sam@v2
# with:
# use-installer: true

# - name: Assume temporary AWS role
# uses: aws-actions/configure-aws-credentials@v1-node16
# with:
# role-to-assume: ${{ secrets.CRI_BAV_GH_VALIDATE_ROLE_ARN }}
# aws-region: ${{ env.AWS_REGION }}

# - name: Cache SAM builds
# uses: actions/cache@v4
# with:
# path: |
# ./di-ipv-cri-bav-api/.aws-sam/cache
# ./di-ipv-cri-bav-api/.aws-sam/deps
# ./di-ipv-cri-bav-api/.aws-sam/build.toml
# key: sam-oauth

# - name: SAM Validate
# run: sam validate --region ${{ env.AWS_REGION }} -t template.yaml

# - name: SAM Build
# run: sam build -t template.yaml
Loading

0 comments on commit 7cad9cf

Please sign in to comment.