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