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
20 changes: 17 additions & 3 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }}
Expand All @@ -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" ] \
Expand All @@ -58,6 +68,7 @@ jobs:
{
echo "runs-on=$runs_on"
echo "ref=${IN_REF:-$SHA}"
echo "task=$IN_TASK"
} >>"$GITHUB_OUTPUT"

check:
Expand All @@ -73,20 +84,23 @@ 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 '|---|---|'
mise ls --current --json \
| 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"
57 changes: 55 additions & 2 deletions .github/workflows/selftest-check.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
52 changes: 52 additions & 0 deletions actions/mise-check/action.yml
Original file line number Diff line number Diff line change
@@ -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"
21 changes: 21 additions & 0 deletions actions/mise-check/guard.sh
Original file line number Diff line number Diff line change
@@ -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:-<deleted repository>}' 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}"
4 changes: 4 additions & 0 deletions mise.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading