Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions .github/workflows/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
name: Action

# Exercises the GitHub Action in this repository on every runner OS. It builds
# docker-lint from source ("version: source") because a pull request must be
# testable before any release exists.
on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
test:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout repository
uses: actions/checkout@v7

- name: Set up Go
uses: actions/setup-go@v7
with:
go-version-file: go.mod
cache: true

- name: Lint a clean Dockerfile
id: clean
uses: ./
with:
version: source
dockerfile: testdata/valid/minimal.Dockerfile
sarif-file: clean.sarif

- name: Check the clean run
shell: bash
env:
EXIT_CODE: ${{ steps.clean.outputs.exit-code }}
SARIF_FILE: ${{ steps.clean.outputs.sarif-file }}
run: |
set -euo pipefail
test "$EXIT_CODE" = 0
test "$SARIF_FILE" = clean.sarif
grep -q '"version": "2.1.0"' clean.sarif

- name: Lint a Dockerfile Docker cannot build
id: broken
continue-on-error: true
uses: ./
with:
version: source
dockerfile: testdata/invalid/no-from.Dockerfile
sarif-file: broken.sarif

- name: Check the failing run
shell: bash
env:
OUTCOME: ${{ steps.broken.outcome }}
EXIT_CODE: ${{ steps.broken.outputs.exit-code }}
run: |
set -euo pipefail
test "$OUTCOME" = failure
test "$EXIT_CODE" = 1
# The report is written even though the lint failed.
grep -q '"ruleId": "DKL001"' broken.sarif

- name: Lint a Dockerfile that does not parse
id: fatal
continue-on-error: true
uses: ./
with:
version: source
dockerfile: testdata/invalid/unknown-instruction.Dockerfile
sarif-file: fatal.sarif

- name: Check the fatal run
shell: bash
env:
OUTCOME: ${{ steps.fatal.outcome }}
EXIT_CODE: ${{ steps.fatal.outputs.exit-code }}
SARIF_FILE: ${{ steps.fatal.outputs.sarif-file }}
run: |
set -euo pipefail
test "$OUTCOME" = failure
test "$EXIT_CODE" = 2
# No empty report is left behind for an upload step to choke on.
test -z "$SARIF_FILE"
test ! -e fatal.sarif
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Release pipeline: prebuilt binaries for Linux, macOS and Windows (amd64 and
arm64) with checksums, built by GoReleaser from a tag into a draft release;
CI builds a snapshot release on every pull request
- `--format` flag selecting `text`, `json`, `sarif` or `github`; `--json` stays as
a shorthand for `--format json`
- SARIF 2.1.0 output for GitHub code scanning and other security dashboards,
validated against the official schema
- GitHub annotations output, which shows findings on pull request diffs in any
repository, without code scanning
- GitHub Action (`uses: devblac/docker-lint@<tag>`) that installs a
checksum-verified release binary, annotates pull requests and can upload
SARIF to code scanning; tested on Linux, macOS and Windows runners

### Changed
- **Breaking:** rule IDs move from `DLxxxx`, which collide with hadolint's IDs for
Expand Down
70 changes: 60 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ docker-lint [flags] [file]
|------|-------|-------------|
| `--help` | `-h` | Show help message |
| `--version` | `-v` | Show version information |
| `--json` | `-j` | Output findings as JSON |
| `--format <fmt>` | `-f` | Output format: `text` (default), `json`, `sarif`, `github` |
| `--json` | `-j` | Shorthand for `--format json` |
| `--quiet` | `-q` | Suppress informational messages (show only warnings and errors) |
| `--strict` | `-s` | Treat warnings as errors (exit code 1 if any warnings) |
| `--ignore <rules>` | | Comma-separated list of rule IDs to ignore |
Expand Down Expand Up @@ -175,11 +176,11 @@ Human-readable output with file path, line number, severity, rule ID, and messag
```
Dockerfile:1:1: [warning] DKL102: Using 'latest' tag for image 'ubuntu' is not recommended
Suggestion: Pin to a specific version like 'ubuntu:<version>' for reproducible builds
Dockerfile:3:1: [warning] DKL202: Found 2 consecutive RUN instructions that could be combined
Dockerfile:3:1: [info] DKL202: Found 2 consecutive RUN instructions that could be combined
Suggestion: Combine RUN instructions using '&&' to reduce layers
```

