forked from sue445/terraform-aws-template
-
Notifications
You must be signed in to change notification settings - Fork 0
144 lines (116 loc) · 4.08 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
name: terraform
env:
# c.f. https://github.com/hashicorp/terraform/blob/main/CHANGELOG.md
TERRAFORM_VERSION: 1.2.8 # TODO: Edit here
GITHUB_OIDC_PROVIDER_ROLE: arn:aws:iam::0000000000000:role/github-oidc-role # TODO: Edit here
AWS_REGION: "REGION_OF_CLOUD_FORMATION" # 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
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
role-to-assume: ${{ env.GITHUB_OIDC_PROVIDER_ROLE }}
aws-region: ${{ env.AWS_REGION }}
- 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, manually 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 }}