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
30 changes: 23 additions & 7 deletions checks/lib-conventions.nix
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,10 @@ mk.script "lib-conventions" { } ''
]}

# With .github/workflows/actions.lock, a workflow step's exact release tag
# passes, and so does a SHA with its comment. A reusable workflow call, a
# composite action's step, a branch and a major-only tag do not; and without
# the lock, an exact tag does not pass either.
# passes, and so does a SHA with its comment. A reusable workflow call to an
# Avunu repository passes with its exact release tag. A reusable call to another
# owner, a composite action's step, a branch and a major-only tag do not; and
# without the lock, a step's exact tag does not pass either.
echo "a SHA passes with a lock: ${pins (stub.variant "locked" "touch .github/workflows/actions.lock")}"
echo "a release tag passes with a lock: ${pins (
stub.variant "tag-locked" ''
Expand All @@ -343,15 +344,30 @@ mk.script "lib-conventions" { } ''
"1 action(s) not pinned"
]
}
echo "a release tag passes on an Avunu reusable workflow: ${pins (
stub.variant "tag-reusable" ''
sed -i 's|dependabot-auto-merge.yml@51c31525e5a1f2ed0d3e4b8b8c0b9c9a2f0f3e11 # main (no release yet)|dependabot-auto-merge.yml@v1.6.0|' .github/workflows/check.yml
''
)}"
${stub.expectFailure
(pins (
stub.variant "tag-reusable" ''
sed -i 's|dependabot-auto-merge.yml@51c31525e5a1f2ed0d3e4b8b8c0b9c9a2f0f3e11 # main (no release yet)|dependabot-auto-merge.yml@v1.6.0|' .github/workflows/check.yml
touch .github/workflows/actions.lock
stub.variant "tag-reusable-other-owner" ''
sed -i 's|Avunu/python-devops-nix/.github/workflows/dependabot-auto-merge.yml@51c31525e5a1f2ed0d3e4b8b8c0b9c9a2f0f3e11 # main (no release yet)|acme/tools/.github/workflows/dependabot-auto-merge.yml@v1.6.0|' .github/workflows/check.yml
''
))
[
".github/workflows/check.yml:10: acme/tools/.github/workflows/dependabot-auto-merge.yml@v1.6.0 is not pinned by a full commit SHA"
"1 action(s) not pinned"
]
}
${stub.expectFailure
(pins (
stub.variant "branch-reusable" ''
sed -i 's|dependabot-auto-merge.yml@51c31525e5a1f2ed0d3e4b8b8c0b9c9a2f0f3e11 # main (no release yet)|dependabot-auto-merge.yml@main|' .github/workflows/check.yml
''
))
[
".github/workflows/check.yml:10: Avunu/python-devops-nix/.github/workflows/dependabot-auto-merge.yml@v1.6.0 is not pinned by a full commit SHA"
".github/workflows/check.yml:10: Avunu/python-devops-nix/.github/workflows/dependabot-auto-merge.yml@main is not pinned by a full commit SHA or exact release tag (vX.Y.Z)"
"1 action(s) not pinned"
]
}
Expand Down
2 changes: 1 addition & 1 deletion docs/conventions.md
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ The pieces in the NixOS modules ([library](library.md#production)), adopted in f

- **Convention:** an action is pinned by the rule for its kind of `uses:`, and a floating branch ref is never allowed:
- **A workflow step's action**, which gh-actions-lock can pin: an exact release tag (`vX.Y.Z`). The repository needs its lock file, `.github/workflows/actions.lock`, for a tag. A full commit SHA with the version in a trailing comment is accepted there too, which is how the products and this repository pin today.
- **A reusable workflow call** (`uses: owner/repo/.github/workflows/x.yml@ref`): a full commit SHA with the version in a trailing comment, because gh-actions-lock does not cover reusable workflows.
- **A reusable workflow call** (`uses: owner/repo/.github/workflows/x.yml@ref`): a full commit SHA with the version in a trailing comment, because gh-actions-lock does not cover reusable workflows. A reusable workflow of an Avunu repository may instead name its exact release tag (`@vX.Y.Z`); the lock does not cover that either, so the tag stays a name rather than a commit. Any other owner's reusable workflow keeps the SHA.
- **A composite action's steps** (`.github/actions/*/action.yml`) that call remote actions: a full commit SHA with the version in a trailing comment, because gh-actions-lock refuses to lock them without `--ignore-local-actions`.
- **A floating branch ref** is never allowed, in any of these places.

Expand Down
2 changes: 1 addition & 1 deletion docs/library.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ Conventions: [Images](conventions.md#images).
| `lib.checks.commandFlags { pkgs, product, commands, exceptions ? [ ] }` | Each of `commands`, `{ restore, scaffolder }`, run with `--help`, lists the flags the conventions name for it, at the head of an option line. Two spaces or a tab end the flags, and a metavar is in upper case or in brackets. |
| `lib.checks.optionPaths { pkgs, product, options, tenant, exceptions ? [ ] }` | An eval check that the options the conventions name are declared and visible under `<product>.*` in `options.shell` and `services.<product>.*` in `options.nixos`, that `ports.*` are not read-only, and that nothing visible is left in `dev`. `tenant` is `{ shell, nixos }`, the default tenant's option in each. |
| `lib.checks.configBlocks { pkgs, product, options, settings }` | An eval check that each `{ module, path, format, args ? { } }` in `settings` is a `settings` submodule whose `freeformType` is `pkgs.formats.<format> args`'s type. |
| `.github/workflows/dependabot-auto-merge.yml` | A reusable workflow that auto-merges Dependabot pull requests by a policy the caller passes. A caller pins it by SHA. With a GitHub App, the App enables auto-merge, so the merge raises a `push`. Its jobs run on GitHub-hosted `ubuntu-latest`, and fetch-metadata runs apart from the job that holds the App's key, which runs only when both fetch-metadata's `policy` and a `diff` job that reads the pull request's diff with `gh` allow it. When either holds a head, a `hold` job turns off, with `GITHUB_TOKEN`, auto-merge an earlier head's run enabled. |
| `.github/workflows/dependabot-auto-merge.yml` | A reusable workflow that auto-merges Dependabot pull requests by a policy the caller passes. A caller pins it by SHA, or by its release tag. With a GitHub App, the App enables auto-merge, so the merge raises a `push`. Its jobs run on GitHub-hosted `ubuntu-latest`, and fetch-metadata runs apart from the job that holds the App's key, which runs only when both fetch-metadata's `policy` and a `diff` job that reads the pull request's diff with `gh` allow it. When either holds a head, a `hold` job turns off, with `GITHUB_TOKEN`, auto-merge an earlier head's run enabled. |
| `.github/workflows/dependabot-update-branches.yml` | A reusable workflow that brings the Dependabot pull requests that are behind the default branch up to date through the update-branch API. A caller pins it by SHA and runs it on a push to the default branch and on a schedule. Its job holds the App's key, so it runs on GitHub-hosted `ubuntu-latest`. |
| `.github/actions/pre-commit-autofix` | Runs the caller's fixers (`nix fmt`) and prek hooks over a pull request's branch, pushes what they rewrote, dispatches the workflows the push cannot start, and comments what it did. |
| `.github/actions/nix-failures` | Markdown from the log of a failed `nix build` or `nix flake check`: each failed derivation with the end of its log, or the evaluation error. |
Expand Down
2 changes: 1 addition & 1 deletion docs/releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ The release pull request is pushed with `GITHUB_TOKEN`, and a push made with it
A product pins a release, never a commit of `main`:

- The flake input at the release branch. `release` carries every release, so `github:Avunu/python-devops-nix/release` locks to the latest one. A product is kept safe by its pinned `flake.lock` and by CI, not by the branch name: Dependabot's nix entry moves the lock only when a release moves the branch, and the product's checks must pass before that pull request merges, a new major included.
- The reusable workflow and the actions at a tag's commit, with the version in a comment, as every other action is pinned: `@<sha> # v1.0.0`. Dependabot's github-actions entry moves both.
- The reusable workflow and the actions at a tag's commit, with the version in a comment, as every other action is pinned: `@<sha> # v1.0.0`. Dependabot's github-actions entry moves both. The reusable workflow may instead name the tag itself, `@v1.0.0`, which [CI](conventions.md#ci) allows for an Avunu reusable workflow.

Both products pin a release's commit, with its version in the comment ([CI](conventions.md#ci)).

Expand Down
44 changes: 29 additions & 15 deletions lib/checks/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
sha256 digest, with a trailing comment, which by convention names the version.
A local `./` path is exempt. Where .github/workflows/actions.lock exists, an
action a workflow step uses may instead be pinned by an exact release tag
(`vX.Y.Z`), which gh-actions-lock locks. A reusable workflow call and a
composite action's steps keep the SHA.
(`vX.Y.Z`), which gh-actions-lock locks. A reusable workflow call to an Avunu
repository may also be pinned by its exact release tag; any other reusable call
and a composite action's steps keep the SHA. A branch is never a pin.

`dependabot` (docs/conventions.md, Dependabot): every ecosystem in
.github/dependabot.yml has the commit prefix, the seven-day cooldown and one
Expand Down Expand Up @@ -48,21 +49,27 @@ def sequence(node: yaml.Node | None) -> list[yaml.Node]:
return node.value if isinstance(node, yaml.SequenceNode) else []


def uses_nodes(root: yaml.Node) -> Iterator[tuple[yaml.Node, bool]]:
# Each `uses:` with whether it is a workflow step's action, the one kind a
# release tag may pin. A job's jobs.<id>.uses is a reusable workflow, and a
# composite action's runs.steps[].uses stays on the SHA.
def uses_nodes(root: yaml.Node) -> Iterator[tuple[yaml.Node, str]]:
# Each `uses:` with its kind. A workflow step's action is "step", the kind a
# lock may pin by release tag. A job's jobs.<id>.uses is "reusable", a
# reusable workflow call. A composite action's runs.steps[].uses is
# "composite", which stays on the SHA.
top = mapping(root)
for job_node in mapping(top.get("jobs")).values():
job = mapping(job_node)
if "uses" in job:
yield job["uses"], False
yield job["uses"], "reusable"
for step in sequence(job.get("steps")):
if "uses" in mapping(step):
yield mapping(step)["uses"], True
yield mapping(step)["uses"], "step"
for step in sequence(mapping(top.get("runs")).get("steps")):
if "uses" in mapping(step):
yield mapping(step)["uses"], False
yield mapping(step)["uses"], "composite"


def avunu_release_tag(ref: str) -> bool:
"""A reusable workflow of an Avunu repository, at an exact release tag."""
return ref.startswith("Avunu/") and TAG.fullmatch(ref) is not None


def pin_findings(path: Path, rel: str, lockable: bool) -> Iterator[str]:
Expand All @@ -71,15 +78,22 @@ def pin_findings(path: Path, rel: str, lockable: bool) -> Iterator[str]:
if root is None:
return
lines = text.splitlines()
for node, step in uses_nodes(root):
for node, kind in uses_nodes(root):
where = f"{rel}:{node.start_mark.line + 1}"
ref = node.value if isinstance(node, yaml.ScalarNode) else ""
if ref.startswith("./"):
continue
if lockable and step and TAG.fullmatch(ref):
if lockable and kind == "step" and TAG.fullmatch(ref):
continue
if kind == "reusable" and avunu_release_tag(ref):
continue
if not PIN.fullmatch(ref):
by = "sha256 digest" if ref.startswith("docker://") else "full commit SHA"
if ref.startswith("docker://"):
by = "sha256 digest"
elif kind == "reusable" and ref.startswith("Avunu/"):
by = "full commit SHA or exact release tag (vX.Y.Z)"
else:
by = "full commit SHA"
yield f"{where}: {ref} is not pinned by a {by}"
continue
if not trailing_comment(lines, node):
Expand Down Expand Up @@ -122,9 +136,9 @@ def pins(product_dir: Path) -> int:
if findings:
print(
f"{len(findings)} action(s) not pinned. Pin each by its full commit SHA,"
" with its version in a trailing comment, or, in a workflow step where"
" .github/workflows/actions.lock exists, by its exact release tag"
" (docs/conventions.md, CI)."
" with its version in a trailing comment, or by its exact release tag where"
" the convention allows one: a workflow step where .github/workflows/actions.lock"
" exists, or a reusable workflow of Avunu (docs/conventions.md, CI)."
)
return 1
print(f"every action in {len(files)} file(s) is pinned")
Expand Down
2 changes: 1 addition & 1 deletion standards/DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -1768,7 +1768,7 @@ These are copied (§2.4). The update-branches bot rule:
Every action:

- is `runs: using: composite`;
- pins every `uses:` by SHA with a version comment (the `ci-pins` check);
- pins every `uses:` by SHA with a version comment, or a reusable workflow of Avunu by its release tag (the `ci-pins` check);
- passes inputs through `env:`, never interpolated into scripts (the zizmor template-injection rule);
- names the product CLI through the `cli` input, so no product name is hard-coded.

Expand Down
Loading