### JSON (`--json`)
### JSON (`--format json`)

Machine-readable JSON output for CI/CD integration:

Expand All @@ -205,22 +206,71 @@ Machine-readable JSON output for CI/CD integration:
}
```

### SARIF (`--format sarif`)

[SARIF 2.1.0](https://docs.oasis-open.org/sarif/sarif/v2.1.0/sarif-v2.1.0.html), the format GitHub code
scanning and other security dashboards ingest. The report carries every rule's metadata, so findings show
up with their name, description and severity. The exit code is unchanged, so CI still fails on errors while
the report is uploaded.

### GitHub annotations (`--format github`)

GitHub Actions workflow commands, which GitHub renders as annotations on the changed lines of a pull
request. They need no code scanning, so they work in private repositories without GitHub Advanced Security:

```
::warning file=Dockerfile,line=1,col=1,title=DKL102::Using 'latest' tag for image 'ubuntu' is not recommended
```

## CI/CD Integration

The repository's CI workflow runs `go test ./... -cover`. Coverage uploads to Codecov are attempted only when a `CODECOV_TOKEN` secret is configured; otherwise the upload step is skipped while tests still gate the build.

### GitHub Actions

The docker-lint action installs a release binary (checksum-verified), lints a Dockerfile and annotates the
pull request diff with the findings:

```yaml
- uses: actions/checkout@v7
- uses: devblac/docker-lint@v0.1.0
with:
dockerfile: Dockerfile
```

To also see findings in the repository's **Security** tab, upload SARIF to code scanning. This needs the
`security-events: write` permission, and GitHub Advanced Security on private repositories:

```yaml
- name: Lint Dockerfile
run: |
docker-lint --strict --json Dockerfile > lint-results.json
if [ $? -ne 0 ]; then
cat lint-results.json
exit 1
fi
permissions:
contents: read
security-events: write

steps:
- uses: actions/checkout@v7
- uses: devblac/docker-lint@v0.1.0
with:
dockerfile: services/api/Dockerfile
strict: true
ignore: DKL404
upload-sarif: true
```

| Input | Default | Description |
|-------|---------|-------------|
| `dockerfile` | `Dockerfile` | Path to the Dockerfile, relative to the repository root |
| `version` | `latest` | docker-lint release to install, e.g. `v0.1.0` |
| `format` | `github` | Log output: `github` (annotations), `text`, `json` or `sarif` |
| `strict` | `false` | Fail on warnings, not only on errors |
| `quiet` | `false` | Hide informational findings |
| `ignore` | | Comma-separated rule IDs to ignore |
| `sarif-file` | | Also write a SARIF report here (`docker-lint.sarif` when uploading) |
| `upload-sarif` | `false` | Upload the SARIF report to GitHub code scanning |

The action fails the step with docker-lint's exit code and exposes it as the `exit-code` output. A SARIF
report is still written and uploaded when the lint fails; a Dockerfile that cannot be parsed produces no
report rather than an empty one.

### GitLab CI

```yaml
Expand Down
179 changes: 179 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
name: docker-lint
description: Lint Dockerfiles for inefficiencies, anti-patterns and security issues, with pull request annotations and SARIF for code scanning
author: devblac
branding:
icon: check-square
color: blue

inputs:
dockerfile:
description: Path to the Dockerfile to lint, relative to the repository root.
default: Dockerfile
version:
description: >-
docker-lint release to use: "latest" or a tag such as "v0.1.0". "source"
builds the action's own checkout instead (needs Go; used to test the action).
default: latest
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 }}
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"

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)"
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 }}
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

# 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" "$DOCKERFILE" || 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
# A fatal error (exit 2) writes nothing to stdout; an empty report
# would only make the upload fail with a confusing message.
docker-lint ${args[@]+"${args[@]}"} --format sarif "$DOCKERFILE" > "$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 the Dockerfile could not 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 the Dockerfile (exit code $EXIT_CODE)"; exit "$EXIT_CODE" ;;
esac
Loading
Loading