1+ name : " Terraform CICD - AWS ECS Instance"
2+
3+ on :
4+ pull_request :
5+ branches :
6+ - main
7+ push :
8+ branches :
9+ - main
10+
11+ permissions :
12+ contents : read
13+ issues : write
14+ pull-requests : write
15+
16+ env :
17+ # Verbosity setting for Terraform logs
18+ TF_LOG : ERROR
19+ # Credentials for deployment to AWS
20+ AWS_ACCESS_KEY_ID : ${{ secrets.AWS_ACCESS_KEY_ID }}
21+ AWS_SECRET_ACCESS_KEY : ${{ secrets.AWS_SECRET_ACCESS_KEY }}
22+ # S3 bucket for the Terraform state
23+ BUCKET_TF_STATE : ${{ secrets.BUCKET_TF_STATE}}
24+
25+ jobs :
26+ # Terraform Dev CICD
27+ terraform-dev :
28+ name : " Terraform Infra CICD Dev"
29+ runs-on : ubuntu-latest
30+ defaults :
31+ run :
32+ shell : bash
33+ working-directory : infra
34+ environment : dev
35+
36+ steps :
37+ - name : Checkout the repository to the runner
38+ uses : actions/checkout@v4
39+
40+ - name : Setup Terraform with specified version on the runner
41+ uses : hashicorp/setup-terraform@v3
42+ with :
43+ terraform_version : 1.11.3
44+
45+ - name : Terraform init dev
46+ id : init-dev
47+ run : terraform init -reconfigure -backend-config=bucket=$BUCKET_TF_STATE
48+
49+ # Quality checks DEV
50+ - name : Terraform format
51+ id : fmt-dev
52+ if : github.event_name == 'pull_request'
53+ run : terraform fmt -check
54+
55+ - name : Terraform validate
56+ id : validate-dev
57+ if : github.event_name == 'pull_request'
58+ run : terraform validate
59+
60+ - name : Terraform plan - dev
61+ id : plan-dev
62+ if : github.event_name == 'pull_request'
63+ run : terraform plan -var-file=envs/dev.tfvars -no-color -input=false
64+ continue-on-error : true
65+
66+ - uses : actions/github-script@v7
67+ if : github.event_name == 'pull_request'
68+ env :
69+ PLAN : " terraform\n ${{ steps.plan-dev.outputs.stdout }}"
70+ with :
71+ script : |
72+ const output = `#### Terraform Format and Style - DEV 🖌\`${{ steps.fmt-dev.outcome }}\`
73+ #### Terraform Initialization - DEV ⚙️\`${{ steps.init-dev.outcome }}\`
74+ #### Terraform Validation - DEV 🤖\`${{ steps.validate-dev.outcome }}\`
75+ #### Terraform Plan - DEV 📖\`${{ steps.plan-dev.outcome }}\`
76+
77+ <details><summary>Show Plan</summary>
78+
79+ \`\`\`\n
80+ ${process.env.PLAN}
81+ \`\`\`
82+
83+ </details>
84+ *Pushed by: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;
85+
86+ github.rest.issues.createComment({
87+ issue_number : context.issue.number,
88+ owner : context.repo.owner,
89+ repo : context.repo.repo,
90+ body : output
91+ })
92+
93+ - name : Terraform Plan Status
94+ if : steps.plan-dev.outcome == 'failure'
95+ run : exit 1
96+
97+ - name : Terraform Apply
98+ if : github.ref == 'refs/heads/main' && github.event_name == 'push'
99+ run : terraform apply -auto-approve -input=false
100+
101+ # # Terraform Prod CI
102+ # terraform-prod-ci:
103+ # name: "Terraform Infra CI Prod"
104+ # runs-on: ubuntu-latest
105+ # defaults:
106+ # run:
107+ # shell: bash
108+ # environment: prod
109+
110+ # steps:
111+ # - name: Checkout the repository to the runner
112+ # uses: actions/checkout@v4
113+
114+ # - name: Setup Terraform with specified version on the runner
115+ # uses: hashicorp/setup-terraform@v3
116+ # with:
117+ # terraform_version: 1.11.3
118+
119+ # - name: Terraform init prod
120+ # id: init-prod
121+ # run: terraform init -reconfigure -backend-config=bucket=$BUCKET_TF_STATE #Create new bucket for prod
122+
123+ # # Quality checks PROD
124+ # - name: Terraform format
125+ # id: fmt-prod
126+ # if: github.event_name == 'pull_request'
127+ # run: terraform fmt -check
128+
129+ # - name: Terraform validate
130+ # id: validate-prod
131+ # if: github.event_name == 'pull_request'
132+ # run: terraform validate
133+
134+ # - name: Terraform plan - prod
135+ # id: plan-prod
136+ # if: github.event_name == 'pull_request'
137+ # run: terraform plan -var-file=envs/prod.tfvars -no-color -input=false
138+ # continue-on-error: true
139+
140+ # - uses: actions/github-script@v6
141+ # if: github.event_name == 'pull_request'
142+ # env:
143+ # PLAN: "terraform\n${{ steps.plan-prod.outputs.stdout }}"
144+ # with:
145+ # script: |
146+ # const output = `#### Terraform Format and Style - PROD 🖌\`${{ steps.fmt-prod.outcome }}\`
147+ # #### Terraform Initialization - PROD ⚙️\`${{ steps.init-prod.outcome }}\`
148+ # #### Terraform Validation - PROD 🤖\`${{ steps.validate-prod.outcome }}\`
149+ # #### Terraform Plan - PROD 📖\`${{ steps.plan-prod.outcome }}\`
150+
151+ # <details><summary>Show Plan</summary>
152+
153+ # \`\`\`\n
154+ # ${process.env.PLAN}
155+ # \`\`\`
156+
157+ # </details>
158+ # *Pushed by: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;
159+
160+ # github.rest.issues.createComment({
161+ # issue_number: context.issue.number,
162+ # owner: context.repo.owner,
163+ # repo: context.repo.repo,
164+ # body: output
165+ # })
166+
167+ # - name: Terraform Plan Status
168+ # if: steps.plan-prod.outcome == 'failure'
169+ # run: exit 1
170+
171+ # # Terraform PROD CD
172+ # terraform-prod-cd:
173+ # name: "Terraform Infra CD Prod"
174+ # needs: [terraform-dev, terraform-prod-ci]
175+ # runs-on: ubuntu-latest
176+ # defaults:
177+ # run:
178+ # shell: bash
179+ # environment: prod
180+
181+ # steps:
182+ # - name: Checkout the repository to the runner
183+ # uses: actions/checkout@v3
184+
185+ # - name: Setup Terraform with specified version on the runner
186+ # uses: hashicorp/setup-terraform@v2
187+ # with:
188+ # terraform_version: 1.3.9
189+
190+ # - name: Terraform init prod
191+ # id: init-prod
192+ # run: terraform init -reconfigure -backend-config=bucket=$BUCKET_TF_STATE
193+
194+ # - name: Terraform Apply
195+ # if: github.ref == 'refs/heads/main' && github.event_name == 'push' # only on push/merge to main
196+ # run: terraform apply -auto-approve -input=false
0 commit comments