-
Notifications
You must be signed in to change notification settings - Fork 1
91 lines (78 loc) · 3.08 KB
/
development-pipeline-deletion-event.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# This pipeline create a new pipeline in AWS CodePipeline if the branch follows the desired pattern defined in BRANCH_PATTERN.
name: "Delete branch and delete AWS CodePipeline"
on:
delete:
branches:
- 'openwrt-2*'
env:
BRANCH_PATTERN: openwrt-2[0-9]{1}\.[0-9]{2}$
TERRAFORM_VERSION: 1.7.4
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
jobs:
branch_created:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v4
- name: Get branch name
run: |
echo "BRANCH_NAME=${{ github.event.ref }}" >> $GITHUB_ENV
- name: Verify branch name
run: |
if [[ ! "${BRANCH_NAME}" =~ $BRANCH_PATTERN ]]; then
echo "Branch name doesn't match the pattern."
echo "VALID_BRANCH=false" >> $GITHUB_ENV
else
echo "New branch created $GITHUB_REF"
echo "VALID_BRANCH=true" >> $GITHUB_ENV
fi
- name: Set up Terraform
if: env.VALID_BRANCH == 'true'
uses: hashicorp/setup-terraform@v3
with:
terraform_version: $TERRAFORM_VERSION
- name: Terraform Init
if: env.VALID_BRANCH == 'true'
run: |
cd .terraform/pipeline
terraform init
- name: Terraform Select Workspace
if: env.VALID_BRANCH == 'true'
run: |
cd .terraform/pipeline
terraform workspace select $BRANCH_NAME
echo "Current Workspace is $(terraform workspace show)"
sed -i "/stage_vars = {/a \ \ \"\${{ env.BRANCH_NAME }}\" = {\n \ \ \ branch = \"\${{ env.BRANCH_NAME }}\"\n \ \ }" terraform.tfvars
- name: Check Terraform Workspace
if: env.VALID_BRANCH == 'true'
run: |
cd .terraform/pipeline
if [ -z "$(terraform state list)" ]; then
echo "TF_IS_EMPTY=true" >> $GITHUB_ENV
echo "Workspace Terraform is empty"
else
echo "TF_IS_EMPTY=false" >> $GITHUB_ENV
echo "Workspace Terraform is not empty"
fi
- name: Terraform Update Bucket Force Deletion
if: env.VALID_BRANCH == 'true' && env.TF_IS_EMPTY == 'false'
run: |
cd .terraform/pipeline
sed -i '/^resource "aws_s3_bucket" "codepipeline_bucket" {/a force_destroy = true' main.tf
terraform apply -auto-approve -var="buildspec_file_name=development-buildspec.yml"
- name: Terraform Destroy && env.TF_IS_EMPTY == 'false'
if: env.VALID_BRANCH == 'true'
run: |
cd .terraform/pipeline
terraform destroy -auto-approve
- name: Terraform Delete Workspace
if: env.VALID_BRANCH == 'true'
run: |
cd .terraform/pipeline
terraform workspace select default
terraform workspace delete $BRANCH_NAME
echo "Workspace $BRANCH_NAME deleted"
- name: NOTHING TO DO
if: env.VALID_BRANCH == 'false'
run: echo "THIS BRANCH DOES NOT CREATE A NEW AWS CODEPIPELINE"