forked from sue445/terraform-gcp-template
-
Notifications
You must be signed in to change notification settings - Fork 0
148 lines (119 loc) · 4.21 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
name: terraform
env:
# c.f. https://github.com/hashicorp/terraform/blob/main/CHANGELOG.md
TERRAFORM_VERSION: 1.2.8 # TODO Edit here
# e.g. projects/123456789/locations/global/workloadIdentityPools/github-actions/providers/github-actions
WORKLOAD_IDENTITY_PROVIDER: "" # TODO: Edit here
# e.g. terraform@GCP_PROJECT_ID.iam.gserviceaccount.com
SERVICE_ACCOUNT_EMAIL: "" # TODO: Edit here
permissions:
contents: read
id-token: write
issues: write
pull-requests: write
on:
push:
branches:
- main
paths-ignore:
- "**.md"
pull_request:
types:
- opened
- synchronize
- reopened
paths-ignore:
- "**.md"
workflow_dispatch:
jobs:
terraform:
runs-on: ubuntu-latest
concurrency: terraform-tfstate
steps:
- uses: actions/checkout@v3
- id: auth
name: Authenticate to Google Cloud
uses: google-github-actions/auth@v0
with:
workload_identity_provider: ${{ env.WORKLOAD_IDENTITY_PROVIDER }}
service_account: ${{ env.SERVICE_ACCOUNT_EMAIL }}
- uses: hashicorp/setup-terraform@v2
with:
terraform_version: ${{ env.TERRAFORM_VERSION }}
- run: terraform init -input=false
- name: terraform fmt
run: |
set +e
terraform fmt -recursive -check
ret=$?
set -e
if [ $ret -ne 0 ]; then
echo '[ERROR] Run `terraform fmt -recursive` or fix followings'
terraform fmt -recursive
git --no-pager diff
fi
exit $ret
- name: tflint
uses: reviewdog/action-tflint@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
- name: terraform plan
id: plan
run: terraform plan -input=false -no-color
- name: Post terraform plan report to PullRequest comment
uses: actions/github-script@v6
if: github.event_name == 'pull_request'
env:
PLAN: "terraform\n${{ steps.plan.outputs.stdout }}"
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const ci_url = "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}";
const output = `#### Terraform Plan 📖\`${{ steps.plan.outcome }}\`
<details><summary>Show Plan</summary>
\`\`\`\n
${process.env.PLAN}
\`\`\`
</details>
*Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Workflow: [\`${{ github.workflow }}\`](${ ci_url })*`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
- name: Post terraform plan report to Job Summaries
uses: actions/github-script@v6
env:
PLAN: "terraform\n${{ steps.plan.outputs.stdout }}"
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const ci_url = "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}";
const output = `#### Terraform Plan 📖\`${{ steps.plan.outcome }}\`
<details><summary>Show Plan</summary>
\`\`\`\n
${process.env.PLAN}
\`\`\`
</details>
*Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Workflow: [\`${{ github.workflow }}\`](${ ci_url })*`;
await core.summary
.addHeading('Terraform plan report')
.addRaw("\n" + output)
.write()
- name: terraform apply (main push, manual running)
run: |
terraform apply -input=false -auto-approve
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
if: github.ref == 'refs/heads/main' || github.event_name == 'workflow_dispatch'
- name: Slack Notification
uses: lazy-actions/slatify@master
if: always()
continue-on-error: true
with:
job_name: terraform
type: ${{ job.status }}
icon_emoji: ":octocat:"
url: ${{ secrets.SLACK_WEBHOOK }}
token: ${{ secrets.GITHUB_TOKEN }}