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
9 changes: 9 additions & 0 deletions .github/actions/docker-lint-pins/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# The Docker lint images, read by action.yml beside this file, by repo_gate.py, and by scripts/docker_lint.py.
# It is never built, and is a Dockerfile only so Dependabot's docker ecosystem bumps each tag and its digest.
FROM ghcr.io/editorconfig-checker/editorconfig-checker:4.0.2@sha256:2ba6232bfa0058f72f5f7d7816711590aa764afab1542af30b3ecb4553587918 AS editorconfig-checker
FROM rhysd/actionlint:1.7.12@sha256:b1934ee5f1c509618f2508e6eb47ee0d3520686341fec936f3b79331f9315667 AS actionlint
FROM davidanson/markdownlint-cli2:v0.24.2@sha256:c768e94d8bb161c449bde28f60656929ca1f02c3010a405f65c9cac71563a889 AS markdownlint
FROM ghcr.io/streetsidesoftware/cspell:10.5.2@sha256:d16ebc7f7b81414c500f279b0f96c43a8c1bfa04279b9b8d98d13c300e2b7095 AS cspell
FROM mirror.gcr.io/koalaman/shellcheck:v0.11.0@sha256:61862eba1fcf09a484ebcc6feea46f1782532571a34ed51fedf90dd25f925a8d AS shellcheck
FROM mirror.gcr.io/mvdan/shfmt:v3.14.1@sha256:8c06884a35683d8763fba6c0d9484d11a61a96da65f8d497d6f625825a6043f2 AS shfmt
FROM mcr.microsoft.com/powershell:7.4-ubuntu-22.04@sha256:62300a213a9293916333df2b014cd3a8f22fb0b0b65f2bb446aaf436bcf8c868 AS powershell
35 changes: 35 additions & 0 deletions .github/actions/docker-lint-pins/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Docker lint pins
description: Read the Docker lint image references, each a version tag with its digest, from the Dockerfile beside this action.

outputs:
editorconfig-checker:
description: The editorconfig-checker image reference the Dockerfile pins.
value: ${{ steps.read.outputs.editorconfig-checker }}
shellcheck:
description: The shellcheck image reference the Dockerfile pins.
value: ${{ steps.read.outputs.shellcheck }}
shfmt:
description: The shfmt image reference the Dockerfile pins.
value: ${{ steps.read.outputs.shfmt }}
powershell:
description: The PowerShell image reference the Dockerfile pins.
value: ${{ steps.read.outputs.powershell }}

runs:
using: composite
steps:
# The manifest is read from the action's own path, so a caller reaching this action at a hub pin runs that pin's images.
- name: Read Docker lint pins step
id: read
shell: bash
run: |
set -Eeuo pipefail
manifest="$GITHUB_ACTION_PATH/Dockerfile"
for stage in editorconfig-checker actionlint markdownlint cspell shellcheck shfmt powershell; do
ref=$(sed -n "s/^FROM \([^[:space:]@]*:[^[:space:]@/]*@sha256:[0-9a-f]\{64\}\) AS ${stage}[[:space:]]*\$/\1/p" "$manifest")
if [ -z "$ref" ] || [ "$ref" != "${ref%%$'\n'*}" ]; then
echo "::error::$manifest must pin $stage on exactly one 'FROM <image>:<tag>@sha256:<digest> AS $stage' line."
exit 1
fi
echo "$stage=$ref" >> "$GITHUB_OUTPUT"
done
71 changes: 53 additions & 18 deletions .github/actions/repo-gate/repo_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import subprocess
import sys
import tempfile
import time
from pathlib import Path
from types import ModuleType
from urllib.parse import quote
Expand Down Expand Up @@ -432,8 +433,11 @@ def check_eol_coverage(root: Path, files: list[str]) -> list[str]:
ACTION_FILE = re.compile(r"^\.github/actions/(?:.+/)?action\.ya?ml$")
# A GitHub expression is not shell, so shellcheck would read its braces as syntax.
EXPRESSION_START = "${{"
SHELLCHECK_IMAGE = "mirror.gcr.io/koalaman/shellcheck:stable"
PIN_MANIFEST = ".github/actions/docker-lint-pins/Dockerfile"
SHELLCHECK_PIN = re.compile(r"(?m)^FROM (\S+:[^\s@/]+@sha256:[0-9a-f]{64}) AS shellcheck[ \t]*$")
SHELLCHECK_TIMEOUT = 300
PULL_ATTEMPTS = 3
PULL_BACKOFF = 5
SCHEMA_TIMEOUT = 300
YAML_TIMEOUT = 60
DOCKER_TIMEOUT = 30
Expand Down Expand Up @@ -655,9 +659,30 @@ def substitute_expressions(body: str) -> str:
return "\n".join(out)


