generated from 3ware/workflows
-
Notifications
You must be signed in to change notification settings - Fork 1
151 lines (131 loc) · 5.71 KB
/
terraform-ci.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
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
149
150
151
name: Terraform CI
run-name: ${{ github.event_name == 'merge_group' && github.event.merge_group.head_commit.message || ''}}
on:
pull_request:
types: [opened, synchronize]
branches: [main]
merge_group:
types: [checks_requested]
permissions: {}
concurrency:
group: ${{ github.workflow }}-${{ github.repository }}
cancel-in-progress: true
defaults:
run:
shell: bash
jobs:
targets:
name: Terraform Targets
runs-on: ubuntu-latest
permissions:
contents: read
outputs:
targets: ${{ steps.directories.outputs.all_changed_files }}
steps:
- name: Checkout repository
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
fetch-depth: 0
persist-credentials: false
- name: Check changed directories
id: directories
uses: tj-actions/changed-files@c3a1bb2c992d77180ae65be6ae6c166cf40f857c # v45.0.3
with:
dir_names: true
dir_names_max_depth: 3
files: terraform/**
matrix: true
terraform-deploy:
needs: [targets]
if: ${{ needs.targets.outputs.targets != '[]' }}
name: Terraform Deploy
runs-on: ubuntu-latest
permissions:
actions: read # Required to identify workflow run.
checks: write # Required to add status summary.
contents: read # Required to checkout repository.
id-token: write # Require for OIDC.
pull-requests: write # Required to add comment and label.
strategy:
fail-fast: true
max-parallel: 1
matrix:
targets: ${{ fromJson(needs.targets.outputs.targets) }}
environment: ${{ contains(matrix.targets, 'production') && 'production' || 'development' }}
env:
TF_TOKEN_APP_TERRAFORM_IO: ${{ secrets.TF_TOKEN_APP_TERRAFORM_IO }}
steps:
- name: Checkout repository
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
persist-credentials: false
# This is required because the 'environment' context does not exist, so cannot be referenced
# by the steps that need it
- name: Set deployment environment as an environment variable
run: echo "ENVIRONMENT=${{ contains(matrix.targets, 'production') && 'production' || 'development' }}" >> $GITHUB_ENV
# AWS Credentials required for tflint deep check
- name: Configure AWS credentials via OIDC
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
with:
aws-region: us-east-1
mask-aws-account-id: true
role-to-assume: ${{ secrets[format('GHA_3WARE_OIDC_{0}', env.ENVIRONMENT)] }}
role-session-name: ${{ github.event_name == 'merge_group' && 'aws-net-sec-terraform-apply' || 'aws-net-sec-terraform-plan' }}
- name: Terraform Fmt + Init + Validate
if: ${{ github.event_name == 'pull_request' }}
id: tf
uses: devsectop/tf-via-pr@a917bd222a6a780f25d2c5cd1942f6b2c2f16a7a # v12.0.5
with:
working-directory: ${{ matrix.targets }}
arg-lock: false
format: true
validate: true
- name: Cache TFLint plugin directory
if: ${{ github.event_name == 'pull_request' }}
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2
with:
path: .trunk/plugins/
key: ${{ runner.os }}-${{ github.repository }}-tflint-${{ hashFiles('.trunk/configs/.tflint_ci.hcl') }}
# Install TFLint; required to download plugins
- name: Install TFLint
if: ${{ github.event_name == 'pull_request' }}
uses: terraform-linters/setup-tflint@15ef44638cb215296e7ba9e7a41f20b5b06f4784 # v4.0.0
with:
tflint_version: v0.53.0
tflint_wrapper: true
# Run TFLint using the configuration file in the trunk directory
- name: Run TFLint
if: ${{ github.event_name == 'pull_request' }}
id: tflint
run: |
tflint -chdir=${{ matrix.targets }} --config=$GITHUB_WORKSPACE/.trunk/configs/.tflint_ci.hcl --init
tflint -chdir=${{ matrix.targets }} --config=$GITHUB_WORKSPACE/.trunk/configs/.tflint_ci.hcl --format compact
continue-on-error: true
# Add PR comment then exit workflow if TFLint detects a violation
- name: Add PR comment on TFLint error
if: ${{ steps.tflint.outputs.exitcode != 0 }}
env:
GH_TOKEN: ${{ github.token }} # https://cli.github.com/manual/gh_auth_login
run: |
# Compose TFLint output.
tflint="
<details><summary>❌ TFLint error.</summary>
\`\`\`hcl
${{ steps.tflint.outputs.stderr || steps.tflint.outputs.stdout }}
\`\`\`
</details>"
# Get body of PR comment from tf step output.
comment=$(gh api /repos/{owner}/{repo}/issues/comments/${{ steps.tf.outputs.comment-id }} --method GET --jq '.body')
# Replace placeholder with TFLint output.
comment=$(echo "$comment" | sed "s|<!-- placeholder-2 -->|$tflint|")
# Update PR comment combined with TFLint output.
gh api /repos/{owner}/{repo}/issues/comments/${{ steps.tf.outputs.comment-id }} --method PATCH --field body="$comment"
# Exit workflow due to TFLint error.
exit 1
- name: Terraform ${{ format('{0}', github.event_name == 'merge_group' && 'Apply' || 'Plan') }}
uses: devsectop/tf-via-pr@a917bd222a6a780f25d2c5cd1942f6b2c2f16a7a # v12.0.5
with:
working-directory: ${{ matrix.targets }}
command: ${{ github.event_name == 'merge_group' && 'apply' || 'plan' }}
arg-lock: ${{ github.event_name == 'merge_group' }}
plan-encrypt: ${{ secrets.PGP_SECRET_SIGNING_PASSPHRASE }}