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
39 changes: 10 additions & 29 deletions .github/actions/validate/action.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# The validate hook the hub's validate-task.yml runs in place of its no-op default, this repo's own domain check.
# It builds the image under test and compiles firmware in it, since the toolchains ESPHome downloads on first compile link against system libraries.
# A missing runtime package fails here and nowhere else, so lint and a build-only smoke cannot see it.
# The publisher always compiles, and the push CI compiles only when the image, what is compiled in it, or the hub pin that runs it changed.
# The publisher always compiles, and pull request CI compiles when the image, what is compiled in it, or its hub pin changed.
# A composite action cannot read secrets, so the build reads the public base image and the shared cache without a Docker Hub login.
name: ESPHome-NonRoot validate hook
description: Compile every .github/compile-test configuration in the built image as a non-root user.
Expand All @@ -10,18 +10,16 @@ runs:
using: composite
steps:

# Only a push run of the pull request workflow is gated.
# The publisher and a dispatch always compile, and so does a new main or develop, which carries no before-commit to diff against.
# A feature branch diffs against its merge-base with develop, so a later docs-only push cannot retire a compile test the branch earned.
# Only a pull request run of the pull request workflow is gated, and the publisher and a dispatch always compile.
# The diff runs from the test merge commit's first parent, the base tip, so it covers the whole pull request.
# A later docs-only push therefore cannot retire a compile test.
- name: Decide compile test step
id: decide
shell: bash
env:
EVENT: ${{ github.event_name }}
WORKFLOW_REF: ${{ github.workflow_ref }}
REPOSITORY: ${{ github.repository }}
REF_NAME: ${{ github.ref_name }}
BEFORE: ${{ github.event.before }}
AFTER: ${{ github.sha }}
run: |
set -Eeuo pipefail
Expand All @@ -30,32 +28,15 @@ runs:
echo "Compile test: $1 ($2)"
exit 0
}
if [[ "$EVENT" != "push" || "$WORKFLOW_REF" != "$REPOSITORY/.github/workflows/test-pull-request.yml@"* ]]; then
compile true "not a pull request workflow push"
if [[ "$EVENT" != "pull_request" || "$WORKFLOW_REF" != "$REPOSITORY/.github/workflows/test-pull-request.yml@"* ]]; then
compile true "not a pull request workflow run"
fi
if [[ "$REF_NAME" == "main" || "$REF_NAME" == "develop" ]]; then
if [[ -z "$BEFORE" || "$BEFORE" =~ ^0+$ ]]; then
compile true "new branch"
fi
from="$BEFORE"
else
from=""
fi
# The hub task checks out one commit, so deepen it and fetch develop before reasoning about a diff.
deepen=()
if [[ "$(git rev-parse --is-shallow-repository)" == "true" ]]; then
deepen=(--unshallow)
fi
if ! git fetch --quiet --no-tags "${deepen[@]}" origin "+refs/heads/develop:refs/remotes/origin/develop"; then
# The hub task checks out the merge commit alone, so fetch it again with its parents before reasoning about a diff.
if ! git fetch --quiet --no-tags --depth=2 origin "$AFTER"; then
compile true "history fetch failed"
fi
if [[ -z "$from" ]] && ! from="$(git merge-base origin/develop "$AFTER")"; then
compile true "no merge-base with develop"
fi
if ! diff="$(git diff --name-only "$from" "$AFTER")"; then
compile true "diff base not in history"
fi
# The two stubs carry the hub pin, so a hub release bump compile-tests the hooks under the new tasks before it auto-merges.
diff="$(git diff --no-renames --name-only "$AFTER^1" "$AFTER")"
# The stubs pinning the validate and release tasks are gated, so a hub bump compile-tests the hooks under the new validate task.
if grep -qE '^(Docker/|\.github/compile-test/|\.github/actions/(validate|docker-prepare)/|\.github/workflows/(test-pull-request|publish-release)\.yml$|upstream-version\.json$)' <<<"$diff"; then
compile true "image or compile inputs changed"
fi
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
actor: ${{ github.actor }}
ref_name: ${{ github.ref_name }}

# The same reusable gate the push CI runs, on the triggering commit, running only when a publish will happen.
# The same reusable gate pull request CI runs, on the triggering commit, running only when a publish will happen.
# The validate hook always compiles firmware here, since a publisher run is never change-gated.
validate:
name: Validate job
Expand Down
57 changes: 38 additions & 19 deletions .github/workflows/test-pull-request.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
name: Test pull request action

