Skip to content

Commit ab37e4c

Browse files
committed
ci: refactor terraform validation
1 parent 26ad5c1 commit ab37e4c

File tree

9 files changed

+133
-163
lines changed

9 files changed

+133
-163
lines changed

.github/workflows/terraform.yml

Lines changed: 28 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -3,180 +3,46 @@ on:
33
push:
44
branches:
55
- main
6+
- npalm/*
67
pull_request:
7-
paths: ["**/*.tf", "**/*.hcl", ".github/workflows/terraform.yml"]
8+
paths: ["**/*.tf", "**/*.hcl", ".github/workflows/terraform.yml", ".utils/**"]
9+
paths-ignore: ["lambdas/**"]
810

911
permissions:
1012
contents: read
1113
pull-requests: write
1214

13-
env:
14-
AWS_REGION: eu-west-1
1515
jobs:
16-
verify_module:
17-
name: Verify module
18-
strategy:
19-
matrix:
20-
terraform: [1.5.6, "latest"]
16+
dirs:
17+
name: Find modules
2118
runs-on: ubuntu-latest
22-
container:
23-
image: hashicorp/terraform:${{ matrix.terraform }}
24-
steps:
25-
- name: "Checkout"
26-
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
27-
- name: "Fake zip files" # Validate will fail if it cannot find the zip files
28-
run: |
29-
touch lambdas/functions/webhook/webhook.zip
30-
touch lambdas/functions/control-plane/runners.zip
31-
touch lambdas/functions/gh-agent-syncer/runner-binaries-syncer.zip
32-
touch lambdas/functions/ami-housekeeper/ami-housekeeper.zip
33-
touch lambdas/functions/termination-watcher/termination-watcher.zip
34-
- name: terraform init
35-
run: terraform init -get -backend=false -input=false
36-
- if: contains(matrix.terraform, '1.5.')
37-
name: check terraform formatting
38-
run: terraform fmt -recursive -check=true -write=false
39-
- if: contains(matrix.terraform, 'latest') # check formatting for the latest release but avoid failing the build
40-
name: check terraform formatting
41-
run: terraform fmt -recursive -check=true -write=false
42-
continue-on-error: true
43-
- name: validate terraform
44-
run: terraform validate
45-
- if: contains(matrix.terraform, '1.5.')
46-
name: Fix for actions/cache on alpine
47-
run: apk add --no-cache tar
48-
continue-on-error: true
49-
- if: contains(matrix.terraform, '1.5.')
50-
uses: actions/cache@ab5e6d0c87105b4c9c2047343972218f562e4319 # v4.0.1
51-
name: Cache TFLint plugin dir
52-
with:
53-
path: ~/.tflint.d/plugins
54-
key: tflint-${{ hashFiles('.tflint.hcl') }}
55-
- if: contains(matrix.terraform, '1.5.')
56-
name: Setup TFLint
57-
uses: terraform-linters/setup-tflint@19a52fbac37dacb22a09518e4ef6ee234f2d4987 # v4.0.0
58-
with:
59-
github_token: ${{ secrets.GITHUB_TOKEN }}
60-
- if: contains(matrix.terraform, '1.5.')
61-
name: Run TFLint
62-
run: |
63-
tflint --init -c ${GITHUB_WORKSPACE}/.tflint.hcl
64-
tflint -c ${GITHUB_WORKSPACE}/.tflint.hcl --var-file ${GITHUB_WORKSPACE}/.github/lint/tflint.tfvars
65-
66-
verify_modules:
67-
name: Verify modules
68-
strategy:
69-
fail-fast: false
70-
matrix:
71-
terraform: [1.5.6, "latest"]
72-
module:
73-
[
74-
"ami-housekeeper",
75-
"download-lambda",
76-
"lambda",
77-
"multi-runner",
78-
"runner-binaries-syncer",
79-
"runners",
80-
"setup-iam-permissions",
81-
"ssm",
82-
"termination-watcher",
83-
"webhook",
84-
]
85-
defaults:
86-
run:
87-
working-directory: modules/${{ matrix.module }}
88-
runs-on: ubuntu-latest
89-
container:
90-
image: hashicorp/terraform:${{ matrix.terraform }}
9119
steps:
9220
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
93-
- name: terraform init
94-
run: terraform init -get -backend=false -input=false
95-
- if: contains(matrix.terraform, '1.3.')
96-
name: check terraform formatting
97-
run: terraform fmt -recursive -check=true -write=false
98-
- if: contains(matrix.terraform, 'latest') # check formatting for the latest release but avoid failing the build
99-
name: check terraform formatting
100-
run: terraform fmt -recursive -check=true -write=false
101-
continue-on-error: true
102-
- name: validate terraform
103-
run: terraform validate
104-
- if: contains(matrix.terraform, '1.3.')
105-
name: Fix for actions/cache on alpine
106-
run: apk add --no-cache tar
107-
continue-on-error: true
108-
- if: contains(matrix.terraform, '1.3.')
109-
uses: actions/cache@v4
110-
name: Cache TFLint plugin dir
111-
with:
112-
path: ~/.tflint.d/plugins
113-
key: tflint-${{ hashFiles('.tflint.hcl') }}
114-
- if: contains(matrix.terraform, '1.3.')
115-
name: Setup TFLint
116-
uses: terraform-linters/setup-tflint@v4
117-
with:
118-
github_token: ${{ secrets.GITHUB_TOKEN }}
119-
- if: contains(matrix.terraform, '1.3.')
120-
name: Run TFLint
121-
working-directory: ${{ github.workspace }}
21+
22+
- id: find
23+
# Ignore directories whose names start with .terraform or obsolete
24+
shell: bash
12225
run: |
123-
tflint --init -c ${GITHUB_WORKSPACE}/.tflint.hcl --chdir modules/${{ matrix.module }}
124-
tflint -f compact -c ${GITHUB_WORKSPACE}/.tflint.hcl --var-file ${GITHUB_WORKSPACE}/.github/lint/tflint.tfvars --chdir modules/${{ matrix.module }}
26+
source ./.utils/find.sh
27+
#modules=$(findAllTerraformDirs --ignores "examples")
28+
#modules=$(echo $modules | jq 'map(select(. | contains("./modules/multi-runner")))')
29+
#examples=$(findAllTerraformDirs --ignores "modules" --hide-root)
30+
modules=$(findAllTerraformDirs)
31+
echo "modules=$(echo $modules)" >> $GITHUB_OUTPUT
32+
echo "examples=$(echo $examples)" >> $GITHUB_OUTPUT
33+
outputs:
34+
tf_modules: ${{ steps.find.outputs.modules }}
35+
tf_examples: ${{ steps.find.outputs.examples }}
12536

126-
verify_examples:
127-
name: Verify examples
37+
verify_all:
38+
uses: ./.github/workflows/terraform-validate-template.yml
39+
name: Verify module ${{ matrix.module }}
40+
needs: dirs
41+
with:
42+
terraform_version: ${{ matrix.terraform }}
43+
module: ${{ matrix.module }}
12844
strategy:
12945
fail-fast: false
13046
matrix:
131-
terraform: [1.5.6, "latest"]
132-
example:
133-
[
134-
"default",
135-
"ubuntu",
136-
"prebuilt",
137-
"arm64",
138-
"ephemeral",
139-
"termination-watcher",
140-
"windows",
141-
"multi-runner",
142-
]
143-
defaults:
144-
run:
145-
working-directory: examples/${{ matrix.example }}
146-
runs-on: ubuntu-latest
147-
container:
148-
image: hashicorp/terraform:${{ matrix.terraform }}
149-
steps:
150-
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
151-
- name: terraform init
152-
run: terraform init -get -backend=false -input=false
153-
- if: contains(matrix.terraform, '1.5.')
154-
name: check terraform formatting
155-
run: terraform fmt -recursive -check=true -write=false
156-
- if: contains(matrix.terraform, 'latest') # check formatting for the latest release but avoid failing the build
157-
name: check terraform formatting
158-
run: terraform fmt -recursive -check=true -write=false
159-
continue-on-error: true
160-
- name: validate terraform
161-
run: terraform validate
162-
- if: contains(matrix.terraform, '1.5.')
163-
name: Fix for actions/cache on alpine
164-
run: apk add --no-cache tar
165-
continue-on-error: true
166-
- if: contains(matrix.terraform, '1.5.')
167-
uses: actions/cache@v4
168-
name: Cache TFLint plugin dir
169-
with:
170-
path: ~/.tflint.d/plugins
171-
key: tflint-${{ hashFiles('.tflint.hcl') }}
172-
- if: contains(matrix.terraform, '1.5.')
173-
name: Setup TFLint
174-
uses: terraform-linters/setup-tflint@v4
175-
with:
176-
github_token: ${{ secrets.GITHUB_TOKEN }}
177-
- if: contains(matrix.terraform, '1.5.')
178-
name: Run TFLint
179-
working-directory: ${{ github.workspace }}
180-
run: |
181-
tflint --init -c ${GITHUB_WORKSPACE}/.tflint.hcl --chdir modules/${{ matrix.module }}
182-
tflint -f compact -c ${GITHUB_WORKSPACE}/.tflint.hcl --var-file ${GITHUB_WORKSPACE}/.github/lint/tflint.tfvars --chdir examples/${{ matrix.example }}
47+
terraform: ["1.5", "latest"]
48+
module: ${{ fromJson(needs.dirs.outputs.tf_modules) }}

.tflint.hcl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@ config {
33
call_module_type = "local"
44
}
55

6+
# plugin "terraform" {
7+
# enabled = true
8+
# version = "0.10.0"
9+
# source = "github.com/terraform-linters/tflint-ruleset-terraform"
10+
# }
11+
612
plugin "aws" {
713
enabled = true
8-
version = "0.30.0"
14+
version = "0.34.0"
915
source = "github.com/terraform-linters/tflint-ruleset-aws"
1016
}
1117

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

.utils/find.sh

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
function findAllTerraformDirs() {
2+
local ignores=""
3+
local hide_root=false
4+
local format="json"
5+
local args=("$@")
6+
7+
# Parse arguments
8+
while [[ $# -gt 0 ]]; do
9+
case $1 in
10+
--ignores)
11+
shift
12+
while [[ $# -gt 0 && $1 != --* ]]; do
13+
ignores="$ignores -o -name '$1'"
14+
shift
15+
done
16+
;;
17+
--hide-root)
18+
hide_root=true
19+
shift
20+
;;
21+
--format)
22+
shift
23+
format=$1
24+
shift
25+
;;
26+
*)
27+
shift
28+
;;
29+
esac
30+
done
31+
32+
33+
find_command="find . -type d \( -name '.terraform*' -o -name 'deprecated*' $ignores \) -prune -o \
34+
-name '*.tf' \
35+
-not -path '*/.terraform/*' \
36+
-exec dirname {} \; | \
37+
sort | \
38+
uniq"
39+
40+
41+
if [ "$format" = "json" ]; then
42+
jq_filter='split("\n") | map(select(. != ""))'
43+
if [ "$hide_root" = true ]; then
44+
jq_filter='split("\n") | map(select(. != "" and . != "."))'
45+
fi
46+
find_command="$find_command | jq --raw-input --slurp '$jq_filter'"
47+
elif [ "$format" = "plain" ]; then
48+
if [ "$hide_root" = true ]; then
49+
find_command="$find_command | grep -v '^\\.$'"
50+
fi
51+
fi
52+
53+
eval $find_command
54+
}
55+
56+
findLambdaFunctions() {
57+
# lambdas are located in lambdas/functions
58+
# only the first level director should be resulted
59+
# output format plain or json, json is default, not other options needed
60+
61+
local format="json"
62+
local args=("$@")
63+
while [[ $# -gt 0 ]]; do
64+
case $1 in
65+
--format)
66+
shift
67+
format=$1
68+
shift
69+
;;
70+
*)
71+
shift
72+
;;
73+
esac
74+
done
75+
76+
find_command="find lambdas/functions -maxdepth 1 -type d | \
77+
sort | \
78+
uniq"
79+
80+
if [ "$format" = "json" ]; then
81+
jq_filter='split("\n") | map(select(. != ""))'
82+
find_command="$find_command | jq --raw-input --slurp '$jq_filter'"
83+
fi
84+
85+
eval $find_command
86+
}
File renamed without changes.

.utils/terraform.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#source "$(dirname "${BASH_SOURCE[0]}")/find.sh"
2+
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/find.sh"
3+
4+
generateDummyLambdaZip() {
5+
lambdaDirs=($(findLambdaFunctions --format plain))
6+
echo ${lambdaDirs[@]}
7+
echo ----
8+
for lambdaDir in "${lambdaDirs[@]}"; do
9+
echo Generating dummy zip for $lambdaDir/$(basename $lambdaDir).zip
10+
touch "$lambdaDir/$(basename $lambdaDir).zip"
11+
done
12+
}

0 commit comments

Comments
 (0)