name: Publish # Publishes the npm package and tags the Go module. Run by hand from the # Actions tab (or `gh workflow run publish.yml -f npm=minor -f go=patch`). # # npm needs no token: the job publishes with OpenID Connect trusted # publishing, so npmjs.com must list this repository and workflow as a # trusted publisher of the `gex` package. That is set once, on npmjs.com # (package settings, Trusted Publisher: GitHub Actions, user `rjrodger`, # repository `gex`, workflow `publish.yml`, no environment) or with # `npm trust github gex --file publish.yml --repository rjrodger/gex # --allow-publish`. npm attaches provenance to a package published this # way. The Go module is published by pushing its tag; proxy.golang.org # picks it up from there. # # Each input is a version bump (`patch`, `minor`, `major`), an explicit # `x.y.z`, `current` to publish the version already in the tree, or `skip`. # With the defaults nothing new goes out while the versions in the tree are # already released. # # With `dry-run`, the run builds, tests and reports what it would release, # and publishes, tags and pushes nothing. It also checks that the job may # push, with a push that sends nothing. # # Each GitHub release says what CHANGELOG.md (npm) or go/CHANGELOG.md (Go) # says under the heading of its version, `## x.y.z`. Write that section # before the release. A version with no section is released all the same, # with a warning, and its GitHub release names the version and no more. # # Only master is released. A run that failed part way can be re-run from the # Actions tab (Re-run jobs). The versions are worked out from the commit the # run was started on, so a re-run works out the same ones, and each thing is # done only if missing: a version on the registry is not published again, a # release that was pushed is taken up from master, and the GitHub release is # made if it is not there. A new run started after a release was pushed # starts from the bumped version: give it `current`, not the bump again. on: workflow_dispatch: inputs: npm: description: 'npm (package.json): patch | minor | major | x.y.z | current | skip' required: true default: current go: description: 'Go (const Version in go/gex.go): patch | minor | major | x.y.z | current | skip' required: true default: current dry-run: description: 'Dry run: build, test and report the versions, but publish, tag and push nothing' type: boolean default: false permissions: contents: write id-token: write concurrency: group: publish cancel-in-progress: false jobs: publish: runs-on: ubuntu-latest timeout-minutes: 15 env: NPM_INPUT: ${{ inputs.npm }} GO_INPUT: ${{ inputs.go }} DRY_RUN: ${{ inputs.dry-run }} steps: - name: Only master is released if: github.ref != 'refs/heads/master' && !inputs.dry-run run: | echo "publish runs on master only, not $GITHUB_REF; use dry-run to try another branch" >&2 exit 1 # The credentials are kept, as the release is pushed from here. - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: fetch-depth: 0 - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: node-version: 24.x registry-url: https://registry.npmjs.org package-manager-cache: false - uses: actions/setup-go@d8cbb8045d7f43706b9177cceb36c1f91a6644d5 # v7.1.0 with: go-version-file: go/go.mod cache: false # The versions to release, worked out from the commit the run was # started on, before anything changes it. - name: Plan the versions id: plan run: | set -euo pipefail target() { # the version in the tree, the input node -e ' const [cur, input] = process.argv.slice(1) let out = input if ("current" === input) out = cur else if (["major", "minor", "patch"].includes(input)) { const v = cur.split(".").map(Number) const i = { major: 0, minor: 1, patch: 2 }[input] v[i]++ for (let j = i + 1; j < 3; j++) v[j] = 0 out = v.join(".") } if (!/^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$/.test(out)) { console.error("\"" + out + "\" is not an x.y.z version") process.exit(1) } console.log(out) ' "$1" "$2" } NPM_CUR=$(node -p "require('./package.json').version") GO_CUR=$(sed -n 's/^const Version = "\(.*\)"/\1/p' go/gex.go) NPM_V= GO_V= if [ "$NPM_INPUT" != skip ]; then NPM_V=$(target "$NPM_CUR" "$NPM_INPUT"); fi if [ "$GO_INPUT" != skip ]; then GO_V=$(target "$GO_CUR" "$GO_INPUT"); fi if [ -n "$GO_V" ]; then # From version 2 on, Go needs the major version in the module path. MODULE=$(sed -n 's/^module //p' go/go.mod) MAJOR=${GO_V%%.*} SUFFIX= if [ "$MAJOR" -ge 2 ]; then SUFFIX="/v$MAJOR"; fi if [ -n "$SUFFIX" ] && [[ "$MODULE" != *"$SUFFIX" ]]; then echo "go: v$GO_V needs a module path that ends in $SUFFIX, but go/go.mod names $MODULE" >&2; exit 1 fi if [ -z "$SUFFIX" ] && [[ "$MODULE" =~ /v[0-9]+$ ]]; then echo "go: v$GO_V does not fit the module path $MODULE in go/go.mod" >&2; exit 1 fi fi echo "npm: ${NPM_V:-skip} (the tree has $NPM_CUR), go: ${GO_V:-skip} (the tree has $GO_CUR)" { echo "npm_cur=$NPM_CUR" echo "npm_v=$NPM_V" echo "go_cur=$GO_CUR" echo "go_v=$GO_V" } >> "$GITHUB_OUTPUT" # The text of each GitHub release: what the changelog says under # the heading of the version, up to the next version's heading. notes() { # the changelog, the version, the text without one, the file to write : > "$4" if [ -z "$2" ]; then return 0; fi if [ -f "$1" ]; then awk -v v="$2" '/^## / { on = ($2 == v); next } on' "$1" > "$4" fi if grep -q '[^[:space:]]' "$4"; then echo "v$2: the release says the $(wc -l < "$4" | tr -d ' ') lines that $1 has for it" else echo "::warning::$1 has no section for $2, so its release says only: $3" echo "$3" > "$4" fi } notes CHANGELOG.md "$NPM_V" "npm package release v$NPM_V" "$RUNNER_TEMP/notes-npm.md" notes go/CHANGELOG.md "$GO_V" "Go module release v$GO_V" "$RUNNER_TEMP/notes-go.md" # Trusted publishing needs npm 11.5.1 or later. - run: npm install -g npm@latest && npm --version # No lockfile is committed, so this is an install and not `npm ci`. - name: Build and test both ports run: | npm install make build test - name: Git identity run: | git config user.name "github-actions[bot]" git config user.email "41898282+github-actions[bot]@users.noreply.github.com" # A dry run pushes nothing, but it can find out whether this job may # push, which a release does last, after the package is published. - name: Check that a release could be pushed if: inputs.dry-run run: git push --dry-run origin HEAD:refs/heads/publish-dry-run - name: Publish npm if: inputs.npm != 'skip' env: GH_TOKEN: ${{ github.token }} CUR: ${{ steps.plan.outputs.npm_cur }} V: ${{ steps.plan.outputs.npm_v }} run: | set -euo pipefail ON_REGISTRY=false if npm view "gex@$V" version >/dev/null 2>&1; then ON_REGISTRY=true; fi TAGGED=false if git rev-parse -q --verify "refs/tags/v$V" >/dev/null; then TAGGED=true; fi if [ "$DRY_RUN" = true ]; then if [ "$ON_REGISTRY" = true ]; then echo "Dry run: gex@$V is already on the registry; nothing to publish" else echo "Dry run: would publish v$V" npm version "$V" --no-git-tag-version --allow-same-version >/dev/null npm publish --dry-run --access public git checkout -- package.json fi exit 0 fi if [ "$TAGGED" = true ]; then # The tag is never moved. Either the release is out already, by # an earlier try of this run or before it, or the tag is on this # very commit and only the publish is left to do. TAG_SHA=$(git rev-parse "refs/tags/v$V^{commit}") if [ "$ON_REGISTRY" = true ]; then if [ "$CUR" != "$V" ] && ! git merge-base --is-ancestor "$GITHUB_SHA" "$TAG_SHA"; then echo "v$V was released from an earlier commit, but package.json here says $CUR" >&2; exit 1 fi echo "gex@$V is already on the registry and tagged; completing the release" elif [ "$TAG_SHA" = "$(git rev-parse HEAD)" ] && [ "$CUR" = "$V" ]; then echo "Publishing v$V, which is tagged on this commit" npm publish --access public else echo "v$V is already tagged on another commit, but gex@$V is not on the registry" >&2; exit 1 fi else echo "Publishing v$V" npm version "$V" --no-git-tag-version --allow-same-version >/dev/null git add package.json git diff --cached --quiet || git commit -m "v$V" git tag "v$V" # A version already on the registry is a run that failed between # publishing and pushing: finish it rather than publish again. if [ "$ON_REGISTRY" = true ]; then echo "gex@$V is already on the registry; completing the release" else npm publish --access public fi git push --atomic origin HEAD:master "v$V" fi # Carry on from master, which has the release whichever try pushed it. git fetch -q origin master git merge -q --ff-only FETCH_HEAD gh release view "v$V" >/dev/null 2>&1 || gh release create "v$V" --title "v$V" --notes-file "$RUNNER_TEMP/notes-npm.md" - name: Publish Go if: inputs.go != 'skip' env: GH_TOKEN: ${{ github.token }} CUR: ${{ steps.plan.outputs.go_cur }} V: ${{ steps.plan.outputs.go_v }} run: | set -euo pipefail TAGGED=false if git rev-parse -q --verify "refs/tags/go/v$V" >/dev/null; then TAGGED=true; fi if [ "$DRY_RUN" = true ]; then if [ "$TAGGED" = true ]; then echo "Dry run: go/v$V is already tagged; nothing to publish" else echo "Dry run: would tag go/v$V" fi exit 0 fi if [ "$TAGGED" = true ]; then # The tag is never moved: the release is out already, by an # earlier try of this run or before it. TAG_SHA=$(git rev-parse "refs/tags/go/v$V^{commit}") if [ "$CUR" != "$V" ] && ! git merge-base --is-ancestor "$GITHUB_SHA" "$TAG_SHA"; then echo "go/v$V was tagged on an earlier commit, but go/gex.go here says $CUR" >&2; exit 1 fi echo "go/v$V is already tagged; completing the release" else echo "Publishing go/v$V" sed -i "s/^const Version = \".*\"/const Version = \"$V\"/" go/gex.go go/README.md sed -i "s/^\*\*Version:\*\* \`.*\`/**Version:** \`$V\`/" go/README.md git add go/gex.go go/README.md git diff --cached --quiet || git commit -m "go: v$V" git tag "go/v$V" git push --atomic origin HEAD:master "go/v$V" fi git fetch -q origin master git merge -q --ff-only FETCH_HEAD gh release view "go/v$V" >/dev/null 2>&1 || gh release create "go/v$V" --title "go/v$V" --notes-file "$RUNNER_TEMP/notes-go.md" # Ask the proxy for the new version so it is cached at once. GOPROXY=https://proxy.golang.org GOFLAGS=-mod=mod go list -m "github.com/rjrodger/gex/go@v$V" || true