# CI for every branch, a thin caller onto the hub's reusable validate and build-release tasks.
# Runs on push so the aggregator, the ruleset required status check, is produced on the head SHA by the sole producing run.
# The validate and smoke-build jobs run on every push with no paths filter, so a hub pin bump always exercises the docker-prepare hook.
# Only the compile test inside the validate hook is change-gated, on the image, its test inputs, and the hub pin in this file and the publisher.
# CI for every pull request into main or develop, a thin caller onto the hub's reusable validate and build-release tasks.
# CI never publishes, since the publisher runs on schedule, dispatch, or a bot pin push.
#
# There is deliberately no pull_request trigger: a fork PR cannot push to this repo, so it produces no run and cannot satisfy the required check.
# A maintainer lands such a contribution on an in-repo branch, which pushes and so validates, before merging.
on:
# All branches, but not tags, since a release tag must not re-run CI.
push:
branches: ['**']
pull_request:
branches: [main, develop]
workflow_dispatch:

concurrency:
Expand All @@ -23,11 +16,32 @@ permissions: {}

jobs:

# The same reusable gate the publisher runs, so the push gate and the publish gate are identical.
# A branch-deletion push is skipped, since github.sha is all zeros and the checkout would fail.
# The one image target lists its own paths, and its build hook under .github/actions/** builds it, so a workflow-only or docs-only pull request marks nothing.
changes:
name: Detect changed targets job
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: read
outputs:
release: ${{ steps.filter.outputs.release }}
steps:
- name: Checkout code step
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Filter changed paths step
id: filter
uses: dorny/paths-filter@fbd0ab8f3e69293af611ebaee6363fc25e6d187d # v4.0.1
with:
filters: |
release:
- 'Docker/**'
- 'version.json'
- 'upstream-version.json'
- '.github/actions/**'

# The same reusable gate the publisher runs, which adds the prose gate on a pull request and change-gates the compile test.
validate:
name: Validate job
if: ${{ !github.event.deleted }}
uses: ptr727/ProjectTemplate/.github/workflows/validate-task.yml@fb7f02b24d6282f943ecb2ded1b206326d3f4610 # 2.0.1122
permissions:
contents: read
Expand All @@ -36,7 +50,8 @@ jobs:
# The Docker Hub secrets are mapped even on smoke, since the hub task logs in on every build for the higher rate limit.
smoke-build:
name: Smoke build job
if: ${{ !github.event.deleted }}
needs: [changes]
if: github.event_name == 'workflow_dispatch' || needs.changes.outputs.release == 'true'
uses: ptr727/ProjectTemplate/.github/workflows/build-release-task.yml@fb7f02b24d6282f943ecb2ded1b206326d3f4610 # 2.0.1122
permissions:
contents: read
Expand All @@ -53,26 +68,30 @@ jobs:
enable_pypi: false
expect_release_assets: false
docker_image: ptr727/esphome-nonroot
branch: ${{ github.ref_name }}
# On a pull_request event github.ref_name is the PR ref (for example 123/merge), so the logical branch reads base_ref first.
branch: ${{ github.base_ref || github.ref_name }}

# Single required status check, whose name is the ruleset-bound context in the live branch rulesets.
# Do not rename it without updating the rulesets in lockstep, and it must succeed, not merely not fail.
# Skips on a branch deletion, when the jobs it needs also skip, so the deletion push does not fail it.
check-workflow-status:
name: Check pull request workflow status job
runs-on: ubuntu-latest
permissions: {}
needs: [validate, smoke-build]
if: ${{ always() && !github.event.deleted }}
needs: [changes, validate, smoke-build]
if: always()
steps:
- name: Check workflow results step
run: |
set -Eeuo pipefail
for result in "validate:${{ needs.validate.result }}" "smoke-build:${{ needs.smoke-build.result }}"; do
for result in "changes:${{ needs.changes.result }}" "validate:${{ needs.validate.result }}"; do
name="${result%%:*}"
value="${result#*:}"
if [[ "$value" != "success" ]]; then
echo "::error::Job '$name' did not succeed ($value)."
exit 1
fi
done
if [[ "${{ needs.smoke-build.result }}" != "success" && "${{ needs.smoke-build.result }}" != "skipped" ]]; then
echo "::error::Job 'smoke-build' did not succeed (${{ needs.smoke-build.result }})."
exit 1
fi
7 changes: 4 additions & 3 deletions OPERATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ How this repo is run: what verifying a change requires before it is pushed, how

Verifying a change here means running the gates, then running by hand the parts of the contract the pull request pipeline never reaches.

