-
Notifications
You must be signed in to change notification settings - Fork 0
81 lines (72 loc) · 2.77 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
# This workflow runs on `pull_request`.
#
# `Terraform` job uses `checkout` to download the content of the repository. It installs the latest version of Terraform CLI
# It uses the GitHub Action `GitHubAction_terraform-fmt` to run `terraform fmt`. If change are made to the code, the code is push
# to the branch.
#
# Documentation
# - https://github.com/actions/checkout
# - https://github.com/ConseilsTI/GitHubAction-TerraformFormat
#
name: Terraform
on: # yamllint disable-line rule:truthy
pull_request:
branches:
- '*' # matches every branch that doesn't contain a '/'
- '*/*' # matches every branch containing a single '/'
- '**' # matches every branch
permissions:
contents: write
pull-requests: write
jobs:
terraform:
name: Terraform
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Branch
id: branch
run: echo "branch=${GITHUB_HEAD_REF}" >> "$GITHUB_OUTPUT"
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ steps.branch.outputs.branch }}
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_wrapper: false
- name: Terraform Format
id: fmt
uses: ConseilsTI/GitHubAction-TerraformFormat@v1.0.3
with:
check: false
recursive: true
comment: true
- name: Git Push
if: ${{ steps.fmt.outputs.exitcode == 3 }}
run: |
echo "INFO | Pushing file(s) to the repository."
{
git config user.name "GitHub Action"
git config user.email "notifications@github.com"
git add -A
git commit -m "Automated Terraform Formatting"
git push -u origin ${{ steps.branch.outputs.branch }}
} ||
{
echo "ERROR | Unable to push file(s) to the repository."
}
echo "INFO | Adding comment to pull request."
accept_header="Accept: application/vnd.github.v3+json"
auth_header="Authorization: token ${GITHUB_TOKEN}"
content_header="Content-Type: application/json"
pr_comments_url=$(jq -r ".pull_request.comments_url" "${GITHUB_EVENT_PATH}")
body="Terraform file(s) has been formatted and pushed to the repository.<br/>You need to update your local copy of the repository."
pr_payload=$(echo '{}' | jq --arg body "${body}" '.body = $body')
{
curl -sS -X POST -H "${auth_header}" -H "${accept_header}" -H "${content_header}" -d "${pr_payload}" -L "${pr_comments_url}" > /dev/null
} ||
{
echo "ERROR | Unable to add comment to pull request."
}