def shellcheck_image() -> str:
"""The shellcheck image reference the pin manifest names, a tag with its digest.

It is read from the hub tree this gate sits in, at the commit that tree holds.
Raises ValueError where the manifest names it on no line or on more than one.
A copy fetched alone has no hub tree, so that error names where the check can run.
"""
hub = gate_hub_root()
if hub is None:
raise ValueError(
f"{PIN_MANIFEST} is not beside this script, so run this check from a hub checkout "
"or through the repo-gate action"
)
manifest = hub / PIN_MANIFEST
pins = SHELLCHECK_PIN.findall(manifest.read_text(encoding="utf-8"))
if len(pins) != 1:
raise ValueError(f"{manifest} names the shellcheck image on {len(pins)} lines, not 1")
return pins[0]


def shellcheck_body(label: str, dialect: str, text: str) -> list[str]:
image = shellcheck_image()
# SC2154 is off because a step's `env:` variables are invisible to shellcheck.
command = ["docker", "run", "--rm", "-i", "--network=none", SHELLCHECK_IMAGE]
command = ["docker", "run", "--rm", "-i", "--network=none", image]
command += ["-s", dialect, "-S", "warning", "-e", "SC2154", "-f", "gcc", "-"]
try:
result = subprocess.run(
Expand Down Expand Up @@ -685,8 +710,13 @@ def docker_unreachable() -> str | None:
"""The reason no docker daemon answers or the image cannot be pulled, or None where both work.

The explicit pull makes a fetch failure one finding with its reason, rather than one per body.
A locally cached image is reused, so a local run may lag CI, which always pulls fresh.
The image is pinned by digest, so a locally cached copy is the one CI pulls.
A pull exiting non-zero is retried a bounded number of times, as a registry error is often transient.
"""
try:
image = shellcheck_image()
except (OSError, ValueError) as error:
return f"the shellcheck pin could not be read: {error}"
try:
result = subprocess.run(
["docker", "info"], capture_output=True, check=False, timeout=DOCKER_TIMEOUT
Expand All @@ -699,7 +729,7 @@ def docker_unreachable() -> str | None:
return f"docker info exited {result.returncode}"
try:
present = subprocess.run(
["docker", "image", "inspect", SHELLCHECK_IMAGE],
["docker", "image", "inspect", image],
capture_output=True,
check=False,
timeout=DOCKER_TIMEOUT,
Expand All @@ -710,21 +740,26 @@ def docker_unreachable() -> str | None:
return f"docker image inspect could not start: {error}"
if present.returncode == 0:
return None
try:
pull = subprocess.run(
["docker", "pull", "--quiet", SHELLCHECK_IMAGE],
capture_output=True,
check=False,
timeout=SHELLCHECK_TIMEOUT,
)
except subprocess.TimeoutExpired:
return f"docker pull did not finish within {SHELLCHECK_TIMEOUT}s"
except OSError as error:
return f"docker pull could not start: {error}"
if pull.returncode != 0:
failure = ""
for attempt in range(PULL_ATTEMPTS):
if attempt:
time.sleep(PULL_BACKOFF * attempt)
try:
pull = subprocess.run(
["docker", "pull", "--quiet", image],
capture_output=True,
check=False,
timeout=SHELLCHECK_TIMEOUT,
)
except subprocess.TimeoutExpired:
return f"docker pull did not finish within {SHELLCHECK_TIMEOUT}s"
except OSError as error:
return f"docker pull could not start: {error}"
if pull.returncode == 0:
return None
detail = pull.stderr.decode("utf-8", errors="replace").strip()
return f"docker pull of {SHELLCHECK_IMAGE} exited {pull.returncode}: {detail}"
return None
failure = f"docker pull of {image} exited {pull.returncode}: {detail}"
return f"{failure} (attempt {PULL_ATTEMPTS} of {PULL_ATTEMPTS})"


def check_composite_shell(root: Path, path: str) -> list[str]:
Expand Down
21 changes: 21 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#
# The github-actions ecosystem covers every workflow and composite action this repo ships.
# The pip pair covers the ruff version the validator runs, pinned in .github/actions/python-tool-pins/requirements.txt.
# The docker pair covers the lint images, each a version tag with its digest, pinned in .github/actions/docker-lint-pins/Dockerfile.
# Both main and develop are targeted so each stays current independently of the develop to main release cadence.
# The merge-bot auto-merges each base with its per-base method.
# See catalog/snippets/configs/dependabot.yml for the multi-ecosystem reference, covering nuget, uv, and pip, that a code-shipping repo uses.
Expand Down Expand Up @@ -43,3 +44,23 @@ updates:
directory: "/.github/actions/python-tool-pins"
schedule:
interval: "daily"

- package-ecosystem: "docker"
target-branch: "main"
directory: "/.github/actions/docker-lint-pins"
schedule:
interval: "daily"
groups:
docker-lint:
patterns:
- "*"

- package-ecosystem: "docker"
target-branch: "develop"
directory: "/.github/actions/docker-lint-pins"
schedule:
interval: "daily"
groups:
docker-lint:
patterns:
- "*"
63 changes: 59 additions & 4 deletions .github/workflows/validate-task.yml
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,53 @@ jobs:
- name: Lint workflows step
uses: raven-actions/actionlint@3d39aea434753780c3b3d4a1a31c854b4dbf49d7 # v2.2.0

# Each image is a version tag with its digest, from the Dockerfile beside the action, which Dependabot's docker ecosystem bumps.
# A fresh runner still pulls a pinned image, so each step retries a failed pull before it runs a container.
- name: Read Docker lint pins step
id: docker-pins
uses: $/.github/actions/docker-lint-pins

- name: Check EditorConfig step
run: docker run --rm --pull=always -v "$PWD":/check --workdir /check ghcr.io/editorconfig-checker/editorconfig-checker:latest
env:
EDITORCONFIG_IMAGE: ${{ steps.docker-pins.outputs.editorconfig-checker }}
run: |
set -Eeuo pipefail
pull() {
local attempt status
for attempt in 1 2 3; do
status=0
timeout 300 docker pull --quiet "$1" || status=$?
[ "$status" -ne 0 ] || return 0
[ "$status" -ne 124 ] || { echo "::error::docker pull of $1 timed out after 300s."; return 1; }
echo "::warning::docker pull of $1 failed on attempt $attempt of 3."
[ "$attempt" -eq 3 ] || sleep $((attempt * 15))
done
return 1
}
pull "$EDITORCONFIG_IMAGE"
docker run --rm -v "$PWD":/check --workdir /check "$EDITORCONFIG_IMAGE"

# The file list comes from git rather than a glob, and the docker run is skipped rather than invoked on an empty argument list.
# That is how a repo with no shell scripts stays clean instead of failing on a linter's own no-file usage error.
# A `*.sh` glob alone misses a bare-command script with no extension, so a shebang-matched file joins the list too.
- name: Check shell scripts step
env:
SHELLCHECK_IMAGE: ${{ steps.docker-pins.outputs.shellcheck }}
SHFMT_IMAGE: ${{ steps.docker-pins.outputs.shfmt }}
run: |
set -Eeuo pipefail
pull() {
local attempt status
for attempt in 1 2 3; do
status=0
timeout 300 docker pull --quiet "$1" || status=$?
[ "$status" -ne 0 ] || return 0
[ "$status" -ne 124 ] || { echo "::error::docker pull of $1 timed out after 300s."; return 1; }
echo "::warning::docker pull of $1 failed on attempt $attempt of 3."
[ "$attempt" -eq 3 ] || sleep $((attempt * 15))
done
return 1
Comment thread
ptr727 marked this conversation as resolved.
Resolved
}
# Tokenizes rather than substring-matches, so `#!/usr/bin/python bash` is not mistaken for a shell script.
# `docker_lint.py`'s shell_shebang_interpreter is the same logic; keep both in sync on a change here.
is_shell_shebang() {
Expand Down Expand Up @@ -258,8 +296,10 @@ jobs:
fi
done
if [ "${#scripts[@]}" -gt 0 ]; then
docker run --rm --pull=always -v "$PWD":/mnt --workdir /mnt mirror.gcr.io/koalaman/shellcheck:stable -- "${scripts[@]}"
docker run --rm --pull=always -v "$PWD":/mnt --workdir /mnt mirror.gcr.io/mvdan/shfmt:latest -d -- "${scripts[@]}"
pull "$SHELLCHECK_IMAGE"
pull "$SHFMT_IMAGE"
docker run --rm -v "$PWD":/mnt --workdir /mnt "$SHELLCHECK_IMAGE" -- "${scripts[@]}"
docker run --rm -v "$PWD":/mnt --workdir /mnt "$SHFMT_IMAGE" -d -- "${scripts[@]}"
else
echo "no shell scripts are tracked"
fi
Expand All @@ -268,11 +308,26 @@ jobs:
# A repo with no .ps1 files pays nothing for the pull.
# 1.23.0 rather than the newest, since 1.24.0 needs a newer System.Management.Automation than this image carries, which installs and then fails to import.
- name: Check PowerShell scripts step
env:
POWERSHELL_IMAGE: ${{ steps.docker-pins.outputs.powershell }}
run: |
set -Eeuo pipefail
pull() {
local attempt status
for attempt in 1 2 3; do
status=0
timeout 300 docker pull --quiet "$1" || status=$?
[ "$status" -ne 0 ] || return 0
[ "$status" -ne 124 ] || { echo "::error::docker pull of $1 timed out after 300s."; return 1; }
echo "::warning::docker pull of $1 failed on attempt $attempt of 3."
[ "$attempt" -eq 3 ] || sleep $((attempt * 15))
done
return 1
}
mapfile -t ps_scripts < <(git ls-files '*.ps1')
if [ "${#ps_scripts[@]}" -gt 0 ]; then
docker run --rm --pull=always -e PS_SCRIPTS="$(git ls-files '*.ps1')" -v "$PWD":/mnt --workdir /mnt mcr.microsoft.com/powershell:latest \
pull "$POWERSHELL_IMAGE"
docker run --rm -e PS_SCRIPTS="$(git ls-files '*.ps1')" -v "$PWD":/mnt --workdir /mnt "$POWERSHELL_IMAGE" \
pwsh -NoProfile -Command '
Set-PSRepository PSGallery -InstallationPolicy Trusted
Install-Module PSScriptAnalyzer -RequiredVersion 1.23.0 -Force -Scope AllUsers
Expand Down
Loading
Loading