1
+ name : " Terraform Infrastructure Change Management Pipeline with GitHub Actions"
2
+
3
+ on :
4
+ push :
5
+ branches :
6
+ - main
7
+ - develop
8
+ paths :
9
+ - terraform/**
10
+ pull_request :
11
+ branches :
12
+ - main
13
+ - develop
14
+ paths :
15
+ - terraform/**
16
+
17
+ env :
18
+ # verbosity setting for Terraform logs
19
+ TF_LOG : INFO
20
+ # # Credentials for deployment to AWS
21
+ # AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
22
+ # AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
23
+ # # S3 bucket for the Terraform state
24
+ # BUCKET_TF_STATE: ${{ secrets.BUCKET_TF_STATE}}
25
+
26
+ jobs :
27
+ terraform :
28
+ name : " Terraform Infrastructure Change Management"
29
+ runs-on : ubuntu-latest
30
+ defaults :
31
+ run :
32
+ shell : bash
33
+ # We keep Terraform files in the terraform directory.
34
+ working-directory : ./terraform
35
+
36
+ steps :
37
+ - name : Checkout the repository to the runner
38
+ uses : actions/checkout@v2
39
+
40
+ - name : Setup Terraform with specified version on the runner
41
+ uses : hashicorp/setup-terraform@v2
42
+ with :
43
+ terraform_version : 1.3.0
44
+
45
+ - name : Terraform init [pull_request]
46
+ id : init-dev
47
+ if : github.event_name == 'pull_request'
48
+ with :
49
+ script : |
50
+ if [ ${{ github.event.pull_request.base.ref }} == "develop" ]; then
51
+ ENV = "dev";
52
+ elif [ ${{ github.event.pull_request.base.ref }} == "main" ]; then
53
+ ENV = "prod";
54
+ else
55
+ echo "unsupported environment";
56
+ fi
57
+ terraform init -upgrade -backend-config=backend/${ENV}.tfvars --reconfigure || exit 1
58
+
59
+ - name : Terraform format
60
+ id : fmt
61
+ run : terraform fmt -check
62
+
63
+ - name : Terraform validate
64
+ id : validate
65
+ run : terraform validate
66
+
67
+ - name : Terraform plan
68
+ id : plan
69
+ if : github.event_name == 'pull_request'
70
+ run : terraform plan -no-color -input=false
71
+ continue-on-error : true
72
+
73
+ - uses : actions/github-script@v6
74
+ if : github.event_name == 'pull_request'
75
+ env :
76
+ PLAN : " terraform\n ${{ steps.plan.outputs.stdout }}"
77
+ with :
78
+ script : |
79
+ const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\`
80
+ #### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
81
+ #### Terraform Validation 🤖\`${{ steps.validate.outcome }}\`
82
+ #### Terraform Plan 📖\`${{ steps.plan.outcome }}\`
83
+
84
+ <details><summary>Show Plan</summary>
85
+
86
+ \`\`\`\n
87
+ ${process.env.PLAN}
88
+ \`\`\`
89
+
90
+ </details>
91
+ *Pushed by: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;
92
+
93
+ github.rest.issues.createComment({
94
+ issue_number : context.issue.number,
95
+ owner : context.repo.owner,
96
+ repo : context.repo.repo,
97
+ body : output
98
+ })
99
+
100
+ - name : Terraform Plan Status
101
+ if : steps.plan.outcome == 'failure'
102
+ run : exit 1
103
+
104
+ - name : Terraform Apply
105
+ if : github.ref == 'refs/heads/main' && github.event_name == 'push'
106
+ run : terraform apply -auto-approve -input=false
0 commit comments