-
Notifications
You must be signed in to change notification settings - Fork 16
96 lines (82 loc) · 2.63 KB
/
terraform-report.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
name: Terraform Validation
on:
pull_request:
paths:
- 'iac/*'
jobs:
targets:
runs-on: ubuntu-latest
outputs:
paths: ${{ steps.ls.outputs.paths }}
steps:
- uses: actions/checkout@v4
- id: ls
run: echo "paths=$(ls -d iac/*/* | jq --raw-input --slurp --compact-output 'split("\n")[:-1]')" >> ${GITHUB_OUTPUT}
validate:
runs-on: ubuntu-latest
needs: targets
strategy:
matrix:
path: ${{ fromJson(needs.targets.outputs.paths) }}
steps:
- uses: actions/checkout@v4
- name: Terraform Formatting
uses: dflook/terraform-fmt@v1
with:
path: ${{ matrix.path }}
- name: Terraform Validation
uses: dflook/terraform-validate@v1
with:
path: ${{ matrix.path }}
- name: Terraform Plan
uses: dflook/terraform-plan@v1
with:
path: ${{ matrix.path }}
changed:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
submodules: true
- uses: 'google-github-actions/auth@v2'
with:
credentials_json: '${{ secrets.GCP_SA_KEY }}'
- uses: google-github-actions/setup-gcloud@v2
- uses: hashicorp/setup-terraform@v2
with:
terraform_version: 1.1.7
terraform_wrapper: false
- name: terraform-report
shell: bash
env:
CONTENT_ROOT : ${{ github.workspace }}/iac
ENGINE_ROOT : ${{ github.workspace }}/iac/.engine
GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
GITHUB_PR_NUMBER : ${{ github.event.number }}
GITHUB_BASE_BRANCH : ${{ github.event.pull_request.base.ref }}
run: |
if [[ $GITHUB_BASE_BRANCH == main ]]; then
export INFRA_TARGET_NAME=prod
else
export INFRA_TARGET_NAME=${GITHUB_BASE_BRANCH#targets/}
if [[ $INFRA_TARGET_NAME == prod ]]; then
echo "fatal: prod changes must be merged to main branch" >&2
exit 1
fi
fi
. "$ENGINE_ROOT"/lib/sh/terraform-report.sh
if [[ ${#target_endpoint_changes[*]} == 0 ]]; then
exit 0
fi
gh pr comment $GITHUB_PR_NUMBER --body-file - <<EOF
## Terraform Deployment Preview
$(
for endpoint in "${!target_endpoint_changes[@]}"; do
printf '\n### %s\n\n' "$endpoint"
printf '```hcl\n'
printf '%s\n' "${target_endpoint_changes[$endpoint]}"
printf '```\n'
done
)
EOF