-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaction.yaml
104 lines (89 loc) · 3.35 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
name: Review terraform plan
branding:
icon: 'map'
color: 'purple'
description: >
Run and cleanup terraform plan output, store it into an output to allow reviewing it.
inputs:
terraform-environment:
description: >
Terraform environment, used to load corresponding var file named
<terraform-environment>.tfvars into the working directory.
required: true
default: "preproduction"
working-directory:
description: working directory
required: true
default: "."
terraform-plan-output:
description: >
Path of the plan file, to be used by the action "Apply terraform plan".
default: "/tmp/plan.tfplan"
required: false
outputs:
plan-details:
description: >
Clean terraform plan, in HTML format, ready to integrate in a comment of
corresponding pull request.
value: ${{ steps.plan.outputs.plan-details }}
runs:
using: "composite"
steps:
- id: plan
name: terraform plan for review
shell: bash
working-directory: ${{ inputs.working-directory }}
run: |
echo "🗺 terraform plan -lock=false -var-file=${{ inputs.terraform-environment }}.tfvars --out ${{ inputs.terraform-plan-output }}"
plan=$(terraform plan -lock=false -no-color -var-file=${{ inputs.terraform-environment }}.tfvars --out ${{ inputs.terraform-plan-output }})
title=$(
echo -e $(echo -e "$plan" | grep -e '^Plan:' || echo -e "") | \
sed -E 's|Plan:||'
)
echo "🧹 remove plan noise"
tf_plan=$(
echo -e "$plan" | \
tail -n +$(
echo -e "$plan" | \
grep -nE 'Terraform will perform the following actions|No changes. Infrastructure is up-to-date|No changes. Your infrastructure matches the configuration' | \
cut -d':' -f1
)
)
echo "📝 generate drift details"
driftSeparatorLineNumber=$(
echo -e "$plan" | \
grep -nE 'Terraform detected the following changes made outside of Terraform' | cut -d':' -f1
) || true
if [ -z "$driftSeparatorLineNumber" ]; then
echo "🚫 no drift detected"
drift=""
else
drift=$(
echo -e "$plan" | \
tail -n +$driftSeparatorLineNumber | sed '1,/─────────────────────────────────────────────────────────────────────────────/!d' | head -n -2
)
fi
echo "🎁 wrap plan into HTML"
message=$(printf "
<details>
<summary><b>Terraform plan:</b> %s %s</summary>
\`\`\`hcl
%s
\`\`\`
</details>
<details>
<summary><b>Terraform drift:</b> %s</summary>
\`\`\`hcl
%s
\`\`\`
</details>
" "${title:-No changes. Infrastructure is up-to-date.}" "$(git rev-parse --short $GITHUB_SHA)" "$tf_plan" "$(git rev-parse --short $GITHUB_SHA)" "$drift")
echo "👨💻 display plan"
echo -e "$tf_plan"
echo ""
echo "👨💻 display drift"
echo -e "$drift"
echo ""
echo 'plan-details<<EOF' >> $GITHUB_OUTPUT
echo "$message" >> $GITHUB_OUTPUT
echo 'EOF' >> $GITHUB_OUTPUT