-
Notifications
You must be signed in to change notification settings - Fork 0
158 lines (154 loc) Β· 5.9 KB
/
Copy pathopentofu-module-ci.yml
File metadata and controls
158 lines (154 loc) Β· 5.9 KB
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
152
153
154
155
156
157
158
---
name: OpenTofu module CI (reusable)
on: # yamllint disable-line rule:truthy
workflow_call:
inputs:
tofu_version_file:
description: Path to OpenTofu version file in the caller repository
type: string
default: .opentofu-version
terraform_docs_version:
description: terraform-docs GitHub release tag to install
type: string
default: v0.21.0
permissions:
contents: read
jobs:
pre-commit:
name: Pre-commit
runs-on: ubuntu-latest
env:
TOFU_VERSION_FILE: ${{ inputs.tofu_version_file }}
steps:
- name: Checkout repository
uses: actions/checkout@v6
- name: Setup Python
uses: actions/setup-python@v6
- name: Setup TFLint
uses: terraform-linters/setup-tflint@v6
- name: Setup Terraform-Docs
uses: jaxxstorm/action-install-gh-release@v2.1.0
with:
repo: terraform-docs/terraform-docs
tag: ${{ inputs.terraform_docs_version }}
- name: Cache pre-commit
uses: actions/cache@v5
with:
path: ~/.cache/pre-commit
key: pre-commit-${{ runner.os }}-${{ hashFiles('.pre-commit-config.yaml') }}
restore-keys: |
pre-commit-${{ runner.os }}-
- name: Install pre-commit
run: pip install pre-commit
- name: Setup OpenTofu
uses: opentofu/setup-opentofu@v2
with:
tofu_version_file: ${{ inputs.tofu_version_file }}
tofu_wrapper: false
- name: Run pre-commit
id: pre_commit
shell: bash
run: |
set -euo pipefail
mkdir -p "${RUNNER_TEMP}/ci-summary"
set -o pipefail
set +e
pre-commit run --all-files 2>&1 | tee "${RUNNER_TEMP}/ci-summary/pre-commit.log"
ec=${PIPESTATUS[0]}
set -e
echo "${ec}" > "${RUNNER_TEMP}/ci-summary/pre-commit.exit"
exit "${ec}"
- name: Job summary
if: always()
shell: bash
env:
TOFU_VERSION_FILE: ${{ inputs.tofu_version_file }}
run: |
set -euo pipefail
SUMMARY_DIR="${RUNNER_TEMP}/ci-summary"
EXIT_FILE="${SUMMARY_DIR}/pre-commit.exit"
LOG_FILE="${SUMMARY_DIR}/pre-commit.log"
if [[ -f "${EXIT_FILE}" ]]; then
PRE_COMMIT_EXIT="$(tr -d ' \n\r' < "${EXIT_FILE}")"
else
PRE_COMMIT_EXIT=""
fi
{
RUN_URL="${{ github.server_url }}/${{ github.repository }}"
RUN_URL="${RUN_URL}/actions/runs/${{ github.run_id }}"
echo "## π§ OpenTofu CI: Pre-commit"
echo
echo "### π Run context"
echo
echo "| | |"
echo "|--|--|"
echo "| Event | \`${{ github.event_name }}\` |"
echo "| Ref | \`${{ github.ref }}\` |"
echo "| SHA | \`${{ github.sha }}\` |"
echo "| Workflow run | [${{ github.run_id }}](${RUN_URL}) |"
echo
echo "### π οΈ Toolchain (this runner)"
echo
echo "| Tool | Version / source |"
echo "|------|------------------|"
if command -v tofu >/dev/null 2>&1; then
printf "| OpenTofu | \`%s\` |\n" "$(tofu version 2>/dev/null | head -n1 | tr -d '\r')"
fi
if command -v python3 >/dev/null 2>&1; then
printf "| Python | \`%s\` |\n" "$(python3 --version 2>&1 | tr -d '\r')"
fi
if command -v pre-commit >/dev/null 2>&1; then
printf "| pre-commit | \`%s\` |\n" "$(pre-commit --version 2>&1 | tr -d '\r')"
fi
if command -v terraform-docs >/dev/null 2>&1; then
printf "| terraform-docs | \`%s\` |\n" "$(terraform-docs version 2>&1 | tr -d '\r' | head -n1)"
fi
if command -v tflint >/dev/null 2>&1; then
printf "| tflint | \`%s\` |\n" "$(tflint --version 2>&1 | tr -d '\r' | head -n1)"
fi
if [[ -f "${TOFU_VERSION_FILE}" ]]; then
printf "| Pinned OpenTofu (file \`%s\`) | \`%s\` |\n" "${TOFU_VERSION_FILE}" "$(tr -d '\n\r' < "${TOFU_VERSION_FILE}")"
fi
echo
echo "### π§ͺ Pre-commit result"
echo
if [[ "${PRE_COMMIT_EXIT}" == "0" ]]; then
echo "Status: **β
passed** (exit 0)."
elif [[ -n "${PRE_COMMIT_EXIT}" ]]; then
echo "Status: **β failed** (exit ${PRE_COMMIT_EXIT})."
else
echo "Status: **β unknown** (no exit file β see Run pre-commit step log)."
fi
echo
echo "Command: \`pre-commit run --all-files\` β hooks from "
echo "[\`.pre-commit-config.yaml\`](.pre-commit-config.yaml) (\`commit-msg\` not run in CI)."
echo
if [[ -f "${LOG_FILE}" ]]; then
echo "<details>"
echo "<summary>π Full pre-commit output</summary>"
echo
echo "\`\`\`text"
# Cap size so the summary stays well under GitHub limits (~1 MiB)
if [[ "$(wc -c < "${LOG_FILE}")" -gt 61440 ]]; then
head -n 400 "${LOG_FILE}"
echo ""
echo "β¦ truncated (first 400 lines; full output is in the Run pre-commit step log)"
else
cat "${LOG_FILE}"
fi
echo "\`\`\`"
echo
echo "</details>"
else
echo "_No pre-commit log file was written (see the Run pre-commit step)._"
fi
echo
echo "### π» If checks failed locally"
echo
echo "\`\`\`bash"
echo "pip install pre-commit"
echo "pre-commit install"
echo "pre-commit install --hook-type commit-msg"
echo "pre-commit run --all-files"
echo "\`\`\`"
} >> "${GITHUB_STEP_SUMMARY}"