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