The gates are the hub's `validate-task.yml` lint block, `markdownlint-cli2`, `cspell` (scoped to `README.md` and `HISTORY.md`), `actionlint`, `editorconfig-checker`, and shellcheck and shfmt over `Docker/entrypoint/*.sh`, plus its repo gate, which checks action pins, line-ending coverage, and the composite-action hooks under `.github/actions/`. CI runs all of them, so run all of them before every push rather than the ones that look relevant. From a hub checkout, `python3 scripts/docker_lint.py --root <this repo>` runs the linters through Docker with no local toolchain, and `python3 scripts/repo_gate.py --root <this repo>` runs the repo gate. The hub task's prose gate runs only on a `pull_request` event, which this repo's push-triggered CI never raises, so run `python3 <hub checkout>/scripts/prose_lint.py --diff <base>` by hand from this repo's root, since it diffs the repository it runs in. A change to `Docker/` is also smoke-built locally with the `docker buildx build` command in the runbook below before it is pushed, the same amd64 build CI runs.
The gates are the hub's `validate-task.yml` lint block, `markdownlint-cli2`, `cspell` (scoped to `README.md` and `HISTORY.md`), `actionlint`, `editorconfig-checker`, and shellcheck and shfmt over `Docker/entrypoint/*.sh`, plus its repo gate, which checks action pins, line-ending coverage, and the composite-action hooks under `.github/actions/`. CI runs all of them, so run all of them before every push rather than the ones that look relevant. From a hub checkout, `python3 scripts/docker_lint.py --root <this repo>` runs the linters through Docker with no local toolchain, and `python3 scripts/repo_gate.py --root <this repo>` runs the repo gate. The hub task's prose gate runs on every pull request. Run it locally from this repo's root with `python3 <hub checkout>/scripts/prose_lint.py --diff <base>`, since it diffs the repository it runs in. A change to `Docker/` is also smoke-built locally with the `docker buildx build` command in the runbook below before it is pushed, the same amd64 build CI runs.

**What pull request CI structurally cannot exercise:**

- **The arm64 image.** The pull request smoke build and every `develop` publish build `linux/amd64` only. Only a full `main` publish builds `linux/amd64,linux/arm64`, so an arm64-only break first surfaces on `main`.
- **The firmware compile test, unless the change touches the image.** On a push run of `test-pull-request.yml`, the `validate` hook compiles only on a change to the image, to what is compiled in it, or to the hub pin. A change outside those paths merges without anything compiling firmware in the image.
- **The firmware compile test, unless the change touches the image.** On a pull request run of `test-pull-request.yml`, the `validate` hook compiles only on a change to the image, to what is compiled in it, or to the hub pin. A change outside those paths merges without anything compiling firmware in the image.
- **The bumped release task, on a hub pin bump.** A workflow-only pull request skips the smoke build, so a bump touching only workflow files never runs the bumped `build-release-task.yml` on its own pull request. A dispatch of `test-pull-request.yml` on the branch the bump merged into always smoke-builds under it. Its check also lands on any open pull request headed by that branch. Whichever run finishes last decides that pull request's required check, and only the pull request's own run checks prose.
- **A running dashboard.** No workflow starts the container and talks to the dashboard. A change that can affect the entrypoint, the dashboard, or file permissions is verified by building and running the image locally, per the runbook below.

Two traps make a headless test of the running container pass while proving nothing:
Expand All @@ -34,7 +35,7 @@ The dashboard is then at `http://localhost:6052`. `docker exec -it --user 1001:1

### Compile a Test Configuration in the Image

The [`validate`](./.github/actions/validate/action.yml) hook, which the hub's `validate-task.yml` runs for push CI and the publisher alike, runs `/entrypoint/cache.sh`, then `esphome config` and `esphome compile` for each fixture in [`.github/compile-test/`](./.github/compile-test/), inside the built image as a non-root user. Reproduce it against a locally built image by bind-mounting the fixtures and running the same two commands for the fixture in question. [`WORKFLOW.md`](./WORKFLOW.md) "The Image Compile Test" states what each fixture covers.
The [`validate`](./.github/actions/validate/action.yml) hook, which the hub's `validate-task.yml` runs for pull request CI and the publisher alike, runs `/entrypoint/cache.sh`, then `esphome config` and `esphome compile` for each fixture in [`.github/compile-test/`](./.github/compile-test/), inside the built image as a non-root user. Reproduce it against a locally built image by bind-mounting the fixtures and running the same two commands for the fixture in question. [`WORKFLOW.md`](./WORKFLOW.md) "The Image Compile Test" states what each fixture covers.

### Check Whether `develop` Is Missing a `main`-Only Fix

Expand Down
Loading
Loading