-
Notifications
You must be signed in to change notification settings - Fork 5
81 lines (70 loc) · 2.68 KB
/
terraform.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
75
76
77
78
79
80
81
name: "Terraform Workflow"
on:
workflow_call:
inputs:
environment:
type: string
required: true
aws-assume-role-arn:
type: string
required: true
aws-region:
type: string
required: true
aws-statefile-s3-bucket:
type: string
required: true
aws-lock-dynamodb-table:
type: string
required: true
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: ${{ inputs.aws-assume-role-arn }}
role-session-name: GitHub_to_AWS_via_FederatedOIDC
aws-region: ${{ inputs.aws-region }}
- name: Read destroy configuration
id: read-destroy-config
run: |
DESTROY="$(jq -r '.${{ inputs.environment }}' ./infra/destroy_config.json)"
echo "destroy=$(echo $DESTROY)" >> $GITHUB_OUTPUT
- name: Terraform Init
run: |
cd infra && terraform init \
-backend-config="bucket=${{ inputs.aws-statefile-s3-bucket }}" \
-backend-config="key=${{ github.event.repository.name }}" \
-backend-config="region=${{ inputs.aws-region }}" \
-backend-config="dynamodb_table=${{ inputs.aws-lock-dynamodb-table }}"
- name: Terraform Validate
run: terraform validate
- name: Terraform Destroy
if: steps.read-destroy-config.outputs.destroy == 'true'
id: terraform-destroy
run: cd infra &&
terraform workspace select ${{ inputs.environment }} || terraform workspace new ${{ inputs.environment }} &&
terraform destroy -var-file="./envs/${{ inputs.environment }}/terraform.tfvars" -auto-approve
- name: Terraform Plan
if: steps.read-destroy-config.outputs.destroy != 'true'
id: terraform-plan
run: cd infra &&
terraform workspace select ${{ inputs.environment }} || terraform workspace new ${{ inputs.environment }} &&
terraform plan -var-file="./envs/${{ inputs.environment }}/terraform.tfvars" -out="${{ inputs.environment }}.plan"
- name: Terraform Apply
if: steps.read-destroy-config.outputs.destroy != 'true'
id: terraform-apply
run: cd infra &&
terraform workspace select ${{ inputs.environment }} || terraform workspace new ${{ inputs.environment }} &&
terraform apply "${{ inputs.environment }}.plan"