Skip to content

Commit 15b7e4c

Browse files
authored
Merge pull request #4 from buildrun-tech/develop
Develop
2 parents 51e8337 + 2156679 commit 15b7e4c

File tree

2 files changed

+84
-30
lines changed

2 files changed

+84
-30
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

+12-30
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
name: Terraform Deployment
1+
name: "[PROD] - Terraform Deployment"
22

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

8+
permissions:
9+
id-token: write
10+
contents: read
11+
912
jobs:
1013
terraform:
1114
runs-on: ubuntu-latest
@@ -16,27 +19,27 @@ jobs:
1619

1720
steps:
1821
- name: Checkout code
19-
uses: actions/checkout@v2
22+
uses: actions/checkout@v4
2023

2124
- name: Setup Terraform
22-
uses: hashicorp/setup-terraform@v2
25+
uses: hashicorp/setup-terraform@v3
2326
with:
2427
terraform_version: 1.8.3
2528

2629
- name: Configure AWS credentials
27-
uses: aws-actions/configure-aws-credentials@v2
30+
uses: aws-actions/configure-aws-credentials@v4
2831
with:
29-
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
30-
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
32+
role-to-assume: arn:aws:iam::179916804929:role/BuildRun-GithubActions-Role #change to reflect your IAM role’s ARN
33+
role-session-name: GitHub_to_AWS_via_FederatedOIDC
3134
aws-region: ${{ vars.AWS_REGION }}
3235

3336
- name: Read destroy configuration
3437
id: read-destroy-config
3538
run: |
3639
DESTROY_DEV="$(jq -r '.dev' ./infra/destroy_config.json)"
3740
DESTROY_PROD="$(jq -r '.prod' ./infra/destroy_config.json)"
38-
echo "::set-output name=destroy_dev::$DESTROY_DEV"
39-
echo "::set-output name=destroy_prod::$DESTROY_PROD"
41+
echo "destroy_dev=$(echo $DESTROY_DEV)" >> $GITHUB_OUTPUT
42+
echo "destroy_prod=$(echo $DESTROY_PROD)" >> $GITHUB_OUTPUT
4043
4144
- name: Terraform Init
4245
run: |
@@ -49,27 +52,6 @@ jobs:
4952
- name: Terraform Validate
5053
run: terraform validate
5154

52-
- name: Terraform Destroy for Dev
53-
if: steps.read-destroy-config.outputs.destroy_dev == 'true' && github.ref == 'refs/heads/develop' && github.event_name == 'push'
54-
id: terraform-destroy-dev
55-
run: cd infra &&
56-
terraform workspace select dev || terraform workspace new dev &&
57-
terraform destroy -var-file="./envs/dev/terraform.tfvars" -auto-approve
58-
59-
- name: Terraform Plan for Dev
60-
if: steps.read-destroy-config.outputs.destroy_dev != 'true' && github.ref == 'refs/heads/develop' && github.event_name == 'push'
61-
id: terraform-plan-dev
62-
run: cd infra &&
63-
terraform workspace select dev || terraform workspace new dev &&
64-
terraform plan -var-file="./envs/dev/terraform.tfvars" -out=dev.plan
65-
66-
- name: Terraform Apply for Dev
67-
id: terraform-apply-dev
68-
if: steps.read-destroy-config.outputs.destroy_dev != 'true' && github.ref == 'refs/heads/develop' && github.event_name == 'push'
69-
run: cd infra &&
70-
terraform workspace select dev || terraform workspace new dev &&
71-
terraform apply "dev.plan"
72-
7355
- name: Terraform Destroy for Prod
7456
if: steps.read-destroy-config.outputs.destroy_prod == 'true' && github.ref == 'refs/heads/main' && github.event_name == 'push'
7557
id: terraform-destroy-prod

0 commit comments

Comments
 (0)