-
Notifications
You must be signed in to change notification settings - Fork 5
157 lines (147 loc) · 6.35 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
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
name: "Terraform"
on:
workflow_call:
inputs:
terraform_root_path:
description: the path to the root module to apply
type: string
required: false
default: .
terraform_operation:
description: the terraform operation to perform (apply or destroy)
type: string
required: false
secrets:
AWS_ACCESS_KEY_ID:
required: false
AWS_SECRET_ACCESS_KEY:
required: false
BOT_GITHUB_TOKEN:
required: false
DOCKER_USERNAME:
required: false
DOCKER_PASSWORD:
required: false
RANCHER2_ACCESS_KEY:
required: false
RANCHER2_SECRET_KEY:
required: false
jobs:
compute_basic_vars:
name: compute basic variables
runs-on: ubuntu-latest
steps:
- name: Evaluate branch name
id: basic_vars
run: |
BRANCH_NAME=${{ github.base_ref || github.ref_name }}
BRANCH_LOWER=${BRANCH_NAME,,}
if [ "$BRANCH_LOWER" == "main" ]; then
environment="production"
elif [ "$BRANCH_LOWER" == "develop" ]; then
environment="develop"
else
echo "$BRANCH_LOWER doesn't have a matching environment"
exit 1
fi
echo "environment_name=${environment}" >> $GITHUB_OUTPUT
outputs:
environment_name: ${{ steps.basic_vars.outputs.environment_name }}
terraform:
runs-on: ubuntu-latest
needs:
- compute_basic_vars
environment: ${{ needs.compute_basic_vars.outputs.environment_name }}
concurrency:
group: ${{ needs.compute_basic_vars.outputs.environment_name }}
cancel-in-progress: false
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: ${{ vars.AWS_DEFAULT_REGION }}
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
RANCHER2_ACCESS_KEY: ${{ secrets.RANCHER2_ACCESS_KEY }}
RANCHER2_SECRET_KEY: ${{ secrets.RANCHER2_SECRET_KEY }}
RANCHER2_URL: ${{ vars.RANCHER2_URL }}
RESOURCE_NAME: ${{ vars.RESOURCE_NAME }}
TERRAFORM_HTTP_CREDENTIALS: |
github.com/Alfresco=alfresco-build:${{ secrets.BOT_GITHUB_TOKEN }}
TERRAFORM_PRE_RUN: |
if [ ! -x ./aws/install ]; then
curl -sSf https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip -o awscliv2.zip
unzip -q awscliv2.zip
fi
./aws/install
# https://github.com/dflook/terraform-github-actions/tree/main/terraform-apply#applying-a-plan-using-a-comment
PR_CHECKOUT_REF: refs/pull/${{ github.event.issue.number }}/merge
steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
ref: ${{ github.event_name == 'issue_comment' && env.PR_CHECKOUT_REF || '' }}
- name: Assume AWS Role if AWS_ROLE_ARN var is set
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
if: vars.AWS_ROLE_ARN != ''
with:
aws-region: ${{ env.AWS_DEFAULT_REGION}}
role-to-assume: ${{ vars.AWS_ROLE_ARN }}
role-session-name: TerraformWorkflow
- name: Load environment variables from yml
uses: Alfresco/alfresco-build-tools/.github/actions/[email protected]
with:
yml_path: ${{ inputs.terraform_root_path }}/tfenv.yml
- name: Check vars requirements
run: |
if [ -z "${{ vars.RESOURCE_NAME }}" ]; then
echo "RESOURCE_NAME must be set in the vars context to provide a unique identifier"
exit 1
fi
if [ -z "${{ vars.TERRAFORM_STATE_BUCKET }}" ]; then
echo "TERRAFORM_STATE_BUCKET must be set in the vars context"
exit 1
fi
- name: Terraform validate
uses: dflook/terraform-validate@93108d6d37b2fd79d527ec77c190105de1429bb5 # v1.44.0
with:
path: ${{ inputs.terraform_root_path }}
backend_config: |
bucket=${{ vars.TERRAFORM_STATE_BUCKET }}
key=${{ vars.RESOURCE_NAME }}/${{ inputs.terraform_root_path }}/terraform.tfstate
- name: Terraform plan
uses: dflook/terraform-plan@b0877a1bf56160160ab482f4aff8a12fff35fe06 # v1.44.0
if: github.event_name == 'pull_request'
with:
label: ${{ vars.RESOURCE_NAME }} ${{ inputs.terraform_root_path }}
path: ${{ inputs.terraform_root_path }}
var_file: |
${{ inputs.terraform_root_path }}/common.tfvars
${{ inputs.terraform_root_path }}/${{ needs.compute_basic_vars.outputs.environment_name }}.tfvars
backend_config: |
bucket=${{ vars.TERRAFORM_STATE_BUCKET }}
key=${{ vars.RESOURCE_NAME }}/${{ inputs.terraform_root_path }}/terraform.tfstate
- name: Terraform apply
uses: dflook/terraform-apply@7d435d4d115a11e5db1e710ac969f5382f0f6f9f # v1.44.0
if: github.event_name == 'push' || inputs.terraform_operation == 'apply' || (github.event_name == 'issue_comment' && contains(github.event.comment.body, 'terraform apply'))
with:
label: ${{ vars.RESOURCE_NAME }} ${{ inputs.terraform_root_path }}
path: ${{ inputs.terraform_root_path }}
auto_approve: ${{ inputs.terraform_operation == 'apply' }}
var_file: |
${{ inputs.terraform_root_path }}/common.tfvars
${{ inputs.terraform_root_path }}/${{ needs.compute_basic_vars.outputs.environment_name }}.tfvars
backend_config: |
bucket=${{ vars.TERRAFORM_STATE_BUCKET }}
key=${{ vars.RESOURCE_NAME }}/${{ inputs.terraform_root_path }}/terraform.tfstate
- name: Terraform destroy
uses: dflook/terraform-destroy@2e74fdc282c2980b12503986b2d8483d7cbfa14a # v1.44.0
if: inputs.terraform_operation == 'destroy'
with:
path: ${{ inputs.terraform_root_path }}
var_file: |
${{ inputs.terraform_root_path }}/common.tfvars
${{ inputs.terraform_root_path }}/${{ needs.compute_basic_vars.outputs.environment_name }}.tfvars
backend_config: |
bucket=${{ vars.TERRAFORM_STATE_BUCKET }}
key=${{ vars.RESOURCE_NAME }}/${{ inputs.terraform_root_path }}/terraform.tfstate