-
Notifications
You must be signed in to change notification settings - Fork 0
77 lines (64 loc) · 2.23 KB
/
deploy.yaml
File metadata and controls
77 lines (64 loc) · 2.23 KB
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
75
76
77
name: Terraform Deploy
on:
push:
branches:
- staging
- main
workflow_dispatch:
env:
TF_WORKING_DIR: terraform
jobs:
terraform:
name: Terraform (plan & apply)
runs-on: ubuntu-latest
environment: ${{ github.ref == 'refs/heads/main' && 'prod' || 'staging' }}
permissions:
contents: read
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_version: 1.5.6
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
# Backend selection based on branch
- name: Terraform Init
working-directory: ${{ env.TF_WORKING_DIR }}
run: |
if [ "${{ github.ref }}" = "refs/heads/staging" ]; then
terraform init -reconfigure -backend-config=backend-staging.tfvars -input=false
else
terraform init -reconfigure -backend-config=backend-prod.tfvars -input=false
fi
- name: Terraform Validate & Format
working-directory: ${{ env.TF_WORKING_DIR }}
run: |
terraform fmt -check
terraform validate
- name: Terraform Plan
id: plan
working-directory: ${{ env.TF_WORKING_DIR }}
run: |
if [ "${{ github.ref }}" = "refs/heads/staging" ]; then
terraform plan -var-file="staging.tfvars" -out=tfplan
else
terraform plan -var-file="prod.tfvars" -out=tfplan
fi
- name: Terraform Apply (staging)
if: github.ref == 'refs/heads/staging'
working-directory: ${{ env.TF_WORKING_DIR }}
run: terraform apply -input=false -auto-approve tfplan
- name: Terraform Apply (prod)
if: github.ref == 'refs/heads/main'
working-directory: ${{ env.TF_WORKING_DIR }}
run: terraform apply -input=false -auto-approve tfplan
- name: Show Outputs
if: success()
working-directory: ${{ env.TF_WORKING_DIR }}
run: terraform output -json