Skip to content

Commit c9736ff

Browse files
authored
Merge pull request #12 from NgaNaNa/feature/github actions cicd for terraform
Feature/GitHub
2 parents 0183389 + 438132f commit c9736ff

File tree

4 files changed

+204
-28
lines changed

4 files changed

+204
-28
lines changed

.github/workflows/terraform.yaml

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
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

README.md

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,58 +17,38 @@ The VPC, public subnets, Internet Gateway, and Terraform remote-state bucket (S3
1717

1818
---
1919

20-
## Repo structure
21-
22-
```
23-
infra/
24-
├─ _backend.tf # remote state (S3 + DynamoDB)
25-
├─ _providers.tf # AWS provider / default tags
26-
├─ _variables.tf # all inputs
27-
├─ cluster.tf # ECS cluster (awsvpc)
28-
├─ asg_capacity.tf # ASG + capacity provider
29-
├─ task_definition.tf # image, ports, health check, logs
30-
├─ ecs_service.tf # service + load balancer attachment
31-
├─ cloudwatch_logs.tf
32-
└─ envs/
33-
├─ dev.tfvars
34-
└─ prod.tfvars
35-
```
36-
37-
---
38-
3920
## 1 · Initialise Terraform (one‑time per env)
4021

4122
```bash
4223
cd infra
43-
terraform init -backend-config="bucket=node-app-infra-tfstate-dev" -backend-config="profile=node-app-terraform-dev"
24+
terraform init -reconfigure -backend-config=bucket=node-app-infra-tfstate-dev -backend-config=profile=node-app-terraform-dev
4425
```
4526

4627
---
4728

48-
## 2 · Build & push the container image
29+
## 2 · Build & push the container image (Apply new version tag where appropriate)
4930

5031
```bash
51-
docker buildx create --name multi --use 2>/dev/null || true
52-
docker buildx build --platform linux/amd64 -t nrampling/demo-node-app:1.0.0 --push .
32+
docker buildx build --platform linux/amd64 -t nrampling/demo-node-app:1.0.2 --push .
5333
```
5434

5535
Update the image tag in `infra/envs/dev.tfvars`:
5636

5737
```hcl
58-
docker_image = "nrampling/demo-node-app:1.0.0"
38+
node_app_image = "nrampling/demo-node-app:1.0.2"
5939
```
6040

6141
---
6242

6343
## 3 · Deploy with Terraform
6444

6545
```bash
66-
AWS_PROFILE=node-app-terraform-dev terraform plan -var-file=envs/dev.tfvars
46+
AWS_PROFILE=node-app-terraform-dev terraform plan -var-file=envs/dev.tfvars
6747

6848
AWS_PROFILE=node-app-terraform-dev terraform apply -var-file=envs/dev.tfvars
6949
```
7050

71-
### Outputs (example)
51+
### Outputs (example only - plug in aws account)
7252

7353
```text
7454
alb_dns_name = dev-app-alb-123456.ap-southeast-2.elb.amazonaws.com

infra/envs/dev.tfvars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ alb_public_subnet_ids = ["subnet-055583b9b74d44b56", "subnet-0e9b56625d00f6c88",
66

77
vpc_id = "vpc-0fabd74c01d8c9d4a"
88

9-
node_app_image = "nrampling/demo-node-app:1.0.2"
9+
node_app_image = "nrampling/demo-node-app:1.0.3"

k8s/deployment.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ spec:
1414
spec:
1515
containers:
1616
- name: demo-node-app
17-
image: nrampling/demo-node-app:1.0.0
17+
image: nrampling/demo-node-app:1.0.3
1818
ports:
1919
- containerPort: 3000
2020
---

0 commit comments

Comments
 (0)