name: Release # Pushing a tag `vX.Y.Z` publishes that version to npm and creates the GitHub release. Create tags with the # release scripts (`npm run release:patch | release:minor | release:major | release:next`), not by hand. # Stable versions must be tagged on `main` and go to the `latest` dist-tag; prereleases (`v2.0.0-next.0`) must be # tagged on another branch and go to the `next` dist-tag. The workflow refuses a tag on the wrong branch. # # Three jobs, so that the code that builds the package never holds a credential: # - build (contents: read) runs the full check and packs the tarball. Dependencies, their install scripts # and the build tools run here, where no token can publish or push. # - publish (id-token: write) publishes that tarball with npm trusted publishing (OIDC) and provenance. It checks # out nothing and runs no scripts, so no repository code or dependency runs while # an npm token can be minted. It runs in the `npm` environment. # - release (contents: write) creates the GitHub release with generated notes. # # One-time setup: # - npmjs.com → package "create-request" → Settings → Trusted publisher → GitHub Actions: # organization/user: DanielAmenou · repository: create-request · workflow filename: release.yml # environment name: npm · and "Allow npm publish" ticked. With the environment name set, only the publish job # can publish, not another job of this workflow. # - GitHub → Settings → Environments → npm → Required reviewers: add yourself, so a pushed tag waits for your # approval before anything is published. Optional; without it the publish job starts as soon as build passes. # Trusted publishing needs npm ≥ 11.5.1, so the publish job installs npm 11. # Actions are pinned to commit SHAs (the tag is in the comment); Dependabot proposes updates. on: push: tags: ["v*"] permissions: {} jobs: build: name: Check and pack runs-on: ubuntu-latest permissions: contents: read steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: fetch-depth: 0 persist-credentials: false - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: node-version: 24 - name: The tag must match package.json (the dist-tag is derived from the tag, the version from package.json) run: test "v$(node -p "require('./package.json').version")" = "$GITHUB_REF_NAME" - name: Stable versions are released from main, prereleases from any other branch run: | on_main=no git merge-base --is-ancestor "$GITHUB_SHA" origin/main && on_main=yes case "$GITHUB_REF_NAME" in *-*) [ "$on_main" = no ] || { echo "::error::$GITHUB_REF_NAME is a prerelease: release it from a branch other than main"; exit 1; } ;; *) [ "$on_main" = yes ] || { echo "::error::$GITHUB_REF_NAME is a stable version: release it from main"; exit 1; } ;; esac - run: npm ci --ignore-scripts - run: npm run check - run: npm pack - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: package path: create-request-*.tgz if-no-files-found: error retention-days: 7 publish: name: Publish to npm needs: build runs-on: ubuntu-latest environment: npm permissions: id-token: write steps: - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: node-version: 24 registry-url: https://registry.npmjs.org - run: npm install -g npm@11 - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 with: name: package - name: Publish the tarball (trusted publishing, with provenance) run: npm publish ./create-request-*.tgz --provenance --access public --ignore-scripts ${{ contains(github.ref_name, '-') && '--tag next' || '' }} release: name: Create the GitHub release needs: publish runs-on: ubuntu-latest permissions: contents: write steps: - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 with: script: | const tag = context.ref.replace("refs/tags/", ""); const { data: notes } = await github.rest.repos.generateReleaseNotes({ owner: context.repo.owner, repo: context.repo.repo, tag_name: tag, target_commitish: context.sha }); await github.rest.repos.createRelease({ owner: context.repo.owner, repo: context.repo.repo, tag_name: tag, name: tag, body: notes.body, prerelease: tag.includes("-") });