-
Notifications
You must be signed in to change notification settings - Fork 4
/
action.yml
244 lines (240 loc) · 9.65 KB
/
action.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
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
242
243
244
name: "Run Terraform via Terragrunt"
description: "This action setup, caches and runs terraform via terragrunt including ssh-agent."
author: Datadrivers GmbH
branding:
icon: "terminal"
color: "blue"
inputs:
terraform-version:
description: "terraform version to setup. A version is required to install terraform binarys"
required: false
default: "disabled"
terraform-plan-filename:
description: "name for terraform plan files, which are used as input to generate plan json files as input for pr-commenter action"
required: false
default: "terraform.tfplan"
enable-terraform-change-pr-commenter:
description: "toggle to add pr comment for terraform-plan-filename files. Requires pre-generated terraform.tfplan file."
required: false
default: "false"
include-plan-job-summary:
description: "If true, add the results of the plan to the workflow job summary."
required: false
default: "true"
pr-commenter-comment-header:
description: "Header for the PR comment"
required: false
default: "Terraform Plan Changes"
pr-commenter-comment-footer:
description: "Footer for the PR comment"
required: false
default: ""
terragrunt-version:
description: "terragrunt version to setup. A version is required to install terragrunt binarys"
required: false
default: "disabled"
terragrunt-working-directory:
description: "working-directory for github step to run terragrunt/terraform command"
required: false
default: "."
terragrunt-cache-extra-key:
description: "string appended to terragrunt cache key "
required: false
default: ""
commands:
description: "terraform execution line. For multiple commands use | multi line syntax"
required: true
use-aws-auth:
description: "Toggle to log in to aws"
required: false
default: "false"
aws-region:
description: "AWS Region for Login"
required: false
default: "eu-central-1"
aws-role-to-assume:
description: "AWS IAM Role for github action to access aws api"
required: false
default: ""
aws-role-duration-seconds:
description: "AWS session time"
required: false
default: "1800"
use-gcloud-auth:
description: "Toggle to log in via gcloud to aws"
required: false
default: "false"
gcp-workload-identity-provider:
description: |-
The full identifier of the Workload Identity Provider, including the
project number, pool name, and provider name. If provided, this must be
the full identifier which includes all parts, for example:
"projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider".
This is required if "use-gcloud-auth" is true.
required: false
default: ""
gcp-service-account-email:
description: |-
Email address or unique identifier of the Google Cloud service account for
which to generate credentials. This is required if
"workload_identity_provider" is specified and "use-gcloud-auth" is true.
required: false
default: ""
gcp-project-id:
description: |-
ID of the default project to use for future API calls and invocations. This is required if
"use-gcloud-auth" is true.
required: false
terragrunt-download:
description: "path to terragrunt cache dir storage"
required: false
default: "$HOME/.terragrunt-cache/"
skip-caches:
description: "Toggle to skip caches"
required: false
default: "false"
use-ssh-agent:
description: "Toggle to setup ssh agent with private key. Requires github action secret passed to `ssh-private-key`"
required: false
default: "false"
ssh-private-key:
description: "Content of secret passed in here`"
required: false
default: ""
enable-debug:
description: "Add debug output to steps"
required: false
default: "false"
github-token:
description: 'Github token to use for getting latest release'
required: false
use-automatic-binary-detection:
description: 'This action automatically detects which binary (terraform XOR terragrunt) to use. It does this by checking if terragrunt.hcl is present. If you set this to false terraform will be used'
required: false
default: "true"
runs:
using: "composite"
steps:
# Setup terraform
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
if: inputs.terraform-version != 'disabled'
with:
terraform_version: ${{ inputs.terraform-version }}
terraform_wrapper: false
- name: Terragrunt Binary Installer Action
uses: autero1/action-terragrunt@v3.0.2
if: inputs.terragrunt-version != 'disabled'
with:
terragrunt-version: ${{ inputs.terragrunt-version }}
token: ${{ inputs.github-token}}
- name: Config caches
if: inputs.skip-caches == 'false'
env:
INPUT_TERRAGRUNT_DOWNLOAD: ${{ inputs.terragrunt-download }}
shell: bash
run: |
TF_PLUGIN_CACHE_DIR="$HOME/.terraform.d/plugin-cache"
echo "TF_PLUGIN_CACHE_DIR=$TF_PLUGIN_CACHE_DIR" >> $GITHUB_ENV
mkdir --parents $TF_PLUGIN_CACHE_DIR
TERRAGRUNT_DOWNLOAD=${INPUT_TERRAGRUNT_DOWNLOAD//\$HOME/$HOME} # make sure $HOME is expanded
echo "TERRAGRUNT_DOWNLOAD=$TERRAGRUNT_DOWNLOAD" >> $GITHUB_ENV
mkdir --parents $TERRAGRUNT_DOWNLOAD
# clean up to make sure self-hosted runner dirs are clean
shopt -s dotglob
cd "${TERRAGRUNT_DOWNLOAD:?}" && rm -rf *
- name: Cache Terraform
if: inputs.skip-caches == 'false'
uses: actions/cache@v4
with:
path: |
~/.terraform.d/plugin-cache
key: ${{ runner.os }}-terraform-${{ hashFiles('**/.terraform.lock.hcl') }}
restore-keys: |
${{ runner.os }}-terraform-
- name: Cache Terragrunt
if: inputs.skip-caches == 'false'
uses: actions/cache@v4
with:
path: |
${{ env.TERRAGRUNT_DOWNLOAD }}
key: ${{ runner.os }}-${{ env.TERRAGRUNT_DOWNLOAD }}-${{ hashFiles('**/terragrunt.hcl','**/.terraform.lock.hcl') }}${{ inputs.terragrunt-cache-extra-key }}
restore-keys: |
${{ runner.os }}-${{ env.TERRAGRUNT_DOWNLOAD }}-${{ hashFiles('**/terragrunt.hcl','**/.terraform.lock.hcl') }}
- id: "gcloud-auth"
if: inputs.use-gcloud-auth == 'true'
name: "Authenticate to GCP"
uses: "google-github-actions/auth@v2"
with:
project_id: ${{ inputs.gcp-project-id }}
workload_identity_provider: ${{ inputs.gcp-workload-identity-provider }}
service_account: ${{ inputs.gcp-service-account-email }}
- id: "aws-auth"
if: inputs.use-aws-auth == 'true'
name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: ${{ inputs.aws-region }}
role-to-assume: ${{ inputs.aws-role-to-assume }}
role-duration-seconds: ${{ inputs.aws-role-duration-seconds }}
- name: Setup SSH agent
if: inputs.use-ssh-agent == 'true'
uses: webfactory/ssh-agent@v0.9.0
with:
ssh-private-key: ${{ inputs.ssh-private-key }}
- name: Run Terraform
shell: bash
working-directory: ${{ inputs.terragrunt-working-directory }}
run: ${{ inputs.commands }}
- name: Convert terraform plan files to json
if: inputs.enable-terraform-change-pr-commenter == 'true'
env:
INPUT_TERRAFORM_PLAN_FILENAME: ${{ inputs.terraform-plan-filename }}
INPUT_USE_AUTOMATIC_BINARY_DETECTION: ${{ inputs.use-automatic-binary-detection }}
INPUT_ENABLE_DEBUG: ${{ inputs.enable-debug }}
shell: bash
id: tf-planfiles
run: |
echo "::group::convert terraform plan files to json"
[[ "$ACT" == "true" ]] && apt-get update && apt-get install parallel -y # on act runner parallel is missing
set -e
[[ "${INPUT_ENABLE_DEBUG}" == "true" ]] && set -x
planfiles_json=()
tf_convert_plan_to_json(){
[[ "${INPUT_ENABLE_DEBUG}" == "true" ]] && set -x
exec 3>&1
exec 1>&2
set -e
local planfile=$1
planfile_name=$(echo ${planfile##*/})
planfile_path=$(dirname $planfile)
terraform_bin="terraform"
tfplan_json=$(readlink -f "${planfile_path}/tfplan.json")
cd "${planfile_path}"
[[ "${INPUT_USE_AUTOMATIC_BINARY_DETECTION}" == "true" ]] && test -s terragrunt.hcl && terraform_bin="terragrunt" || true
$terraform_bin show -no-color -json "${planfile_name}" > "${tfplan_json}"
echo ${tfplan_json} >&3
cd - &>/dev/null
};
export -f tf_convert_plan_to_json
mapfile -t planfiles_json < <( find . -type f -name ${INPUT_TERRAFORM_PLAN_FILENAME} | parallel 'tf_convert_plan_to_json {}' )
content=$(printf "%s\n" "${planfiles_json[@]}")
if [[ -n "${content}" ]]; then
delimiter="$(openssl rand -hex 8)"
echo "terraform_planfiles_json<<${delimiter}" >> "${GITHUB_OUTPUT}"
echo "${content}" >> "${GITHUB_OUTPUT}"
echo "${delimiter}" >> "${GITHUB_OUTPUT}"
fi
echo "::endgroup::"
- name: Add PR comment for terraform plan changes
if: inputs.enable-terraform-change-pr-commenter == 'true' && endsWith( steps.tf-planfiles.outputs.terraform_planfiles_json , '.json' )
uses: liatrio/terraform-change-pr-commenter@v1.7.0
continue-on-error: true
with:
include-plan-job-summary: ${{ inputs.include-plan-job-summary }}
comment-header: ${{ inputs.pr-commenter-comment-header }}
comment-footer: ${{ inputs.pr-commenter-comment-footer }}
hide-previous-comments: true
log-changed-resources: false
json-file: |
${{ steps.tf-planfiles.outputs.terraform_planfiles_json }}