diff --git a/.github/workflows/actions-selftest.yml b/.github/workflows/actions-selftest.yml index 3dc835a..8aa02ab 100644 --- a/.github/workflows/actions-selftest.yml +++ b/.github/workflows/actions-selftest.yml @@ -95,3 +95,37 @@ jobs: policy: openshell/policy.yaml - name: Assert drift failed run: test "${{ steps.drift.outcome }}" = failure + + # Regression test for #65: run `check` the way an external consumer does. This repository is checked out + # into a subdirectory, so the workspace root is a separate "consumer" repo with no actions/ directory. + # A relative `uses: ./actions/...` inside a composite resolves against that workspace and would fail here. + consumer-check: + name: consumer check (${{ matrix.version }}) + runs-on: ubuntu-24.04 + strategy: + fail-fast: false + matrix: + version: [source, v0.1.0-rc.3] + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v4.2.2 + with: + path: annotate-src + - name: Create consumer fixture repo + run: | + cp -a annotate-src/examples/polyglot consumer + test ! -e actions + - name: Check (as a consumer) + id: check + uses: ./annotate-src/actions/check + with: + version: ${{ matrix.version }} + root: consumer + policy: openshell/policy.yaml + providers: openshell/providers.json + failure-threshold: warning + - name: Assert version output + env: + VERSION: ${{ steps.check.outputs.version }} + run: | + echo "installed: $VERSION" + test -n "$VERSION" diff --git a/actions/check/action.yml b/actions/check/action.yml index f416923..7ca04b1 100644 --- a/actions/check/action.yml +++ b/actions/check/action.yml @@ -38,27 +38,62 @@ outputs: description: Installed openshell-annotate version. value: ${{ steps.setup.outputs.version }} +# The steps below call the shared scripts in actions/lib through `github.action_path` instead of +# `uses: ./actions/`: inside a composite action a relative `uses:` resolves against the caller's +# workspace, which breaks consumers of `ilanh/openshell-annotate/actions/check@` (issue #65). runs: using: composite steps: - - name: Setup - id: setup - uses: ./actions/setup + - name: Resolve install directory + id: dir + shell: bash + run: bash "${{ github.action_path }}/../lib/setup.sh" dir + + - name: Restore cache + if: inputs.version != 'source' + id: cache + uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.2.2 with: - version: ${{ inputs.version }} + path: ${{ steps.dir.outputs.dir }}/openshell-annotate + key: openshell-annotate-${{ runner.os }}-${{ runner.arch }}-${{ inputs.version }} + + - name: Install from release + if: inputs.version != 'source' && steps.cache.outputs.cache-hit != 'true' + shell: bash + env: + INSTALL_DIR: ${{ steps.dir.outputs.dir }} + OPENSHELL_ANNOTATE_VERSION: ${{ inputs.version }} + VERIFY: "false" + run: bash "${{ github.action_path }}/../lib/setup.sh" install + + - name: Build from source + if: inputs.version == 'source' + shell: bash + env: + INSTALL_DIR: ${{ steps.dir.outputs.dir }} + run: bash "${{ github.action_path }}/../lib/setup.sh" build + + - name: Add to PATH and record version + id: setup + shell: bash + env: + INSTALL_DIR: ${{ steps.dir.outputs.dir }} + run: bash "${{ github.action_path }}/../lib/setup.sh" finish - name: Drift - uses: ./actions/drift - with: - root: ${{ inputs.root }} - policy: ${{ inputs.policy }} - providers: ${{ inputs.providers }} - profiles-dir: ${{ inputs.profiles-dir }} - strict: ${{ inputs.strict-drift }} + shell: bash + working-directory: ${{ inputs.root }} + env: + POLICY: ${{ inputs.policy }} + PROVIDERS: ${{ inputs.providers }} + PROFILES_DIR: ${{ inputs.profiles-dir }} + STRICT: ${{ inputs.strict-drift }} + run: bash "${{ github.action_path }}/../lib/drift.sh" - name: Lint - uses: ./actions/lint - with: - root: ${{ inputs.root }} - policy: ${{ inputs.policy }} - failure-threshold: ${{ inputs.failure-threshold }} + shell: bash + working-directory: ${{ inputs.root }} + env: + POLICY: ${{ inputs.policy }} + THRESHOLD: ${{ inputs.failure-threshold }} + run: bash "${{ github.action_path }}/../lib/lint.sh" diff --git a/actions/drift/action.yml b/actions/drift/action.yml index 2028d9b..720542f 100644 --- a/actions/drift/action.yml +++ b/actions/drift/action.yml @@ -1,7 +1,7 @@ name: openshell-annotate drift description: Fail when the committed policy no longer matches @openshell directives in the tree. branding: - icon: stop + icon: alert-octagon color: red inputs: @@ -36,34 +36,10 @@ runs: id: run shell: bash working-directory: ${{ inputs.root }} - run: | - set -euo pipefail - root_abs="$(pwd)" - policy="${{ inputs.policy }}" - args=(drift --root "$root_abs" --policy "$policy" --json) - if [ -n "${{ inputs.providers }}" ]; then - args+=(--providers "${{ inputs.providers }}") - fi - if [ -n "${{ inputs.profiles-dir }}" ]; then - args+=(--profiles-dir "${{ inputs.profiles-dir }}") - fi - if [ "${{ inputs.strict }}" = "true" ]; then - args+=(--strict) - fi - # shellcheck disable=SC2206 - extra=(${{ inputs.paths }}) - if [ "${#extra[@]}" -gt 0 ]; then - args+=("${extra[@]}") - fi - report="$RUNNER_TEMP/drift.json" - set +e - openshell-annotate "${args[@]}" >"$report" 2> "$RUNNER_TEMP/drift.stderr" - code=$? - set -e - cat "$RUNNER_TEMP/drift.stderr" >&2 - bash "${{ github.action_path }}/../lib/drift-summary.sh" "$report" - if [ "$code" -ne 0 ]; then - echo "::error::Policy drift detected (exit $code). See the job summary and logs." - exit "$code" - fi - echo "no drift" + env: + POLICY: ${{ inputs.policy }} + PROVIDERS: ${{ inputs.providers }} + PROFILES_DIR: ${{ inputs.profiles-dir }} + STRICT: ${{ inputs.strict }} + PATHS: ${{ inputs.paths }} + run: bash "${{ github.action_path }}/../lib/drift.sh" diff --git a/actions/generate/action.yml b/actions/generate/action.yml index 2bb0134..ab4e77b 100644 --- a/actions/generate/action.yml +++ b/actions/generate/action.yml @@ -1,7 +1,7 @@ name: openshell-annotate generate description: Regenerate policy.yaml and optional providers from @openshell directives. branding: - icon: file-code + icon: file-text color: green inputs: diff --git a/actions/lib/drift.sh b/actions/lib/drift.sh new file mode 100755 index 0000000..55b8370 --- /dev/null +++ b/actions/lib/drift.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash +# Run `openshell-annotate drift` in the current directory (the repository root) and write a job summary. +# in: POLICY (required), PROVIDERS, PROFILES_DIR, STRICT (true|false), PATHS (space-separated) +set -euo pipefail + +lib_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +root_abs="$(pwd)" +args=(drift --root "$root_abs" --policy "${POLICY:?policy input is required}" --json) +if [ -n "${PROVIDERS:-}" ]; then + args+=(--providers "$PROVIDERS") +fi +if [ -n "${PROFILES_DIR:-}" ]; then + args+=(--profiles-dir "$PROFILES_DIR") +fi +if [ "${STRICT:-false}" = "true" ]; then + args+=(--strict) +fi +# shellcheck disable=SC2206 +extra=(${PATHS:-}) +if [ "${#extra[@]}" -gt 0 ]; then + args+=("${extra[@]}") +fi +tmp="${RUNNER_TEMP:-/tmp}" +report="$tmp/drift.json" +set +e +openshell-annotate "${args[@]}" >"$report" 2>"$tmp/drift.stderr" +code=$? +set -e +cat "$tmp/drift.stderr" >&2 +bash "$lib_dir/drift-summary.sh" "$report" +if [ "$code" -ne 0 ]; then + echo "::error::Policy drift detected (exit $code). See the job summary and logs." + exit "$code" +fi +echo "no drift" diff --git a/actions/lib/lint.sh b/actions/lib/lint.sh new file mode 100755 index 0000000..9128ad6 --- /dev/null +++ b/actions/lib/lint.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +# Run `openshell-annotate lint` in the current directory (the repository root); annotations + job summary. +# in: POLICY, PATHS (space-separated), THRESHOLD (error|warning|never) +set -euo pipefail + +lib_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +root_abs="$(pwd)" +threshold="${THRESHOLD:-error}" +args=(lint --root "$root_abs" --format json) +if [ -n "${POLICY:-}" ]; then + args+=(--policy "$POLICY") +fi +if [ "$threshold" = "warning" ]; then + args+=(--deny-warnings) +fi +# shellcheck disable=SC2206 +extra=(${PATHS:-}) +if [ "${#extra[@]}" -gt 0 ]; then + args+=("${extra[@]}") +fi +tmp="${RUNNER_TEMP:-/tmp}" +findings="$tmp/lint.json" +stderr="$tmp/lint.stderr" +set +e +openshell-annotate "${args[@]}" >"$findings" 2>"$stderr" +cli_code=$? +set -e +cat "$stderr" >&2 +set +e +bash "$lib_dir/lint-report.sh" "$findings" "$threshold" +report_code=$? +set -e +if [ "$threshold" = "never" ]; then + if [ "$cli_code" -ne 0 ]; then + echo "::notice::lint reported findings but failure-threshold=never (step succeeds)." + fi + exit 0 +fi +if [ "$report_code" -ne 0 ]; then + exit "$report_code" +fi +if [ "$cli_code" -eq 2 ]; then + echo "::error::openshell-annotate lint failed with a usage or I/O error (exit 2)." + exit 2 +fi diff --git a/actions/lib/setup.sh b/actions/lib/setup.sh new file mode 100755 index 0000000..cfbb5fa --- /dev/null +++ b/actions/lib/setup.sh @@ -0,0 +1,67 @@ +#!/usr/bin/env bash +# Shared steps for the `setup` and `check` composite actions. +# Usage: setup.sh dir|install|build|finish +# Paths are resolved from this script's location, never from the caller's workspace, so the actions work +# when consumed remotely (`uses: ilanh/openshell-annotate/actions/check@`) or from any checkout path. +set -euo pipefail + +lib_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +repo_dir="$(cd "$lib_dir/../.." && pwd)" + +case "${1:-}" in + dir) + # in: INPUT_INSTALL_DIR (optional) + dir="${INPUT_INSTALL_DIR:-}" + [ -n "$dir" ] || dir="${RUNNER_TOOL_CACHE:?}/openshell-annotate" + mkdir -p "$dir" + echo "dir=$dir" >>"$GITHUB_OUTPUT" + ;; + install) + # in: INSTALL_DIR, OPENSHELL_ANNOTATE_VERSION, VERIFY + install_sh="$repo_dir/install.sh" + if [ ! -f "$install_sh" ]; then + install_sh="${RUNNER_TEMP:-/tmp}/openshell-annotate-install.sh" + curl -fsSL "https://raw.githubusercontent.com/ilanh/openshell-annotate/${GITHUB_ACTION_REF:-main}/install.sh" \ + -o "$install_sh" + fi + args=(--dir "${INSTALL_DIR:?}") + if [ -n "${OPENSHELL_ANNOTATE_VERSION:-}" ]; then + args+=(--version "$OPENSHELL_ANNOTATE_VERSION") + fi + if [ "${VERIFY:-false}" = "true" ]; then + args+=(--verify) + fi + attempt=0 + max=5 + until sh "$install_sh" "${args[@]}"; do + attempt=$((attempt + 1)) + [ "$attempt" -ge "$max" ] && exit 1 + echo "install.sh failed (attempt $attempt/$max); retrying in ${attempt}0s..." >&2 + sleep $((attempt * 10)) + done + ;; + build) + # in: INSTALL_DIR. Builds the openshell-annotate sources that ship with this action. + [ -f "$repo_dir/Cargo.toml" ] || { echo "::error::version=source needs the action sources (no Cargo.toml in $repo_dir)"; exit 1; } + cd "$repo_dir" + if ! command -v cargo >/dev/null 2>&1; then + rustup toolchain install 1.94 --profile minimal + rustup default 1.94 + fi + cargo build --release --locked --target-dir "$repo_dir/target" + install -m 0755 "$repo_dir/target/release/openshell-annotate" "${INSTALL_DIR:?}/openshell-annotate" + ;; + finish) + # in: INSTALL_DIR. Adds it to PATH and records outputs `version` and `install-dir`. + echo "${INSTALL_DIR:?}" >>"$GITHUB_PATH" + bin="$INSTALL_DIR/openshell-annotate" + ver="$("$bin" --version 2>/dev/null || true)" + echo "version=$ver" >>"$GITHUB_OUTPUT" + echo "install-dir=$INSTALL_DIR" >>"$GITHUB_OUTPUT" + echo "openshell-annotate $ver at $bin" + ;; + *) + echo "usage: setup.sh dir|install|build|finish" >&2 + exit 2 + ;; +esac diff --git a/actions/lint/action.yml b/actions/lint/action.yml index 5a51b09..a0ea476 100644 --- a/actions/lint/action.yml +++ b/actions/lint/action.yml @@ -28,41 +28,8 @@ runs: - name: Run lint shell: bash working-directory: ${{ inputs.root }} - run: | - set -euo pipefail - root_abs="$(pwd)" - args=(lint --root "$root_abs" --format json) - if [ -n "${{ inputs.policy }}" ]; then - args+=(--policy "${{ inputs.policy }}") - fi - threshold="${{ inputs.failure-threshold }}" - if [ "$threshold" = "warning" ]; then - args+=(--deny-warnings) - fi - # shellcheck disable=SC2206 - extra=(${{ inputs.paths }}) - if [ "${#extra[@]}" -gt 0 ]; then - args+=("${extra[@]}") - fi - findings="$RUNNER_TEMP/lint.json" - stderr="$RUNNER_TEMP/lint.stderr" - set +e - openshell-annotate "${args[@]}" >"$findings" 2>"$stderr" - cli_code=$? - set -e - cat "$stderr" >&2 - bash "${{ github.action_path }}/../lib/lint-report.sh" "$findings" "$threshold" - report_code=$? - if [ "$threshold" = "never" ]; then - if [ "$cli_code" -ne 0 ]; then - echo "::notice::lint reported findings but failure-threshold=never (step succeeds)." - fi - exit 0 - fi - if [ "$report_code" -ne 0 ]; then - exit "$report_code" - fi - if [ "$cli_code" -eq 2 ]; then - echo "::error::openshell-annotate lint failed with a usage or I/O error (exit 2)." - exit 2 - fi + env: + POLICY: ${{ inputs.policy }} + PATHS: ${{ inputs.paths }} + THRESHOLD: ${{ inputs.failure-threshold }} + run: bash "${{ github.action_path }}/../lib/lint.sh" diff --git a/actions/setup/action.yml b/actions/setup/action.yml index a9bcaf3..3cc8f5d 100644 --- a/actions/setup/action.yml +++ b/actions/setup/action.yml @@ -38,14 +38,9 @@ runs: - name: Resolve install directory id: dir shell: bash - run: | - if [ -n "${{ inputs.install-dir }}" ]; then - dir="${{ inputs.install-dir }}" - else - dir="${RUNNER_TOOL_CACHE}/openshell-annotate" - fi - mkdir -p "$dir" - echo "dir=$dir" >>"$GITHUB_OUTPUT" + env: + INPUT_INSTALL_DIR: ${{ inputs.install-dir }} + run: bash "${{ github.action_path }}/../lib/setup.sh" dir - name: Restore cache if: inputs.cache == 'true' && inputs.version != 'source' @@ -62,55 +57,18 @@ runs: INSTALL_DIR: ${{ steps.dir.outputs.dir }} OPENSHELL_ANNOTATE_VERSION: ${{ inputs.version }} VERIFY: ${{ inputs.verify-attestation }} - run: | - set -euo pipefail - install_sh="${{ github.action_path }}/../../install.sh" - if [ ! -f "$install_sh" ]; then - ref="${GITHUB_ACTION_REPOSITORY#*/}" - ref_repo="ilanh/openshell-annotate" - tag="${GITHUB_ACTION_REF:-main}" - curl -fsSL "https://raw.githubusercontent.com/${ref_repo}/${tag}/install.sh" -o "$install_sh" - fi - args=(--dir "$INSTALL_DIR") - if [ -n "${OPENSHELL_ANNOTATE_VERSION}" ]; then - args+=(--version "$OPENSHELL_ANNOTATE_VERSION") - fi - if [ "$VERIFY" = "true" ]; then - args+=(--verify) - fi - attempt=0 - max=5 - until sh "$install_sh" "${args[@]}"; do - attempt=$((attempt + 1)) - [ "$attempt" -ge "$max" ] && exit 1 - echo "install.sh failed (attempt $attempt/$max); retrying in ${attempt}0s..." >&2 - sleep $((attempt * 10)) - done + run: bash "${{ github.action_path }}/../lib/setup.sh" install - name: Build from source if: inputs.version == 'source' shell: bash env: INSTALL_DIR: ${{ steps.dir.outputs.dir }} - run: | - set -euo pipefail - if ! command -v cargo >/dev/null 2>&1; then - rustup toolchain install 1.94 --profile minimal - rustup default 1.94 - fi - cargo build --release --locked - install -m 0755 target/release/openshell-annotate "$INSTALL_DIR/openshell-annotate" - - - name: Add to PATH - shell: bash - run: echo "${{ steps.dir.outputs.dir }}" >>"$GITHUB_PATH" + run: bash "${{ github.action_path }}/../lib/setup.sh" build - - name: Record version + - name: Add to PATH and record version id: meta shell: bash - run: | - bin="${{ steps.dir.outputs.dir }}/openshell-annotate" - ver="$("$bin" --version 2>/dev/null || true)" - echo "version=$ver" >>"$GITHUB_OUTPUT" - echo "install-dir=${{ steps.dir.outputs.dir }}" >>"$GITHUB_OUTPUT" - echo "openshell-annotate $ver at $bin" + env: + INSTALL_DIR: ${{ steps.dir.outputs.dir }} + run: bash "${{ github.action_path }}/../lib/setup.sh" finish diff --git a/docs/GITHUB_ACTIONS.md b/docs/GITHUB_ACTIONS.md index 01606bb..497f199 100644 --- a/docs/GITHUB_ACTIONS.md +++ b/docs/GITHUB_ACTIONS.md @@ -52,7 +52,7 @@ jobs: | Input | Default | Description | | --- | --- | --- | -| `version` | `v0.1.0-rc.3` | Release tag, `latest`, or `source` (build with cargo; needs checkout + Rust 1.94) | +| `version` | `v0.1.0-rc.3` | Release tag, `latest`, or `source` (build the action's own sources with cargo; Rust 1.94) | | `install-dir` | `$RUNNER_TOOL_CACHE/openshell-annotate` | Install location | | `verify-attestation` | `false` | `gh attestation verify` (needs GitHub CLI) | | `cache` | `true` | Cache the binary between runs | @@ -116,6 +116,12 @@ summary table. Combines `setup`, `drift`, and `lint`. Inputs: `version`, `root`, `policy`, `providers`, `profiles-dir`, `failure-threshold`, `strict-drift`. Output: `version` (installed CLI). +The actions share their logic through scripts in [`actions/lib`](../actions/lib), called via +`github.action_path`. `check` does not nest `uses: ./actions/...` steps, because inside a composite action a +relative `uses:` resolves against the caller's workspace and breaks remote consumers (#65). The +`consumer-check` job in `.github/workflows/actions-selftest.yml` runs `check` from a subdirectory checkout to +catch that regression. + ## CD: regenerate policy in CI ```yaml