Repository navigation
Expand file tree
/
Copy pathaction.yml
More file actions
237 lines (219 loc) · 9.02 KB
/
Copy pathaction.yml
File metadata and controls
237 lines (219 loc) · 9.02 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
name: docker-lint by devblac
description: Lint Dockerfiles for anti-patterns, inefficiencies and security issues; annotates pull requests and outputs SARIF
author: devblac
branding:
icon: check-square
color: blue
inputs:
dockerfile:
description: >-
What to lint, one entry per line: a Dockerfile, a directory searched
recursively for Dockerfiles, or a glob such as "services/**/Dockerfile".
Paths are relative to the repository root.
default: Dockerfile
config:
description: >-
Configuration file. Defaults to the .docker-lint.yaml at the repository
root, when there is one.
default: ""
version:
description: >-
docker-lint release to use. By default, the release matching the tag the
action is used at (uses: devblac/docker-lint@v1.2.3 installs v1.2.3, and
@v1 the newest v1.x.y), or the latest release when the action is used
from a branch or commit. Also accepts "latest", a tag such as "v0.1.1", a
line such as "v0" or "v0.1", or "source" to build the action's own
checkout (needs Go; used to test the action).
default: ""
format:
description: >-
Output format for the job log: "github" (annotations on the pull request
diff), "text", "json" or "sarif".
default: github
strict:
description: Fail on warnings, not only on errors.
default: "false"
quiet:
description: Hide informational findings.
default: "false"
ignore:
description: Comma-separated rule IDs to ignore, e.g. "DKL103,DKL404".
default: ""
sarif-file:
description: >-
Also write a SARIF report to this path. Defaults to docker-lint.sarif when
upload-sarif is enabled.
default: ""
upload-sarif:
description: >-
Upload the SARIF report to GitHub code scanning. Needs the
"security-events: write" permission, and GitHub Advanced Security on
private repositories.
default: "false"
github-token:
description: Token used to download the docker-lint release.
default: ${{ github.token }}
outputs:
exit-code:
description: "docker-lint exit code: 0 clean, 1 lint errors (or warnings with strict), 2 fatal error."
value: ${{ steps.lint.outputs.exit-code }}
sarif-file:
description: Path of the SARIF report, when one was written.
value: ${{ steps.lint.outputs.sarif-file }}
runs:
using: composite
steps:
# Inputs reach the scripts through env, never ${{ }} interpolation, so a
# crafted value cannot inject shell commands.
- name: Install docker-lint
shell: bash
env:
VERSION: ${{ inputs.version }}
GH_TOKEN: ${{ inputs.github-token }}
ACTION_PATH: ${{ github.action_path }}
ACTION_REF: ${{ github.action_ref }}
run: |
set -euo pipefail
case "$RUNNER_OS" in
Linux) os=linux ;;
macOS) os=darwin ;;
Windows) os=windows ;;
*) echo "::error::docker-lint: unsupported runner OS $RUNNER_OS"; exit 1 ;;
esac
case "$RUNNER_ARCH" in
X64) arch=amd64 ;;
ARM64) arch=arm64 ;;
*) echo "::error::docker-lint: unsupported runner architecture $RUNNER_ARCH"; exit 1 ;;
esac
exe=""
if [ "$os" = windows ]; then exe=".exe"; fi
bin_dir="$RUNNER_TEMP/docker-lint-bin"
mkdir -p "$bin_dir"
# Pinning the action pins the linter: by default, install the release
# whose tag the action is used at. A major or minor tag (v1, v1.2)
# installs the newest release in that line. The runner checks the action
# out under .../_actions/<owner>/<repo>/<ref>, which also gives the ref
# when github.action_ref is empty, as it is in some nested composite
# actions.
semver='^v[0-9]+\.[0-9]+\.[0-9]+([-+][0-9A-Za-z.-]+)?$'
line='^v[0-9]+(\.[0-9]+)?$'
if [ -z "$VERSION" ]; then
ref="${ACTION_REF:-$(basename "$ACTION_PATH")}"
if printf '%s' "$ref" | grep -Eq "$semver|$line"; then
VERSION="$ref"
else
VERSION=latest
fi
fi
echo "docker-lint: installing $VERSION"
if [ "$VERSION" = source ]; then
(cd "$ACTION_PATH" && go build -o "$bin_dir/docker-lint$exe" ./cmd/docker-lint)
else
repo=devblac/docker-lint
tag="$VERSION"
if [ "$tag" = latest ]; then
tag="$(gh release view --repo "$repo" --json tagName --jq .tagName)"
elif printf '%s' "$tag" | grep -Eq "$line"; then
# The prefix passed the check above, so it is safe inside the filter.
tag="$(gh release list --repo "$repo" --exclude-drafts --exclude-pre-releases \
--limit 1000 --json tagName --jq "[.[].tagName | select(startswith(\"$tag.\"))
| select(test(\"^v[0-9]+\\\\.[0-9]+\\\\.[0-9]+\$\"))]
| sort_by(ltrimstr(\"v\") | split(\".\") | map(tonumber)) | last // empty")"
if [ -z "$tag" ]; then
echo "::error::docker-lint: no published release matches $VERSION"
exit 1
fi
echo "docker-lint: $VERSION resolves to $tag"
fi
ext=tar.gz
if [ "$os" = windows ]; then ext=zip; fi
archive="docker-lint_${tag#v}_${os}_${arch}.${ext}"
download="$RUNNER_TEMP/docker-lint-download"
rm -rf "$download"
mkdir -p "$download"
gh release download "$tag" --repo "$repo" --dir "$download" \
--pattern "$archive" --pattern checksums.txt
cd "$download"
grep " ${archive}\$" checksums.txt > expected.sha256
if command -v sha256sum >/dev/null; then
sha256sum --check expected.sha256
else
shasum -a 256 --check expected.sha256
fi
if [ "$ext" = zip ]; then
if command -v unzip >/dev/null; then unzip -q -o "$archive"; else 7z x -y "$archive" >/dev/null; fi
else
tar -xzf "$archive"
fi
mv "docker-lint$exe" "$bin_dir/"
fi
echo "$bin_dir" >> "$GITHUB_PATH"
"$bin_dir/docker-lint$exe" --version
- name: Lint Dockerfile
id: lint
shell: bash
env:
DOCKERFILE: ${{ inputs.dockerfile }}
CONFIG: ${{ inputs.config }}
FORMAT: ${{ inputs.format }}
STRICT: ${{ inputs.strict }}
QUIET: ${{ inputs.quiet }}
IGNORE: ${{ inputs.ignore }}
SARIF_FILE: ${{ inputs.sarif-file }}
UPLOAD_SARIF: ${{ inputs.upload-sarif }}
run: |
# The runner starts bash with -e; failures are handled explicitly below.
set -uo pipefail
args=()
if [ "$STRICT" = true ]; then args+=(--strict); fi
if [ "$QUIET" = true ]; then args+=(--quiet); fi
if [ -n "$IGNORE" ]; then args+=(--ignore "$IGNORE"); fi
if [ -n "$CONFIG" ]; then args+=(--config "$CONFIG"); fi
# One path or pattern per line. Patterns stay quoted: docker-lint
# expands them itself, including ** on every runner OS.
paths=()
while IFS= read -r entry; do
entry="${entry#"${entry%%[![:space:]]*}"}"
entry="${entry%"${entry##*[![:space:]]}"}"
if [ -n "$entry" ]; then paths+=("$entry"); fi
done <<< "$DOCKERFILE"
if [ ${#paths[@]} -eq 0 ]; then
echo "::error::docker-lint: the dockerfile input is empty"
echo "exit-code=2" >> "$GITHUB_OUTPUT"
exit 0
fi
# Record the exit code instead of failing here, so the SARIF report is
# still written and uploaded when the lint finds errors.
code=0
docker-lint ${args[@]+"${args[@]}"} --format "$FORMAT" "${paths[@]}" || code=$?
echo "exit-code=$code" >> "$GITHUB_OUTPUT"
if [ "$UPLOAD_SARIF" = true ] && [ -z "$SARIF_FILE" ]; then
SARIF_FILE=docker-lint.sarif
fi
if [ -n "$SARIF_FILE" ]; then
# When no file can be analyzed nothing is written to stdout; an empty
# report would only make the upload fail with a confusing message.
docker-lint ${args[@]+"${args[@]}"} --format sarif "${paths[@]}" > "$SARIF_FILE" 2>/dev/null || true
if [ -s "$SARIF_FILE" ]; then
echo "sarif-file=$SARIF_FILE" >> "$GITHUB_OUTPUT"
else
rm -f "$SARIF_FILE"
echo "::warning::docker-lint: no SARIF report was written because no Dockerfile could be analyzed"
fi
fi
- name: Upload SARIF to code scanning
if: inputs.upload-sarif == 'true' && steps.lint.outputs.sarif-file != ''
uses: github/codeql-action/upload-sarif@v4
with:
sarif_file: ${{ steps.lint.outputs.sarif-file }}
category: docker-lint
- name: Report result
shell: bash
env:
EXIT_CODE: ${{ steps.lint.outputs.exit-code }}
run: |
case "$EXIT_CODE" in
0) ;;
1) echo "::error::docker-lint found errors (or warnings with strict enabled)"; exit 1 ;;
*) echo "::error::docker-lint could not analyze every Dockerfile (exit code $EXIT_CODE)"; exit "$EXIT_CODE" ;;
esac