-
Notifications
You must be signed in to change notification settings - Fork 0
241 lines (197 loc) · 8.34 KB
/
terraform-apply.yaml
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
name: Terraform Apply
on:
pull_request:
branches:
- "main"
push:
branches:
- "*-GHTEST"
- "*-GHTESTAPPLY"
permissions:
id-token: write
contents: read
jobs:
terraform-apply-dev:
name: 'Development'
runs-on: ubuntu-latest
environment: development
defaults:
run:
shell: bash
env:
ENV: "development"
TF_VAR_assume_role: ${{ secrets.ASSUME_ROLE }}
steps:
# Checkout the repository to the GitHub Actions runner
- name: Checkout
uses: actions/checkout@v4
- name: Set specified Terraform Version
run: |
tf_version=$(cat versions.tf 2> /dev/null | grep required_version | cut -d "\"" -f 2)
echo "Terraform version specified is: $tf_version"
echo "TFVERSION=${tf_version}" >> $GITHUB_ENV
# Configure AWS credentials
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: eu-west-2
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
role-session-name: GitHubWorkflow
- name: Get TF_VARS for environment
run: |
./scripts/generate-github-env.sh
shell: bash
- name: Check env
run: |
env | sort
shell: bash
# Install the specified version of Terraform CLI
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ env.TFVERSION }}
# Initialize a new or existing Terraform working directory by creating initial files, loading any remote state, downloading modules, etc.
- name: Terraform Init
run: terraform init -reconfigure --backend-config="key=terraform.$ENV.state"
# Select or create and then select a Terraform workspace
- name: Terraform Workspace
run: terraform workspace select $ENV || terraform workspace new $ENV
# Checks that all Terraform configuration files adhere to a canonical format
- name: Terraform Format
run: terraform fmt --recursive -check
# Validates Terraform configuration
- name: Terraform Validate
run: terraform validate
# Generates an execution plan for Terraform
- name: Terraform Plan
run: terraform plan
################################################################
### TODO Changes to some TF_VARs required before we can use ###
################################################################
# # On push to main, build or change infrastructure according to Terraform configuration files
# # Note: It is recommended to set up a required "strict" status check in your repository for "Terraform Cloud". See the documentation on "strict" required status checks for more information: https://help.github.com/en/github/administering-a-repository/types-of-required-status-checks
# - name: Terraform Apply
# if: github.ref == 'refs/heads/main' && github.event_name == 'push'
# run: terraform apply -auto-approve
# # Terraform apply in pre-production
# terraform-apply-prep:
# name: 'Pre-Production'
# needs: terraform-apply-dev
# runs-on: ubuntu-latest
# environment: pre-production
# defaults:
# run:
# shell: bash
# env:
# ENV: "pre-production"
# TF_VAR_assume_role: ${{ secrets.ASSUME_ROLE }}
# steps:
# # Checkout the repository to the GitHub Actions runner
# - name: Checkout
# uses: actions/checkout@v3
# - name: Set specified Terraform Version
# run: |
# tf_version=$(cat versions.tf 2> /dev/null | grep required_version | cut -d "\"" -f 2)
# echo "Terraform version specified is: $tf_version"
# echo "TFVERSION=${tf_version}" >> $GITHUB_ENV
# # Configure AWS credentials
# - name: Configure AWS Credentials
# uses: aws-actions/configure-aws-credentials@v2
# with:
# aws-region: eu-west-2
# role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
# role-session-name: GitHubWorkflow
# - name: Get TF_VARS for environment
# run: |
# ./scripts/generate-github-env.sh
# shell: bash
# - name: Check env
# run: |
# env | sort
# shell: bash
# # Install the specified version of Terraform CLI
# - name: Setup Terraform
# uses: hashicorp/setup-terraform@v2
# with:
# terraform_version: ${{ env.TFVERSION }}
# # Initialize a new or existing Terraform working directory by creating initial files, loading any remote state, downloading modules, etc.
# - name: Terraform Init
# run: terraform init -reconfigure --backend-config="key=terraform.$ENV.state"
# # Select or create and then select a Terraform workspace
# - name: Terraform Workspace
# run: terraform workspace select $ENV || terraform workspace new $ENV
# # Checks that all Terraform configuration files adhere to a canonical format
# - name: Terraform Format
# run: terraform fmt --recursive -check
# # Validates Terraform configuration
# - name: Terraform Validate
# run: terraform validate
# # Generates an execution plan for Terraform
# - name: Terraform Plan
# run: terraform plan
# # On push to main, build or change infrastructure according to Terraform configuration files
# # Note: It is recommended to set up a required "strict" status check in your repository for "Terraform Cloud". See the documentation on "strict" required status checks for more information: https://help.github.com/en/github/administering-a-repository/types-of-required-status-checks
# # - name: Terraform Apply
# # if: github.ref == 'refs/heads/main' && github.event_name == 'push'
# # run: terraform apply -auto-approve
# # Terraform apply in production
# terraform-apply-prod:
# name: 'Production'
# needs: terraform-apply-prep
# runs-on: ubuntu-latest
# environment: production
# defaults:
# run:
# shell: bash
# env:
# ENV: "production"
# TF_VAR_assume_role: ${{ secrets.ASSUME_ROLE }}
# steps:
# # Checkout the repository to the GitHub Actions runner
# - name: Checkout
# uses: actions/checkout@v3
# - name: Set specified Terraform Version
# run: |
# tf_version=$(cat versions.tf 2> /dev/null | grep required_version | cut -d "\"" -f 2)
# echo "Terraform version specified is: $tf_version"
# echo "TFVERSION=${tf_version}" >> $GITHUB_ENV
# # Configure AWS credentials
# - name: Configure AWS Credentials
# uses: aws-actions/configure-aws-credentials@v2
# with:
# aws-region: eu-west-2
# role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
# role-session-name: GitHubWorkflow
# - name: Get TF_VARS for environment
# run: |
# ./scripts/generate-github-env.sh
# shell: bash
# - name: Check env
# run: |
# env | sort
# shell: bash
# # Install the specified version of Terraform CLI
# - name: Setup Terraform
# uses: hashicorp/setup-terraform@v2
# with:
# terraform_version: ${{ env.TFVERSION }}
# # Initialize a new or existing Terraform working directory by creating initial files, loading any remote state, downloading modules, etc.
# - name: Terraform Init
# run: terraform init -reconfigure --backend-config="key=terraform.$ENV.state"
# # Select or create and then select a Terraform workspace
# - name: Terraform Workspace
# run: terraform workspace select $ENV || terraform workspace new $ENV
# # Checks that all Terraform configuration files adhere to a canonical format
# - name: Terraform Format
# run: terraform fmt --recursive -check
# # Validates Terraform configuration
# - name: Terraform Validate
# run: terraform validate
# # Generates an execution plan for Terraform
# - name: Terraform Plan
# run: terraform plan
# # On push to main, build or change infrastructure according to Terraform configuration files
# # Note: It is recommended to set up a required "strict" status check in your repository for "Terraform Cloud". See the documentation on "strict" required status checks for more information: https://help.github.com/en/github/administering-a-repository/types-of-required-status-checks
# # - name: Terraform Apply
# # if: github.ref == 'refs/heads/main' && github.event_name == 'push'
# # run: terraform apply -auto-approve