From 4a5050618f0d296b0077c61d49a69ed23ae4f3a7 Mon Sep 17 00:00:00 2001 From: RiriAgent <39219425+RiriAgent@users.noreply.github.com> Date: Fri, 27 Nov 2026 00:31:06 +0900 Subject: [PATCH] Add actions/mise-check and a task input for check.yml - actions/mise-check: composite action run as a step of a job named test, so the PR shows one top-level test check; guard.sh rejects task names outside ^[A-Za-z0-9_][A-Za-z0-9_:-]*$ and pull requests from another repository on non-GitHub-hosted runners before mise is installed - check.yml: optional task input (default check), validated in parse and passed to mise run as one argument - selftest-check.yml: runs the action and check.yml from the dispatched commit, including the default task, a passed task and an invalid task - mise.toml: selftest:task fixture Refs mouriya-devops/ci#19 --- .github/workflows/check.yml | 20 ++++++++-- .github/workflows/selftest-check.yml | 57 +++++++++++++++++++++++++++- actions/mise-check/action.yml | 52 +++++++++++++++++++++++++ actions/mise-check/guard.sh | 21 ++++++++++ mise.toml | 4 ++ 5 files changed, 149 insertions(+), 5 deletions(-) create mode 100644 actions/mise-check/action.yml create mode 100755 actions/mise-check/guard.sh diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index ada9ba7..eac02d5 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -17,6 +17,11 @@ on: triggering commit. type: string default: '' + task: + description: >- + mise task to run, matching `^[A-Za-z0-9_][A-Za-z0-9_:-]*$`. + type: string + default: 'check' permissions: contents: read @@ -27,12 +32,14 @@ jobs: outputs: runs-on: ${{ steps.parse.outputs.runs-on }} ref: ${{ steps.parse.outputs.ref }} + task: ${{ steps.parse.outputs.task }} steps: - name: Parse inputs id: parse env: IN_RUNS_ON: ${{ inputs.runs-on }} IN_REF: ${{ inputs.ref }} + IN_TASK: ${{ inputs.task }} EVENT_NAME: ${{ github.event_name }} HEAD_REPO: ${{ github.event.pull_request.head.repo.full_name }} REPO: ${{ github.repository }} @@ -46,6 +53,9 @@ jobs: || fail runs-on "must be a non-empty JSON array of non-empty strings, got '$IN_RUNS_ON'" runs_on=$(jq -c . <<<"$IN_RUNS_ON") + [[ "$IN_TASK" =~ ^[A-Za-z0-9_][A-Za-z0-9_:-]*$ ]] \ + || fail task "must match ^[A-Za-z0-9_][A-Za-z0-9_:-]*\$, got '$IN_TASK'" + case "$EVENT_NAME" in pull_request|pull_request_target) if [ "$HEAD_REPO" != "$REPO" ] \ @@ -58,6 +68,7 @@ jobs: { echo "runs-on=$runs_on" echo "ref=${IN_REF:-$SHA}" + echo "task=$IN_TASK" } >>"$GITHUB_OUTPUT" check: @@ -73,12 +84,14 @@ jobs: uses: jdx/mise-action@main - name: Summarize task and toolchain + env: + TASK: ${{ needs.parse.outputs.task }} run: | set -euo pipefail { echo '### check' echo - echo "Task: \`mise run check\` at \`$(git rev-parse HEAD)\`" + echo "Task: \`mise run $TASK\` at \`$(git rev-parse HEAD)\`" echo echo '| tool | version |' echo '|---|---|' @@ -86,7 +99,8 @@ jobs: | jq -r 'to_entries[] | .key as $t | .value[] | "| \($t) | \(.version) |"' } >>"$GITHUB_STEP_SUMMARY" - - name: mise run check + - name: mise run env: CI: 'true' - run: mise run check + TASK: ${{ needs.parse.outputs.task }} + run: mise run "$TASK" diff --git a/.github/workflows/selftest-check.yml b/.github/workflows/selftest-check.yml index 8d830be..8ed0eb0 100644 --- a/.github/workflows/selftest-check.yml +++ b/.github/workflows/selftest-check.yml @@ -1,5 +1,9 @@ name: selftest-check +# Exercises actions/mise-check and check.yml from the dispatched commit: +# `test` (default task) must appear as a top-level check, a passed task must +# run, an invalid task must be rejected before mise runs, and check.yml must +# run both its default task and a passed `task`. on: workflow_dispatch: inputs: @@ -16,8 +20,57 @@ permissions: contents: read jobs: - check: - uses: mouriya-devops/ci/.github/workflows/check.yml@main + test: + name: test + runs-on: ubuntu-24.04 + steps: + - uses: actions/checkout@main + with: + persist-credentials: false + - uses: ./actions/mise-check + + task-input: + runs-on: ubuntu-24.04 + steps: + - uses: actions/checkout@main + with: + persist-credentials: false + - uses: ./actions/mise-check + with: + task: selftest:task + - name: Expect the task to have run + run: cat "$RUNNER_TEMP/selftest-task-ran" + + reject-invalid-task: + runs-on: ubuntu-24.04 + steps: + - uses: actions/checkout@main + with: + persist-credentials: false + - id: invalid + continue-on-error: true + uses: ./actions/mise-check + with: + task: 'selftest:task; touch injected' + - name: Expect rejection before mise runs + env: + OUTCOME: ${{ steps.invalid.outcome }} + run: | + set -euo pipefail + test "$OUTCOME" = failure + test ! -e "$RUNNER_TEMP/selftest-task-ran" + test ! -e injected + echo "invalid task rejected: outcome=$OUTCOME, no marker, no injected file" + + check-default: + uses: ./.github/workflows/check.yml + with: + runs-on: ${{ inputs.runs-on }} + ref: ${{ inputs.ref }} + + check-task: + uses: ./.github/workflows/check.yml with: runs-on: ${{ inputs.runs-on }} ref: ${{ inputs.ref }} + task: selftest:task diff --git a/actions/mise-check/action.yml b/actions/mise-check/action.yml new file mode 100644 index 0000000..98c8e8d --- /dev/null +++ b/actions/mise-check/action.yml @@ -0,0 +1,52 @@ +name: mise-check +description: >- + Install the repository's mise toolchain and run one mise task with CI=true. + Use it as a step of a job named `test` so the pull request shows a single + top-level `test` check. The caller checks out the repository first. + +inputs: + task: + description: >- + mise task to run, matching `^[A-Za-z0-9_][A-Za-z0-9_:-]*$`. Pull requests + from another repository are rejected unless the job runs on a + GitHub-hosted runner. + default: check + +runs: + using: composite + steps: + - name: Guard task name and runner + shell: bash + env: + TASK: ${{ inputs.task }} + EVENT_NAME: ${{ github.event_name }} + HEAD_REPO: ${{ github.event.pull_request.head.repo.full_name }} + REPO: ${{ github.repository }} + run: '"$GITHUB_ACTION_PATH/guard.sh"' + + - name: Install mise and repository toolchain + uses: jdx/mise-action@main + + - name: Summarize task and toolchain + shell: bash + env: + TASK: ${{ inputs.task }} + run: | + set -euo pipefail + { + echo '### mise-check' + echo + echo "Task: \`mise run $TASK\` at \`$(git rev-parse HEAD)\`" + echo + echo '| tool | version |' + echo '|---|---|' + mise ls --current --json \ + | jq -r 'to_entries[] | .key as $t | .value[] | "| \($t) | \(.version) |"' + } >>"$GITHUB_STEP_SUMMARY" + + - name: mise run + shell: bash + env: + CI: 'true' + TASK: ${{ inputs.task }} + run: mise run "$TASK" diff --git a/actions/mise-check/guard.sh b/actions/mise-check/guard.sh new file mode 100755 index 0000000..ea1ae01 --- /dev/null +++ b/actions/mise-check/guard.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash +# Inputs (environment): TASK, EVENT_NAME, HEAD_REPO, REPO, RUNNER_ENVIRONMENT. +# Exits non-zero before anything is installed or run when the task name is not +# a plain mise task name, or when a pull request from another repository runs +# on a runner that is not GitHub-hosted. +set -euo pipefail + +fail() { echo "::error title=mise-check $1::$2"; exit 1; } + +[[ "${TASK:-}" =~ ^[A-Za-z0-9_][A-Za-z0-9_:-]*$ ]] \ + || fail task "task '${TASK:-}' must match ^[A-Za-z0-9_][A-Za-z0-9_:-]*\$" + +case "${EVENT_NAME:-}" in + pull_request|pull_request_target) + if [ "${HEAD_REPO:-}" != "${REPO:-}" ] && [ "${RUNNER_ENVIRONMENT:-}" != github-hosted ]; then + fail runner "pull request from '${HEAD_REPO:-}' must not run on a ${RUNNER_ENVIRONMENT:-unknown} runner of ${REPO:-}" + fi + ;; +esac + +echo "task '$TASK' on a ${RUNNER_ENVIRONMENT:-unknown} runner for ${EVENT_NAME:-unknown}" diff --git a/mise.toml b/mise.toml index 28965da..0b432aa 100644 --- a/mise.toml +++ b/mise.toml @@ -6,6 +6,10 @@ shellcheck = "latest" description = "Lint every workflow in .github/workflows" run = "actionlint" +[tasks."selftest:task"] +description = "Self-test fixture for actions/mise-check and the check.yml task input: report CI and leave a marker" +run = 'echo "selftest:task ran with CI=$CI" | tee "${RUNNER_TEMP:-/tmp}/selftest-task-ran"' + [tasks.build] description = "Self-test fixture for image.yml: build ci-selftest/alpha and ci-selftest/beta" run = "selftest/image/build.sh"