iterate.com / kernel

git clone https://git.iterate.com/repos/kernel

kernel / .github / workflows / npm-publish.ymlmain at 949894f

name: npm
# Publishes `core/lib` of this repository to npm as `iterate`, the SDK and the CLI
# (.github/npm-publish.mjs says how). It runs in the mirror github.com/iterate/kernel: npm's trusted
# publishing takes an OIDC token from GitHub's own runners alone. The package's settings on
# npmjs.com name that repository, this file and the environment `npm`.
#
# A push to main publishes the package under the tag `main` and moves `latest` to it, when its
# packed files changed, with no person: `0.4.1-main.<commit date>-<sha>` says that it is a build
# of a commit and no release. A dispatch on main names something else to do with main's tip:
#
#   gh workflow run npm-publish.yml --repo iterate/kernel -f action=<dry-run | canary | main | check>
#
# `pack` is the only job that runs the package's code, and it has no OIDC token. `install` installs
# its tarball on computers that have nothing of ours. `publish` hands npm the tarball that both
# installs passed, with no script of it run. `check` asks npm, as this workflow, whether the
# package trusts it; it writes and removes the tag `npm-publish-check` and nothing else.
#
# WHO PUBLISHES: whoever can put a commit on main of the source, because mirror.yml brings every
# such commit here and this file is part of it. Misha chose that on 27 Nov 2026 for builds of main;
# a real release gets a person's approval when there is one.
on:
  push:
    branches:
      - main
  workflow_dispatch:
    inputs:
      action:
        description: "`dry-run` packs and installs, and publishes nothing. `canary` publishes and leaves `latest` alone. `main` publishes and moves `latest`. `check` asks npm whether the package trusts this workflow."
        type: choice
        options:
          - dry-run
          - canary
          - main
          - check
        default: dry-run
# One run at a time for a dist-tag, in order, and no run is cancelled: a publish that was cut off
# could leave `latest` behind `main`. A run that waits is replaced by a newer one, which publishes
# what both changed.
concurrency:
  group: npm-${{ inputs.action || 'main' }}
  cancel-in-progress: false
permissions: {}
jobs:
  pack:
    if: github.repository == 'iterate/kernel' && inputs.action != 'check'
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
      # no token of this repository stays on disk: the pack runs the package's own build
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
        with:
          persist-credentials: false
      - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7
        with:
          node-version-file: .nvmrc
      # npm 11 packs the tarball; pnpm is the version that package.json names
      - run: npm install --global npm@11 pnpm@12.18.0
      - run: node --test .github/
      # the version names the dist-tag, so the pack takes it; a dry run packs for `main`
      - run: node .github/npm-publish.mjs pack --dist-tag "$DIST_TAG" --out "$RUNNER_TEMP/tarball"
        env:
          DIST_TAG: ${{ inputs.action == 'canary' && 'canary' || 'main' }}
      - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
        with:
          name: tarball
          path: ${{ runner.temp }}/tarball/*.tgz
          if-no-files-found: error
  # A computer with nothing of ours installs the tarball: Node at the oldest version that the
  # package allows, its dependencies from npm, and of this repository the script alone.
  install:
    needs: pack
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest]
    runs-on: ${{ matrix.os }}
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
        with:
          persist-credentials: false
          sparse-checkout: .github
      - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7
        with:
          node-version: 22
      - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
        with:
          name: tarball
          path: ${{ runner.temp }}/tarball
      - run: node .github/npm-publish.mjs install --tarball "$(ls "$RUNNER_TEMP"/tarball/*.tgz)"
  # npm gets the tarball that both installs passed, as it is: no script of the package runs here.
  # Only main gets here: a push, or a dispatch that names a dist-tag. The environment `npm` takes
  # main alone.
  publish:
    needs: [pack, install]
    if: github.ref == 'refs/heads/main' && (github.event_name == 'push' || inputs.action == 'canary' || inputs.action == 'main')
    runs-on: ubuntu-latest
    environment: npm
    permissions:
      contents: read
      id-token: write
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
        with:
          persist-credentials: false
          sparse-checkout: .github
      - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7
        with:
          node-version: 26
      # trusted publishing needs npm 11.5.1 or later; an exact version, because this job holds
      # npm's token
      - run: npm install --global npm@11.21.3
      - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
        with:
          name: tarball
          path: ${{ runner.temp }}/tarball
      # Only main's tip writes to npm. A run of an older commit (one that waited, or a rerun) stops
      # here, so that no tag moves back; the tip's own run publishes what both changed.
      - id: tip
        run: |
          tip="$(git ls-remote "https://github.com/$GITHUB_REPOSITORY" refs/heads/main | cut -f1)"
          if [ "$tip" = "$GITHUB_SHA" ]; then echo "is=true" >> "$GITHUB_OUTPUT"; else echo "main is at $tip, not at $GITHUB_SHA: not published" | tee -a "$GITHUB_STEP_SUMMARY"; fi
      - if: steps.tip.outputs.is == 'true'
        run: node .github/npm-publish.mjs publish --tarball "$(ls "$RUNNER_TEMP"/tarball/*.tgz)" --dist-tag "$DIST_TAG"
        env:
          DIST_TAG: ${{ inputs.action || 'main' }}
  check:
    if: github.repository == 'iterate/kernel' && github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/main' && inputs.action == 'check'
    runs-on: ubuntu-latest
    environment: npm
    permissions:
      contents: read
      id-token: write
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
        with:
          persist-credentials: false
          sparse-checkout: .github
      - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7
        with:
          node-version: 26
      - run: node .github/npm-publish.mjs check