-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yaml
144 lines (135 loc) · 4.78 KB
/
action.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
name: Terraform (wemogy)
author: wemogy
description: Connects to a Terraform backend, applies or plans the changes and outputs the Terraform Output variables
branding:
icon: "upload-cloud"
color: "blue"
inputs:
working-directory:
required: true
description: "The directory of your terraform scripts"
workspace:
description: "The terraform workspace"
required: false
plan:
description: "Plan the changes"
default: "false"
required: true
apply:
description: "Apply the changes"
default: "true"
required: true
destroy:
description: "Destroy the changes"
default: "false"
required: true
force:
description: "Enforce changes, even if prevent_destroy is set to 'true'"
default: "false"
required: true
client-id:
description: "The Azure Service Pricipal Client ID"
required: true
client-secret:
description: "The Azure Service Pricipal Secret"
required: true
tenant-id:
description: "The Azure Service Pricipal Tenant ID"
required: true
backend-storage-account-name:
description: "The Name of the Azure Storage Account that hosts the remote Terraform backend"
required: true
backend-container-name:
description: "The Name of the the storage container that hosts the remote Terraform backend"
required: true
backend-key:
description: "The name of the file that hosts the remote Terraform backend"
required: true
backend-access-key:
description: "The ARM Access Key to the Azure Storage Account that hosts the remote Terraform backend"
required: true
outputs:
output:
description: "The Terraform output in JSON format"
value: ${{ steps.terraform-output.outputs.output }}
runs:
using: "composite"
steps:
# Check if 'force' is set.
# If so, this script replace s'prevent_destroy = true' with 'prevent_destroy = false' in all .tf files
# see: https://notes.rioastamal.net/2020/03/terraform-force-destroy-resource-when-prevent-destroy-true.html
- name: Check if force is set
run: |
if [[ "${{ inputs.force }}" == "true" ]]; then
find . -name '*.tf' -type f -exec perl -i -pe 's@prevent_destroy = true@prevent_destroy = false@g' {} \;
fi
shell: bash
working-directory: ${{ inputs.working-directory }}
# Setup terraform
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
# Initialize terraform
- name: Init Terraform
run: |
terraform init \
-upgrade \
-reconfigure \
-backend-config="storage_account_name=${{ inputs.backend-storage-account-name }}" \
-backend-config="container_name=${{ inputs.backend-container-name }}" \
-backend-config="key=${{ inputs.backend-key }}"
shell: bash
working-directory: ${{ inputs.working-directory }}
env:
ARM_ACCESS_KEY: ${{ inputs.backend-access-key }}
# Switch terraform workspace
- name: Switch to dev workspace
run: |
if [[ "${{ inputs.workspace }}" != "" ]]; then
echo "Switching workspace to ${{ inputs.workspace }}..."
terraform workspace select ${{ inputs.workspace }}
fi
shell: bash
working-directory: ${{ inputs.working-directory }}
env:
ARM_ACCESS_KEY: ${{ inputs.backend-access-key }}
# Plan the terraform changes
- name: Plan terraform
run: |
if [[ "${{ inputs.plan }}" == "true" ]]; then
echo "Running terraform plan..."
terraform plan
fi
shell: bash
working-directory: ${{ inputs.working-directory }}
env:
ARM_CLIENT_ID: ${{ inputs.client-id }}
ARM_CLIENT_SECRET: ${{ inputs.client-secret }}
ARM_TENANT_ID: ${{ inputs.tenant-id }}
ARM_ACCESS_KEY: ${{ inputs.backend-access-key }}
# Apply / Destroy
- name: Apply / Destroy terraform
run: |
if [[ "${{ inputs.apply }}" == "true" ]]; then
echo "Running terraform apply..."
terraform apply -auto-approve
elif [[ "${{ inputs.destroy }}" == "true" ]]; then
echo "Running terraform destroy..."
terraform destroy -auto-approve
fi
shell: bash
working-directory: ${{ inputs.working-directory }}
env:
ARM_CLIENT_ID: ${{ inputs.client-id }}
ARM_CLIENT_SECRET: ${{ inputs.client-secret }}
ARM_TENANT_ID: ${{ inputs.tenant-id }}
ARM_ACCESS_KEY: ${{ inputs.backend-access-key }}
# Export terraform outputs to output of this job
- name: Terraform Output
id: terraform-output
working-directory: ${{ inputs.working-directory }}
run: |
TERRAFORM_OUTPUT=$(terraform output --json | jq -c .)
echo "::set-output name=output::$TERRAFORM_OUTPUT"
shell: bash
env:
ARM_ACCESS_KEY: ${{ inputs.backend-access-key }}