Skip to content

Commit 2156679

Browse files
committed
feat: separate actions per env
1 parent 1abb541 commit 2156679

File tree

2 files changed

+75
-28
lines changed

2 files changed

+75
-28
lines changed

.github/workflows/terraform-dev.yml

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: "[DEV] - Terraform Deployment"
2+
3+
on:
4+
push:
5+
branches:
6+
- develop
7+
8+
permissions:
9+
id-token: write
10+
contents: read
11+
12+
jobs:
13+
terraform:
14+
runs-on: ubuntu-latest
15+
16+
defaults:
17+
run:
18+
shell: bash
19+
20+
steps:
21+
- name: Checkout code
22+
uses: actions/checkout@v4
23+
24+
- name: Setup Terraform
25+
uses: hashicorp/setup-terraform@v3
26+
with:
27+
terraform_version: 1.8.3
28+
29+
- name: Configure AWS credentials
30+
uses: aws-actions/configure-aws-credentials@v4
31+
with:
32+
role-to-assume: arn:aws:iam::179916804929:role/BuildRun-GithubActions-Role
33+
role-session-name: GitHub_to_AWS_via_FederatedOIDC
34+
aws-region: ${{ vars.AWS_REGION }}
35+
36+
- name: Read destroy configuration
37+
id: read-destroy-config
38+
run: |
39+
DESTROY_DEV="$(jq -r '.dev' ./infra/destroy_config.json)"
40+
echo "destroy_dev=$(echo $DESTROY_DEV)" >> $GITHUB_OUTPUT
41+
42+
- name: Terraform Init
43+
run: |
44+
cd infra && terraform init \
45+
-backend-config="bucket=${{ vars.TERRAFORM_S3_STATEFILE_BUCKET }}" \
46+
-backend-config="key=${{ github.event.repository.name }}" \
47+
-backend-config="region=${{ env.AWS_REGION }}" \
48+
-backend-config="dynamodb_table=${{ vars.TERRAFORM_DYNAMODB_LOCK_TABLE }}"
49+
50+
- name: Terraform Validate
51+
run: terraform validate
52+
53+
- name: Terraform Destroy for Dev
54+
if: steps.read-destroy-config.outputs.destroy_dev == 'true' && github.ref == 'refs/heads/develop' && github.event_name == 'push'
55+
id: terraform-destroy-dev
56+
run: cd infra &&
57+
terraform workspace select dev || terraform workspace new dev &&
58+
terraform destroy -var-file="./envs/dev/terraform.tfvars" -auto-approve
59+
60+
- name: Terraform Plan for Dev
61+
if: steps.read-destroy-config.outputs.destroy_dev != 'true' && github.ref == 'refs/heads/develop' && github.event_name == 'push'
62+
id: terraform-plan-dev
63+
run: cd infra &&
64+
terraform workspace select dev || terraform workspace new dev &&
65+
terraform plan -var-file="./envs/dev/terraform.tfvars" -out=dev.plan
66+
67+
- name: Terraform Apply for Dev
68+
id: terraform-apply-dev
69+
if: steps.read-destroy-config.outputs.destroy_dev != 'true' && github.ref == 'refs/heads/develop' && github.event_name == 'push'
70+
run: cd infra &&
71+
terraform workspace select dev || terraform workspace new dev &&
72+
terraform apply "dev.plan"

.github/workflows/terraform.yml .github/workflows/terraform-prod.yml

+3-28
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
name: Terraform Deployment
1+
name: "[PROD] - Terraform Deployment"
22

33
on:
44
push:
55
branches:
6-
- develop
76
- main
87

9-
# Permission can be added at job level or workflow level
108
permissions:
11-
id-token: write # This is required for requesting the JWT
12-
contents: read # This is required for actions/checkout
9+
id-token: write
10+
contents: read
1311

1412
jobs:
1513
terraform:
@@ -33,8 +31,6 @@ jobs:
3331
with:
3432
role-to-assume: arn:aws:iam::179916804929:role/BuildRun-GithubActions-Role #change to reflect your IAM role’s ARN
3533
role-session-name: GitHub_to_AWS_via_FederatedOIDC
36-
# aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
37-
# aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
3834
aws-region: ${{ vars.AWS_REGION }}
3935

4036
- name: Read destroy configuration
@@ -56,27 +52,6 @@ jobs:
5652
- name: Terraform Validate
5753
run: terraform validate
5854

59-
- name: Terraform Destroy for Dev
60-
if: steps.read-destroy-config.outputs.destroy_dev == 'true' && github.ref == 'refs/heads/develop' && github.event_name == 'push'
61-
id: terraform-destroy-dev
62-
run: cd infra &&
63-
terraform workspace select dev || terraform workspace new dev &&
64-
terraform destroy -var-file="./envs/dev/terraform.tfvars" -auto-approve
65-
66-
- name: Terraform Plan for Dev
67-
if: steps.read-destroy-config.outputs.destroy_dev != 'true' && github.ref == 'refs/heads/develop' && github.event_name == 'push'
68-
id: terraform-plan-dev
69-
run: cd infra &&
70-
terraform workspace select dev || terraform workspace new dev &&
71-
terraform plan -var-file="./envs/dev/terraform.tfvars" -out=dev.plan
72-
73-
- name: Terraform Apply for Dev
74-
id: terraform-apply-dev
75-
if: steps.read-destroy-config.outputs.destroy_dev != 'true' && github.ref == 'refs/heads/develop' && github.event_name == 'push'
76-
run: cd infra &&
77-
terraform workspace select dev || terraform workspace new dev &&
78-
terraform apply "dev.plan"
79-
8055
- name: Terraform Destroy for Prod
8156
if: steps.read-destroy-config.outputs.destroy_prod == 'true' && github.ref == 'refs/heads/main' && github.event_name == 'push'
8257
id: terraform-destroy-prod

0 commit comments

Comments
 (0)