-
-
Notifications
You must be signed in to change notification settings - Fork 0
159 lines (137 loc) · 4.75 KB
/
tf-deploy.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
name: Terraform Deploy
on:
# Permit targeted manual trigger within console
workflow_dispatch:
inputs:
location:
description: 'Env to deploy to'
type: choice
required: true
options:
- dev-s001
- stg-s001
# Permits trigger from orchestration layer
workflow_call:
inputs:
location:
required: true
type: string
env:
tf_version: 1.4.5
ARM_USE_OIDC: true
permissions:
id-token: write
contents: read
repository-projects: read
jobs:
plan:
name: TF Plan
runs-on: az-terraform
outputs:
TF_APPLY_ENVIRONMENT: ${{ steps.set-tf-apply-env.outputs.ENVIRONMENT }}
TF_CHANGES: ${{ steps.tf-changes-test.outputs.TF_CHANGES }}
steps:
- uses: actions/checkout@v3
- name: Install Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_version: ${{ env.tf_version }}
- name: az login
uses: azure/login@v1
with:
tenant-id: ${{ env.ARM_TENANT_ID }}
client-id: ${{ env.ARM_CLIENT_ID }}
subscription-id: ${{ env.ARM_SUBSCRIPTION_ID }}
- name: Terraform Init
id: init
run: |
terraform init \
-input=false \
-backend-config="resource_group_name=${{ env.tf_storage_resource_group_name }}" \
-backend-config="storage_account_name=${{ env.tf_storage_account_name }}" \
-backend-config="container_name=${{ env.tf_storage_container_name }}" \
-backend-config="key=${{ env.tf_state_filename }}.tfstate"
- name: Terraform Plan
id: plan
run: |
terraform plan \
-input=false \
-var-file="data/${{ env.solution_name }}.tfvars" \
-out tf.plan
- name: Terraform No Change Test
id: tf-changes-test
run: |
# Check the tf.plan file, see if there are any changes
if terraform show tf.plan | grep -q "Your infrastructure matches the configuration."; then
echo "TF_CHANGES=false" >> $GITHUB_OUTPUT
echo "There are no changes for terraform to apply, exiting."
else
echo "TF_CHANGES=true" >> $GITHUB_OUTPUT
echo "There are changes for terraform to apply, continuing."
fi
- name: Cache Files
id: cache-files
# Attempt to run only when there are TF changes
if: steps.tf-changes-test.outputs.TF_CHANGES == 'true'
uses: actions/upload-artifact@v4
with:
name: tf-cache-${{ github.run_id }}-${{ inputs.location }}
retention-days: 365 # max 400 for private repos, 90 for public
# Grab the entire workspace, but exclude the .terraform folder
# It's large and has provider binaries we can easily download in deploy step
path: |
${{ github.workspace }}/
!${{ github.workspace }}/.terraform
- name: Set TF Apply Environment
# Attempt to run only when there are TF changes
if: steps.tf-changes-test.outputs.TF_CHANGES == 'true'
id: set-tf-apply-env
run: |
echo "ENVIRONMENT=$TF_APPLY_ENV" >> $GITHUB_OUTPUT
deploy:
name: Deploy
needs: plan
# Attempt to run only when there are TF changes
if: |
needs.plan.outputs.TF_CHANGES == 'true'
runs-on: az-terraform
environment: ${{ needs.plan.outputs.TF_APPLY_ENVIRONMENT }}
steps:
- uses: actions/checkout@v3
- name: Download Env Vars
id: download-env-vars
uses: actions/download-artifact@v4
with:
name: env-cache-${{ github.run_id }}-${{ inputs.location }}
- name: Read Env Vars
id: read-env-vars
run: |
cat env.vars >> $GITHUB_ENV
- name: Install Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_version: ${{ env.tf_version }}
- name: az login
uses: azure/login@v1
with:
tenant-id: ${{ env.ARM_TENANT_ID }}
client-id: ${{ env.ARM_CLIENT_ID }}
subscription-id: ${{ env.ARM_SUBSCRIPTION_ID }}
- name: Terraform Init
id: init
run: |
terraform init \
-input=false \
-backend-config="resource_group_name=${{ env.tf_storage_resource_group_name }}" \
-backend-config="storage_account_name=${{ env.tf_storage_account_name }}" \
-backend-config="container_name=${{ env.tf_storage_container_name }}" \
-backend-config="key=${{ env.tf_state_filename }}.tfstate"
- name: Download Cache
id: download-cache
uses: actions/download-artifact@v4
with:
name: tf-cache-${{ github.run_id }}-${{ inputs.location }}
- name: Terraform Apply
id: apply
run: |
terraform apply -input=false tf.plan