diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2955043..6aa7765 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,10 +17,12 @@ env: # The state cache jobs need kapi 1.2, the first release that keeps its state # under .kapi/work/. STATE_CACHE_KAPI_VERSION: 1.2.0-rc32 - # The plugin cache jobs seed a cache with this kapi and an older bowrain, - # then set up the newest kapi over it. + # The plugin cache jobs seed a cache with this kapi and a bowrain the newest + # stable kapi does not pick on a fresh install, then set up the newest kapi + # over it. The registry lists bowrain 1.3.0 (stable) and 1.4.0-rc5 (beta) + # only (registry#6), so the seed is the beta. PLUGIN_SEED_KAPI_VERSION: 1.2.0 - PLUGIN_SEED_BOWRAIN_VERSION: 1.0.2 + PLUGIN_SEED_BOWRAIN_VERSION: 1.4.0-rc5 jobs: test-latest: @@ -36,8 +38,17 @@ jobs: - name: Setup kapi (latest) id: setup uses: ./ + # No bowrain build is listed for Windows until the next kapi release + # (registry#6, a founder decision), so the Windows run installs no + # plugin and the step below asserts the install still fails there. with: token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} + plugins: ${{ matrix.os != 'windows-latest' && 'bowrain' || '' }} + + - name: Assert no bowrain build is listed for Windows + if: runner.os == 'Windows' + shell: bash + run: test/plugins/windows-unlisted.sh - name: Verify version output shell: bash @@ -80,9 +91,18 @@ jobs: - name: Setup kapi (pinned) id: setup uses: ./ + # No bowrain build is listed for Windows until the next kapi release + # (registry#6, a founder decision), so the Windows run installs no + # plugin and the step below asserts the install still fails there. with: token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} version: ${{ steps.version.outputs.version }} + plugins: ${{ matrix.os != 'windows-latest' && 'bowrain' || '' }} + + - name: Assert no bowrain build is listed for Windows + if: runner.os == 'Windows' + shell: bash + run: test/plugins/windows-unlisted.sh - name: Verify correct version shell: bash @@ -264,6 +284,98 @@ jobs: name: state-cache-restore-${{ matrix.os }} path: ${{ runner.temp }}/results + # parse-cache and its deprecated name cache-tm. The save job left a parse + # cache to restore, and no kapi command runs here, so whether the cache + # directory exists after setup shows which value the action acted on. When + # cache-tm is set, it decides. + test-parse-cache-input: + name: "Parse cache input (${{ matrix.case }})" + needs: test-state-cache-save + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + include: + - case: "parse-cache: false" + parse_cache: "false" + cache_tm: "" + restored: "false" + - case: "cache-tm: false" + parse_cache: "true" + cache_tm: "false" + restored: "false" + - case: "cache-tm: true over parse-cache: false" + parse_cache: "false" + cache_tm: "true" + restored: "true" + steps: + - uses: actions/checkout@v6 + + - name: Setup kapi + uses: ./ + with: + token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} + version: ${{ env.STATE_CACHE_KAPI_VERSION }} + plugins: "" + project-dir: test/fixture + parse-cache: ${{ matrix.parse_cache }} + cache-tm: ${{ matrix.cache_tm }} + + - name: Check whether the parse cache was restored + shell: bash + env: + WANT_RESTORED: ${{ matrix.restored }} + run: | + docs=test/fixture/.kapi/work/cache/docs + if [ "${WANT_RESTORED}" = "true" ]; then + test -s "${docs}/index.db" + else + test ! -e "${docs}" + fi + + # The step that names the parse cache directory, run out of action.yml for + # each value KAPI_PARSE_CACHE_DIR can have, and the two paths both parse + # cache steps carry. + test-parse-cache-dir: + name: "Parse cache directory (KAPI_PARSE_CACHE_DIR)" + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v6 + + - name: Run the parse cache directory harness + shell: bash + run: | + yq --version + test/parse-cache/env.sh + + # Released kapi versions ignore KAPI_PARSE_CACHE_DIR and keep the parse + # cache in the checkout, which the action also carries. + test-parse-cache-in-tree: + name: "Parse cache in the checkout (kapi ${{ matrix.version }})" + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + version: ["1.3.0", "1.4.0-rc5"] + steps: + - uses: actions/checkout@v6 + + - name: Setup kapi + uses: ./ + with: + token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} + version: ${{ matrix.version }} + plugins: "" + project-dir: test/fixture + + - name: Check the exported directory and the in-tree cache + shell: bash + run: | + test "${KAPI_PARSE_CACHE_DIR}" = "${RUNNER_TEMP}/kapi-parse-cache" + kapi status -p test/fixture/kapi.yaml > /dev/null + test -s test/fixture/.kapi/work/cache/docs/index.db + test ! -e "${KAPI_PARSE_CACHE_DIR}" + test-cache: name: "Cache hit (${{ matrix.os }})" runs-on: ${{ matrix.os }} @@ -274,17 +386,27 @@ jobs: steps: - uses: actions/checkout@v6 - # First install — populates cache + # First install: populates the cache. - name: Setup kapi (first) uses: ./ + # No bowrain build is listed for Windows until the next kapi release + # (registry#6, a founder decision), so the Windows run installs no + # plugin and the step below asserts the install still fails there. with: token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} + plugins: ${{ matrix.os != 'windows-latest' && 'bowrain' || '' }} - # Second install — should hit cache + # Second install: should hit the cache. - name: Setup kapi (cached) uses: ./ with: token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} + plugins: ${{ matrix.os != 'windows-latest' && 'bowrain' || '' }} + + - name: Assert no bowrain build is listed for Windows + if: runner.os == 'Windows' + shell: bash + run: test/plugins/windows-unlisted.sh - name: Verify kapi works from cache shell: bash @@ -292,16 +414,17 @@ jobs: # A cache hit installs no plugin, so check the cached one is current. - name: Verify the cached plugin is current + if: runner.os != 'Windows' shell: bash run: test/plugins/matches-fresh.sh bowrain # The plugin cache across a kapi upgrade. The seed job sets up an older kapi - # and leaves an older bowrain in the plugin root, as a cache saved before a - # newer bowrain was published would hold; the action saves that at job end. + # and leaves a bowrain in the plugin root that a fresh install by the newest + # kapi would not pick, as a cache saved for another kapi or registry entry + # would hold; the action saves that at job end. # The upgrade job then sets up the newest kapi with the same plugins input, # and the bowrain it is left with must be the one this kapi installs fresh. - # Windows is absent because the registry has only one Windows bowrain build, - # so no older one can be cached there. + # Windows is absent because the registry lists no Windows bowrain build. test-plugin-seed: name: "Plugin cache seed (${{ matrix.os }})" runs-on: ${{ matrix.os }} @@ -318,12 +441,12 @@ jobs: token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} version: ${{ env.PLUGIN_SEED_KAPI_VERSION }} plugins: bowrain - cache-tm: "false" + parse-cache: "false" - - name: Leave an older bowrain in the plugin root + - name: Leave a bowrain a fresh install would not pick shell: bash run: | - kapi plugin install "bowrain@${PLUGIN_SEED_BOWRAIN_VERSION}" + kapi plugin install --channel beta "bowrain@${PLUGIN_SEED_BOWRAIN_VERSION}" grep -q "\"${PLUGIN_SEED_BOWRAIN_VERSION}\"" ~/.local/share/kapi/plugins/bowrain/installed.json test-plugin-upgrade: @@ -343,7 +466,7 @@ jobs: with: token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} plugins: bowrain - cache-tm: "false" + parse-cache: "false" - name: Require a newer kapi than the seed shell: bash @@ -373,7 +496,7 @@ jobs: with: token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} plugins: bowrain - cache-tm: "false" + parse-cache: "false" - name: Require a plugin cache hit shell: bash @@ -384,3 +507,35 @@ jobs: - name: Verify bowrain matches a fresh install shell: bash run: test/plugins/matches-fresh.sh bowrain + + # A token passed as a plain value through the real action reaches later + # steps intact, and its line break starts no other variable. The token's + # second line has the shape of an assignment. + test-auth-env: + name: "Auth token through GITHUB_ENV (${{ matrix.os }})" + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + steps: + - uses: actions/checkout@v6 + + - name: Setup kapi with a dummy token + uses: ./ + with: + token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} + plugins: "" + auth-token: "dummy-token-a\nSETUP_KAPI_INJECTED=1" + server: https://bowrain.invalid + + - name: Read the variables back + shell: bash + run: | + want="$(printf 'dummy-token-a\nSETUP_KAPI_INJECTED=1')" + test "${BOWRAIN_AUTH_TOKEN}" = "${want}" + test "${BOWRAIN_SERVER_URL}" = "https://bowrain.invalid" + test -z "${SETUP_KAPI_INJECTED+set}" + if env | grep -q '^SETUP_KAPI_EOF'; then exit 1; fi + echo "BOWRAIN_AUTH_TOKEN and BOWRAIN_SERVER_URL arrived intact; no other variable was set." + diff --git a/README.md b/README.md index bef1e8b..93ad123 100644 --- a/README.md +++ b/README.md @@ -42,15 +42,12 @@ needs credentials: steps: - uses: neokapi/setup-kapi@v1 with: - version: "1.2.0-rc32" auth-token: ${{ secrets.BOWRAIN_AUTH_TOKEN }} server: https://your.bowrain.server - run: kapi up ``` -The newest stable release, which `latest` installs, has no `kapi up`, so this example pins the prerelease that has it. - `kapi up` runs the kapi loop: with the bowrain plugin installed and a `server:` block in the recipe, it pushes, catches up on the server, and pulls the produced targets back. To run it and commit the results, pair this with [`kapi-action`](https://github.com/neokapi/kapi-action). ## Inputs @@ -59,11 +56,12 @@ The newest stable release, which `latest` installs, has no `kapi up`, so this ex |-------|-------------|---------|----------| | `version` | Kapi CLI version (e.g. `1.1.0`), or `latest` for the newest stable CLI release | `latest` | No | | `token` | GitHub token for release downloads and API rate limits | `${{ github.token }}` | No | -| `plugins` | Newline- or comma-separated plugin refs to install, as the registry names them (`bowrain`, `okapi-bridge`; a `kapi-` prefix is stripped). Pass `''` to install nothing | `bowrain` | No | +| `plugins` | Newline- or comma-separated plugin refs to install, as the registry names them (`bowrain`, `okapi-bridge`; a `kapi-` prefix is stripped). Pass `''` to install nothing. No Bowrain plugin build is listed for Windows before the next kapi release, so a Windows job sets `plugins: ""` until then | `bowrain` | No | | `auth-token` | Bowrain server JWT, exported as `BOWRAIN_AUTH_TOKEN` | — | No | | `server` | Bowrain server URL, exported as `BOWRAIN_SERVER_URL` | — | No | -| `cache-tm` | Carry kapi's parse cache (`.kapi/work/cache/docs`) between runs with the job cache; see [Project parse cache](#project-parse-cache). Runs only when a `kapi.yaml` recipe (or legacy `*.kapi`) is present. Set `false` to disable | `true` | No | -| `project-dir` | Directory holding the `kapi.yaml` project whose parse cache is carried between runs | `.` | No | +| `parse-cache` | Keep kapi's parse cache between runs, so a later run replays files it already parsed; see [Project parse cache](#project-parse-cache). Runs only when a `kapi.yaml` recipe (or legacy `*.kapi`) is present. Set `false` to turn it off | `true` | No | +| `cache-tm` | Deprecated name of `parse-cache`. When set, it decides in place of `parse-cache`, and the action prints a warning | `''` | No | +| `project-dir` | Directory holding the `kapi.yaml` project whose parse cache is kept between runs | `.` | No | ## Outputs @@ -75,7 +73,7 @@ The newest stable release, which `latest` installs, has no `kapi up`, so this ex ### With the Okapi bridge (Java plugin) The `okapi-bridge` plugin runs Okapi Framework filters as a Java subprocess, -so the runner needs a JVM — Java 11+, or Java 17+ for Okapi 1.48.0 and later. +so the runner needs a JVM: Java 11+, or Java 17+ for Okapi 1.48.0 and later. GitHub's hosted Ubuntu runners ship a default JDK; pin one explicitly with `setup-java` when you need a specific version: @@ -98,13 +96,13 @@ is paid once for each kapi version and plugin release. ## How it works -1. **Resolve version** — `latest` resolves the newest *stable CLI* release (a `vX.Y.Z` tag). It deliberately does not use GitHub's "latest release" flag: the same repository publishes plugin and app releases (`check-v0.1.0`, `asr-v0.1.1`, `bowrain-v…`), and that flag lands on whichever was published last. Pinned versions pass through. -2. **Cache check** — restores a cached binary keyed on `kapi-{version}-{os}-{arch}`. -3. **Download + verify** (on cache miss) — downloads the archive and `checksums.txt` from the GitHub release, verifies the SHA-256 checksum, and extracts the binary. -4. **Add to PATH** — makes `kapi` available to all subsequent steps. -5. **Configure auth** (optional) — exports `BOWRAIN_AUTH_TOKEN`/`BOWRAIN_SERVER_URL` when `auth-token` is set. -6. **Install plugins** — installs each ref in `plugins` (default: `bowrain`) via `kapi plugins install`, cached as described below. Refs use the registry names; a `kapi-` binary prefix is stripped (`kapi-bowrain` → `bowrain`). -7. **Restore the project parse cache** (when a `kapi.yaml` recipe, or legacy `*.kapi`, is present): restores `.kapi/work/cache/docs` from the job cache, and saves it again at job end under a key unique to the job and run attempt. See [Project parse cache](#project-parse-cache). Disable with `cache-tm: false`. +1. **Resolve version**: `latest` resolves the newest *stable CLI* release (a `vX.Y.Z` tag). It deliberately does not use GitHub's "latest release" flag: the same repository publishes plugin and app releases (`check-v0.1.0`, `asr-v0.1.1`, `bowrain-v…`), and that flag lands on whichever was published last. Pinned versions pass through. +2. **Cache check**: restores a cached binary keyed on `kapi-{version}-{os}-{arch}`. +3. **Download + verify** (on cache miss): downloads the archive and `checksums.txt` from the GitHub release, verifies the SHA-256 checksum, and extracts the binary. +4. **Add to PATH**: makes `kapi` available to all subsequent steps. +5. **Configure auth** (optional): exports `BOWRAIN_AUTH_TOKEN`/`BOWRAIN_SERVER_URL` when `auth-token` is set. +6. **Install plugins**: installs each ref in `plugins` (default: `bowrain`) via `kapi plugins install`, cached as described below. Refs use the registry names; a `kapi-` binary prefix is stripped (`kapi-bowrain` → `bowrain`). +7. **Restore the project parse cache** (when a `kapi.yaml` recipe, or legacy `*.kapi`, is present): restores kapi's parse cache from the job cache, and saves it again at job end under a key unique to the job and run attempt. See [Project parse cache](#project-parse-cache). Disable with `parse-cache: false`. ## Caching @@ -112,18 +110,24 @@ The binary is cached keyed on version + OS + arch. Plugins are cached keyed on t ### Project parse cache -kapi keeps the state it derives out of git, under `.kapi/work/` (the project's `.kapi/.gitignore` ignores `work/` and `filters.local.json`). With `cache-tm` on and a recipe in `project-dir`, the action restores one directory of it, `.kapi/work/cache/docs`, before your steps run, and `actions/cache` saves it again when the job ends. kapi records there how it parsed each source and target file, so a later run can replay a file instead of parsing it again. +kapi records how it parsed each source and target file, so a later run can replay a file instead of parsing it again. With `parse-cache` on and a recipe in `project-dir`, the action restores that parse cache before your steps run, and `actions/cache` saves it again when the job ends. + +The action sets `KAPI_PARSE_CACHE_DIR` to a directory under `runner.temp` for the rest of the job, and caches that directory together with `/.kapi/work/cache/docs`. A kapi that reads the variable keeps its parse cache there; kapi 1.3 and earlier keep it in the checkout. If your workflow sets `KAPI_PARSE_CACHE_DIR` itself, the action keeps your value and caches it when it lies under `runner.temp`. On another CI system, set `KAPI_PARSE_CACHE_DIR` to a fixed absolute directory outside the repository and cache that directory. Let kapi create it, which it does with access for your user only. kapi stops with an error when the directory is inside the repository, is a link, belongs to another user, or gives group or other users any access. + +A restored parse cache does not change any result. kapi keys each entry by the file's path and content hash, the parse configuration, the recipe and the kapi build, and parses the file again when any of them differs. The test workflow checks this on every change for the parse cache kept in the checkout (kapi 1.2): with the cache restored, `kapi status`, `kapi check`, `kapi check --ship` and `kapi up` must match a cold run exactly, in exit codes, output and every file written, and they must still match after the source changes. -A restored parse cache does not change any result. kapi keys each entry by the file's path and content hash, the parse configuration, the recipe and the kapi build, and parses the file again when any of them differs. The test workflow checks this on every change: with the cache restored, `kapi status`, `kapi check`, `kapi check --ship` and `kapi up` must match a cold run exactly, in exit codes, output and every file written, and they must still match after the source changes. +The action carries the parse cache and nothing else. The project store holds this checkout's blocks and the targets a run wrote, and a restored copy changes what `kapi status`, `kapi check --ship` and `kapi up` report. The extraction batches, the redaction vault and the sync state (with its claim token) belong to the checkout that wrote them. The project's context (its terms, voice profiles, content memory and recorded decisions) lives in kapi's workspace and moves between machines through a context backend (kapi 1.3 and later). A job that needs it runs `kapi context pull` before kapi works and `kapi context push` after a run records something. [`kapi-action`](https://github.com/neokapi/kapi-action)'s `context-sync` input runs both around its command, or your workflow can run them as steps of its own. -Everything else under `.kapi/` stays out of the cache: +Each run saves its own cache, keyed by the runner OS, the resolved kapi version, the ref, the job and the run. A run restores the newest cache for the same kapi version, from its own ref first. The key does not change with your source files: a cache from before a source edit still holds the entries for every file the edit left alone. A new kapi version starts from an empty cache. How much time the cache saves depends on the formats: container formats such as DOCX replay faster than they parse, while JSON and Markdown parse about as fast as they replay. -- The content memory, terms, voice profile and unit-state record are committed under `.kapi/`, so the checkout already holds them, and kapi builds its local store from them. -- `.kapi/work/store.db` also holds stored targets and staged review decisions. Restored from an earlier run, it reports targets the checkout does not hold, and `kapi status`, `kapi check --ship` and `kapi up` report differently than they would on a cold run. -- `.kapi/work/cache/extractions/` holds `kapi extract` batches for `kapi merge`, and `.kapi/work/cache/redaction/` and `.kapi/work/vault/` hold withheld original values. -- `.kapi/work/cache/sync-cache.json` and `.kapi/work/cache/refs.json` hold server sync state, including a claim token. +If kapi stops with `KAPI_PARSE_CACHE_DIR is refused`, the directory it names is one kapi will not use, for example because something made it readable by other users. `actions/cache` saves only from a job that succeeds, so such a directory reaches the cache only when a workflow changes it after kapi's last run or lets a failed kapi step pass. To clear it, list the repository's parse cache entries and delete them: + +```bash +gh cache list --key kapi-parse- --limit 1000 +gh cache list --key kapi-parse- --limit 1000 --json id --jq '.[].id' | xargs -n 1 gh cache delete +``` -The cache key holds the runner OS, the resolved kapi version and the ref, so a new kapi version starts from an empty cache. How much time the cache saves depends on the formats: container formats such as DOCX replay faster than they parse, while JSON and Markdown parse about as fast as they replay. +kapi trusts what it finds in its parse cache, so who can write the cache decides whether a restored entry can be trusted. GitHub scopes caches by branch: a run restores only caches saved on its own branch, on the default branch, or, for a pull request, on its base branch. A cache saved by a pull request, including one from a fork, is never restored on the default branch. A `pull_request_target`, `workflow_run` or `issue_comment` run works in the default or base branch's scope, often for someone who cannot push, so in those runs the action restores the parse cache and does not save it. Any other workflow that runs untrusted code with this action can write to the cache of the branch it runs on; set `parse-cache: false` in such a workflow. ## Platform support diff --git a/action.yml b/action.yml index 7909f2e..ef45d2f 100644 --- a/action.yml +++ b/action.yml @@ -30,22 +30,26 @@ inputs: description: "Bowrain server URL (exported as BOWRAIN_SERVER_URL)" required: false default: "" - cache-tm: + parse-cache: description: >- - Carry kapi's parse cache (.kapi/work/cache/docs) between CI runs with the - job cache. kapi keeps the state it derives out of git, under .kapi/work/. - The content memory, terms and unit-state record are committed under - .kapi/, and kapi builds its local store from them in a fresh checkout, so - they need no cache. Only the parse cache is restored: each entry is keyed - by the file's content, the parse configuration, the recipe and the kapi - build, and kapi parses a file again when any of them differs. The store - and the rest of .kapi/work/ are never restored, because restoring them - changes what kapi status, kapi check and kapi up report. Runs only when a - kapi.yaml recipe (or legacy *.kapi) is present. Set 'false' to disable. + Keep kapi's parse cache between CI runs, so a later run replays the files + it already parsed instead of parsing them again. A restored entry never + changes a result: kapi parses a file again when its content, the parse + configuration, the recipe or the kapi build differs. Only the parse cache + is carried, never the project store or the project's context; a job that + needs the context runs `kapi context pull`. Runs only when a kapi.yaml + recipe (or legacy *.kapi) is present in project-dir. Set 'false' to turn + it off. required: false default: "true" + cache-tm: + description: >- + Deprecated: use parse-cache. When set, it decides in place of + parse-cache, and the action prints a warning. + required: false + default: "" project-dir: - description: "Directory holding the kapi.yaml project whose parse cache is carried between runs. Default: repository root." + description: "Directory holding the kapi.yaml project whose parse cache is kept between runs. Default: repository root." required: false default: "." @@ -70,6 +74,14 @@ runs: ACTION_PATH: ${{ github.action_path }} run: | VERSION=$("${ACTION_PATH}/scripts/resolve-version.sh" "${INPUT_VERSION}") + # The version names a release tag and becomes an output and a cache + # key, so it may hold only the characters a release version has. + case "${VERSION}" in + "" | *[!0-9A-Za-z.+-]*) + echo "::error title=setup-kapi::The version input is not a release version." + exit 1 + ;; + esac echo "version=${VERSION}" >> "$GITHUB_OUTPUT" echo "Resolved version: ${VERSION}" @@ -134,9 +146,21 @@ runs: INPUT_AUTH_TOKEN: ${{ inputs.auth-token }} INPUT_SERVER: ${{ inputs.server }} run: | - echo "BOWRAIN_AUTH_TOKEN=${INPUT_AUTH_TOKEN}" >> "$GITHUB_ENV" + # Mask the token before anything else touches it, each line on its + # own, so a token passed as a plain value stays out of later logs. + while IFS= read -r line; do + if [ -n "${line}" ]; then echo "::add-mask::${line}"; fi + done <<< "${INPUT_AUTH_TOKEN}" + # The multi-line form with a random delimiter, so a line break in a + # value cannot start another variable. + put_env() { + local delim + delim="SETUP_KAPI_EOF_$(od -An -N16 -tx1 /dev/urandom | tr -d ' \n')" + printf '%s<<%s\n%s\n%s\n' "$1" "${delim}" "$2" "${delim}" >> "$GITHUB_ENV" + } + put_env BOWRAIN_AUTH_TOKEN "${INPUT_AUTH_TOKEN}" if [ -n "${INPUT_SERVER}" ]; then - echo "BOWRAIN_SERVER_URL=${INPUT_SERVER}" >> "$GITHUB_ENV" + put_env BOWRAIN_SERVER_URL "${INPUT_SERVER}" fi echo "Auth configured for kapi" @@ -197,12 +221,36 @@ runs: kapi plugins install ${args[@]+"${args[@]}"} "${plugin}" done <<< "${PLUGINS}" - # 11. Detect a kapi project: a committed kapi.yaml recipe (or legacy *.kapi). + # 11. Resolve whether to carry the parse cache. cache-tm is the deprecated + # name of parse-cache; a workflow that still sets it keeps its meaning. + - name: Resolve parse cache input + id: parse-cache + shell: bash + env: + INPUT_PARSE_CACHE: ${{ inputs.parse-cache }} + INPUT_CACHE_TM: ${{ inputs.cache-tm }} + run: | + ENABLED="${INPUT_PARSE_CACHE}" + if [ -n "${INPUT_CACHE_TM}" ]; then + echo "::warning title=setup-kapi::The cache-tm input is deprecated; use parse-cache instead." + ENABLED="${INPUT_CACHE_TM}" + fi + # Only 'false' turns the cache off, compared without regard to case + # as v1's expression `inputs.cache-tm != 'false'` did; write nothing + # else of the input to the output. + if [ "$(printf '%s' "${ENABLED}" | tr '[:upper:]' '[:lower:]')" = "false" ]; then + echo "enabled=false" >> "$GITHUB_OUTPUT" + else + echo "enabled=true" >> "$GITHUB_OUTPUT" + fi + + # 12. Detect a kapi project: a committed kapi.yaml recipe (or legacy *.kapi). # The parse cache is carried only for a project. The recipe is the - # signal because .kapi/work/ is gitignored and absent from a checkout. + # signal because kapi's state is never committed, so a fresh checkout + # holds none. - name: Detect kapi project id: detect-project - if: inputs.cache-tm != 'false' + if: steps.parse-cache.outputs.enabled != 'false' shell: bash env: PROJECT_DIR: ${{ inputs.project-dir }} @@ -214,33 +262,112 @@ runs: echo "found=false" >> "$GITHUB_OUTPUT" fi - # 12. Restore kapi's parse cache, and save it at job end. The key is unique - # to the job and run attempt, so actions/cache saves through its own - # post step; the restore takes the newest cache for the same kapi - # version, preferring the same ref. + # 13. Name the directory kapi keeps its parse cache in. A kapi that reads + # KAPI_PARSE_CACHE_DIR keeps the parse cache there; a kapi up to 1.3 + # ignores the variable and keeps it in the checkout, at + # .kapi/work/cache/docs. The cache step carries both paths, and the one + # the installed kapi does not use is absent. Both are the same strings + # on every run, which keeps the cache version stable. # - # Only .kapi/work/cache/docs is carried. kapi keys each entry by the - # file's absolute path and content hash, the parse configuration, the - # recipe and the kapi build, and parses again on any mismatch, so a - # restored entry is either valid for the checkout or ignored. The rest - # of .kapi/work/ stays out of the cache: - # - store.db holds stored targets and the unit working set; restored, - # it changes what kapi status, kapi check --ship and kapi up report. - # - cache/extractions holds kapi extract batches that kapi merge reads. - # - cache/redaction and vault/ hold withheld original values. - # - cache/sync-cache.json and cache/refs.json hold server sync state, - # including a claim token. - # The content memory, terms and unit-state record are committed under - # .kapi/ and come with the checkout. + # A KAPI_PARSE_CACHE_DIR the workflow set itself is kept, and cached + # when it lies under runner.temp; otherwise the parse cache is not + # carried. (kapi refuses a directory inside the checkout.) + # + # The step creates no directory. kapi creates the parse cache + # directory with owner-only permissions (0700) and refuses one that is + # wider, a link, or owned by someone else; a directory made here with + # a plain mkdir would be 0755 and be saved, and restored, that way. + - name: Set kapi parse cache directory + id: parse-cache-dir + if: steps.parse-cache.outputs.enabled != 'false' && steps.detect-project.outputs.found == 'true' + shell: bash + env: + FIXED_DIR: ${{ runner.temp }}/kapi-parse-cache + run: | + if [ -z "${KAPI_PARSE_CACHE_DIR:-}" ]; then + echo "KAPI_PARSE_CACHE_DIR=${FIXED_DIR}" >> "$GITHUB_ENV" + echo "dir=${FIXED_DIR}" >> "$GITHUB_OUTPUT" + exit 0 + fi + dir="${KAPI_PARSE_CACHE_DIR}" + skip() { + echo "::warning title=setup-kapi::KAPI_PARSE_CACHE_DIR $1, so the parse cache is not carried." + exit 0 + } + case "${dir}" in + *[[:cntrl:]]*) skip "contains a control character" ;; + esac + base="${RUNNER_TEMP%[/\\]}" + [ -n "${base}" ] || skip "cannot be checked, because RUNNER_TEMP is not set" + case "${dir}" in + "${base}"/* | "${base}"\\*) rel="${dir:${#base}+1}" ;; + *) skip "(${dir}) is not under runner.temp" ;; + esac + # Count the components below runner.temp, ignoring empty ones and + # '.'; a '..' refuses the value outright. + IFS='/\' read -ra parts <<< "${rel}" + depth=0 + for part in ${parts[@]+"${parts[@]}"}; do + case "${part}" in + "" | .) ;; + ..) skip "(${dir}) has a '..' component" ;; + *) depth=$((depth + 1)) ;; + esac + done + [ "${depth}" -gt 0 ] || skip "(${dir}) names runner.temp itself" + echo "dir=${dir}" >> "$GITHUB_OUTPUT" + + # 14. Restore kapi's parse cache, and save it at job end. The key is unique + # to the job and run attempt, so every run saves its own cache through + # actions/cache's post step, and the restore takes the newest cache for + # the same kapi version, preferring the same ref. The key hashes no + # source file: kapi keys each entry by the file's path and content + # hash, the parse configuration, the recipe and the kapi build, and + # parses again on any mismatch, so an older cache is still useful after + # a source edit, and a restored entry is either valid for the checkout + # or ignored. + # + # Only the parse cache is carried. The project store, the extraction + # batches, the redaction vault and the sync state (with its claim + # token) stay out: a restored store changes what kapi status, kapi + # check --ship and kapi up report, and the rest belongs to the + # checkout that wrote it. The project's context lives in kapi's + # workspace; a job fetches it with `kapi context pull`, which this + # action leaves to kapi-action's context-sync input or the workflow's + # own step. # # The kapi version is part of the key because the parse cache keeps the # entries every build wrote; a new version starts from an empty cache. + # An underscore ends the version, a character no release version holds, + # so the restore prefix for 1.4.0 does not match a 1.4.0-rc5 cache. + # + # GitHub scopes caches by ref: a run restores caches its own ref saved + # and those of the default branch (and a pull request's base), never + # another branch's or a pull request's, so a pull request from a fork + # cannot reach the default branch's runs through this cache. A + # pull_request_target, workflow_run or issue_comment run executes in + # the default (or base) branch's scope and often on behalf of someone + # who cannot push, so there the cache is restored and never saved. - name: Restore kapi parse cache - if: inputs.cache-tm != 'false' && steps.detect-project.outputs.found == 'true' + if: steps.parse-cache-dir.outputs.dir != '' && !contains(fromJSON('["pull_request_target","workflow_run","issue_comment"]'), github.event_name) uses: actions/cache@v5 with: - path: ${{ inputs.project-dir }}/.kapi/work/cache/docs - key: kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}-${{ github.ref_name }}-${{ github.job }}-${{ github.run_id }}-${{ github.run_attempt }} + path: | + ${{ steps.parse-cache-dir.outputs.dir }} + ${{ inputs.project-dir }}/.kapi/work/cache/docs + key: kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}_${{ github.ref_name }}-${{ github.job }}-${{ github.run_id }}-${{ github.run_attempt }} + restore-keys: | + kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}_${{ github.ref_name }}- + kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}_ + + - name: Restore kapi parse cache (no save) + if: steps.parse-cache-dir.outputs.dir != '' && contains(fromJSON('["pull_request_target","workflow_run","issue_comment"]'), github.event_name) + uses: actions/cache/restore@v5 + with: + path: | + ${{ steps.parse-cache-dir.outputs.dir }} + ${{ inputs.project-dir }}/.kapi/work/cache/docs + key: kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}_${{ github.ref_name }}-${{ github.job }}-${{ github.run_id }}-${{ github.run_attempt }} restore-keys: | - kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}-${{ github.ref_name }}- - kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}- + kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}_${{ github.ref_name }}- + kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}_ diff --git a/test/parse-cache/env.sh b/test/parse-cache/env.sh new file mode 100755 index 0000000..1477167 --- /dev/null +++ b/test/parse-cache/env.sh @@ -0,0 +1,152 @@ +#!/usr/bin/env bash +# Runs the action's "Set kapi parse cache directory" step, read out of +# action.yml, for each value KAPI_PARSE_CACHE_DIR can have when the step +# starts, and asserts what it exports and outputs. Then it asserts that both +# parse cache steps carry the directory the step names and the in-tree path a +# kapi up to 1.3 uses. Needs yq (v4). +set -euo pipefail + +here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +root="$(cd "${here}/../.." && pwd)" +work="$(mktemp -d)" +trap 'rm -rf "${work}"' EXIT + +yq '.runs.steps[] | select(.id == "parse-cache-dir") | .run' "${root}/action.yml" > "${work}/step.sh" +if ! grep -q KAPI_PARSE_CACHE_DIR "${work}/step.sh"; then + echo "could not read the parse-cache-dir step out of action.yml" >&2 + exit 1 +fi + +failures=0 +fail() { + echo "FAIL $1: $2" + failures=$((failures + 1)) +} + +temp="${work}/runner-temp" +workspace="${work}/workspace" + +# run_case NAME PRESET WANT_DIR WANT_ENV WANT_WARNING: PRESET is the value of +# KAPI_PARSE_CACHE_DIR before the step ("" for unset). WANT_ENV is the line the +# step must append to GITHUB_ENV ("" for none). WANT_WARNING is "yes" or "no". +run_case() { + local name="$1" preset="$2" want_dir="$3" want_env="$4" want_warning="$5" + local dir="${work}/cases/${name}" + mkdir -p "${dir}" + : > "${dir}/env" + : > "${dir}/output" + local rc=0 + if [ -n "${preset}" ]; then + env KAPI_PARSE_CACHE_DIR="${preset}" FIXED_DIR="${temp}/kapi-parse-cache" \ + RUNNER_TEMP="${temp}" GITHUB_WORKSPACE="${workspace}" \ + GITHUB_ENV="${dir}/env" GITHUB_OUTPUT="${dir}/output" \ + bash -e "${work}/step.sh" > "${dir}/stdout" 2>&1 || rc=$? + else + env -u KAPI_PARSE_CACHE_DIR FIXED_DIR="${temp}/kapi-parse-cache" \ + RUNNER_TEMP="${temp}" GITHUB_WORKSPACE="${workspace}" \ + GITHUB_ENV="${dir}/env" GITHUB_OUTPUT="${dir}/output" \ + bash -e "${work}/step.sh" > "${dir}/stdout" 2>&1 || rc=$? + fi + local before="${failures}" + [ "${rc}" = 0 ] || fail "${name}" "exit code ${rc}" + local got + got="$(sed -n 's/^dir=//p' "${dir}/output")" + [ "${got}" = "${want_dir}" ] || fail "${name}" "dir: got '${got}', want '${want_dir}'" + got="$(cat "${dir}/env")" + [ "${got}" = "${want_env}" ] || fail "${name}" "GITHUB_ENV: got '${got}', want '${want_env}'" + if [ "${want_warning}" = yes ]; then + grep -q '^::warning' "${dir}/stdout" || fail "${name}" "printed no warning" + else + ! grep -q '^::warning' "${dir}/stdout" || fail "${name}" "unexpected warning: $(cat "${dir}/stdout")" + fi + if [ -z "${want_dir}" ] && [ -s "${dir}/output" ]; then + fail "${name}" "wrote outputs: $(cat "${dir}/output")" + fi + [ ! -e "${temp}" ] || fail "${name}" "created a directory" + if [ "${failures}" = "${before}" ]; then + echo "ok ${name}" + fi +} + +run_case unset "" "${temp}/kapi-parse-cache" "KAPI_PARSE_CACHE_DIR=${temp}/kapi-parse-cache" no +run_case preset-temp "${temp}/mine" "${temp}/mine" "" no +run_case preset-workspace "${workspace}/.cache/kapi" "" "" yes +run_case preset-outside "/home/runner/.cache/kapi" "" "" yes +run_case preset-relative "kapi-parse-cache" "" "" yes +run_case preset-dotdot "${temp}/../elsewhere" "" "" yes +run_case preset-prefix "${temp}-other/cache" "" "" yes +run_case preset-newline "${temp}/x +dir=/etc/ssl" "" "" yes +run_case preset-cr "${temp}/x$(printf '\r')" "" "" yes +run_case preset-temp-itself "${temp}" "" "" yes +run_case preset-temp-slash "${temp}/" "" "" yes +run_case preset-temp-dot "${temp}/." "" "" yes +run_case preset-temp-dots "${temp}/./" "" "" yes +run_case preset-temp-nested "${temp}/a/./b" "${temp}/a/./b" "" no + +# The auth step writes each value with a random delimiter, so a line break in +# a token cannot add another variable. +yq '.runs.steps[] | select(.name == "Configure auth") | .run' "${root}/action.yml" > "${work}/auth.sh" +: > "${work}/auth.env" +env INPUT_AUTH_TOKEN="tok +BASH_ENV=/tmp/evil" INPUT_SERVER="https://example.invalid" GITHUB_ENV="${work}/auth.env" \ + bash -e "${work}/auth.sh" > /dev/null +names="$(awk 'd != "" { if ($0 == d) d = ""; next } /< "${work}/enabled.sh" +# enabled_case PARSE_CACHE CACHE_TM WANT +enabled_case() { + : > "${work}/enabled.out" + env INPUT_PARSE_CACHE="$1" INPUT_CACHE_TM="$2" GITHUB_OUTPUT="${work}/enabled.out" \ + bash -e "${work}/enabled.sh" > /dev/null + local got + got="$(cat "${work}/enabled.out")" + if [ "${got}" = "enabled=$3" ]; then + echo "ok parse-cache='$1' cache-tm='$2': $3" + else + fail enabled "parse-cache='$1' cache-tm='$2': got '${got}', want 'enabled=$3'" + fi +} +for value in false False FALSE; do + enabled_case "${value}" "" false + enabled_case true "${value}" false +done +enabled_case true "" true +enabled_case false true true +enabled_case "no" "" true + + +# Both parse cache steps carry the directory the step names and the in-tree +# path, as fixed strings. +for name in "Restore kapi parse cache" "Restore kapi parse cache (no save)"; do + paths="$(NAME="${name}" yq '.runs.steps[] | select(.name == strenv(NAME)) | .with.path' "${root}/action.yml")" + # shellcheck disable=SC2016 # the literal expression text in action.yml + want='${{ steps.parse-cache-dir.outputs.dir }} +${{ inputs.project-dir }}/.kapi/work/cache/docs' + if [ "${paths}" = "${want}" ]; then + echo "ok ${name}: both paths" + else + fail "${name}" "path: got '${paths}'" + fi +done + +if [ "${failures}" -gt 0 ]; then + echo "${failures} assertion(s) failed" + exit 1 +fi +echo "all parse cache directory cases passed" diff --git a/test/plugins/windows-unlisted.sh b/test/plugins/windows-unlisted.sh new file mode 100755 index 0000000..7ef9e59 --- /dev/null +++ b/test/plugins/windows-unlisted.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash +# Asserts that the registry lists no bowrain plugin build for Windows: a +# default install fails and names the platform. registry#6 left Windows +# without a listed build until the next kapi release, by decision. When this +# script fails because the install succeeds, a Windows build is listed again: +# restore the default `plugins` input in the Windows jobs and delete this +# script. +set -uo pipefail + +out="$(kapi plugin install bowrain 2>&1)" +rc=$? +echo "${out}" +if [ "${rc}" -eq 0 ]; then + echo "::error::bowrain installed on Windows, so a Windows build is listed again. Restore the default plugins input in the Windows jobs and delete test/plugins/windows-unlisted.sh." + exit 1 +fi +if ! grep -q 'no version of plugin "bowrain"' <<< "${out}" || ! grep -q 'for platform windows/' <<< "${out}"; then + echo "::error::bowrain failed to install on Windows for a reason other than a missing Windows build." + exit 1 +fi +echo "As expected: no bowrain build is listed for Windows."