diff --git a/.github/workflows/node-release-tag.yml b/.github/workflows/node-release-tag.yml new file mode 100644 index 0000000..4ca0f7e --- /dev/null +++ b/.github/workflows/node-release-tag.yml @@ -0,0 +1,173 @@ +name: Node Release Tag + +on: + workflow_call: + inputs: + version: + type: string + required: true + version-file: + type: string + default: 'package.json' + environment: + type: string + default: 'release' + secrets: + RELEASE_APP_PRIVATE_KEY: + required: false + MATRIX_ACCESS_TOKEN: + required: false + +jobs: + # runs no code of the project, it only rewrites the version in a JSON file, so it may hold the key of the GitHub App + tag: + runs-on: ubuntu-latest + # must differ from the group of the caller, equal groups deadlock + concurrency: + group: node-release-tag-job-${{ github.repository }} + cancel-in-progress: false + # holds the key of the GitHub App, its deployment branches decide which branches may release + environment: ${{ inputs.environment }} + cache-mode: none + permissions: + contents: read + steps: + - uses: $/.github/actions/check-trigger + with: + workflow: node-release-tag + dispatch-actor: ${{ vars.DISPATCH_ACTOR }} + + # a push with GITHUB_TOKEN starts no workflow, the tag would never be published + - name: Check the GitHub App + env: + CLIENT_ID: ${{ vars.RELEASE_APP_CLIENT_ID }} + run: | + if [ -z "$CLIENT_ID" ]; then + echo "::error::RELEASE_APP_CLIENT_ID is not set, a tag pushed with GITHUB_TOKEN would not start the release" + exit 1 + fi + + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - name: Check the inputs + env: + VERSION: ${{ inputs.version }} + VERSION_FILE: ${{ inputs.version-file }} + run: | + set -euo pipefail + if [[ ! "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]]; then + echo "::error::version '${VERSION}' is not a version like 1.2.3 or 1.2.3-beta.1, without a leading v" + exit 1 + fi + if [ ! -f "$VERSION_FILE" ]; then + echo "::error::version-file '${VERSION_FILE}' does not exist" + exit 1 + fi + + - name: Set the version + env: + VERSION: ${{ inputs.version }} + VERSION_FILE: ${{ inputs.version-file }} + run: | + set -euo pipefail + # npm version would run the version scripts of the project, this keeps indentation and key order + node -e ' + const fs = require("fs"); + const [file, version] = process.argv.slice(1); + const text = fs.readFileSync(file, "utf8"); + const data = JSON.parse(text); + if (typeof data.version !== "string") { + console.error("::error::" + file + " has no version"); + process.exit(1); + } + if (data.version === version) { + console.error("::error::" + file + " is at " + version + " already"); + process.exit(1); + } + const indent = text.match(/^[ \t]+(?=")/m)?.[0] ?? " "; + data.version = version; + fs.writeFileSync(file, JSON.stringify(data, null, indent) + text.slice(text.trimEnd().length)); + ' "$VERSION_FILE" "$VERSION" + # anything but the version line means the file was formatted differently, it is not rewritten then + CHANGES="$(git diff --numstat)" + if [ "$CHANGES" != "$(printf '1\t1\t%s' "$VERSION_FILE")" ]; then + echo "::error::setting the version would change more than one line, set it by hand:" + git diff + exit 1 + fi + + - name: Commit and tag + env: + VERSION: ${{ inputs.version }} + VERSION_FILE: ${{ inputs.version-file }} + ACTOR: ${{ github.actor }} + ACTOR_ID: ${{ github.actor_id }} + run: | + set -euo pipefail + git config user.name "$ACTOR" + # the noreply address with the id links the commits to the account, also after a rename + git config user.email "${ACTOR_ID}+${ACTOR}@users.noreply.github.com" + git -c core.hooksPath=/dev/null commit --quiet -m "chore(release): v${VERSION}" -- "$VERSION_FILE" + git -c core.hooksPath=/dev/null tag -a "v${VERSION}" -m "v${VERSION}" + + # valid for one hour and for this repository only, so it is created right before the push + - name: Create the push token + id: push-token + uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 + with: + client-id: ${{ vars.RELEASE_APP_CLIENT_ID }} + private-key: ${{ secrets.RELEASE_APP_PRIVATE_KEY }} + permission-contents: write + + # the tag starts the release workflow of the project, which publishes it with node-release.yml + - name: Push the release commit and the tag + env: + VERSION: ${{ inputs.version }} + TOKEN: ${{ steps.push-token.outputs.token }} + run: | + set -euo pipefail + AUTH="$(printf 'x-access-token:%s' "$TOKEN" | base64 -w0)" + echo "::add-mask::${AUTH}" + export GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0="http.${GITHUB_SERVER_URL}/.extraheader" \ + GIT_CONFIG_VALUE_0="AUTHORIZATION: basic ${AUTH}" + STATUS=0 + git ls-remote --exit-code origin "refs/tags/v${VERSION}" > /dev/null || STATUS=$? + case "$STATUS" in + 0) + echo "::error::the tag v${VERSION} exists already" + exit 1 + ;; + 2) ;; + *) + echo "::error::cannot check whether the tag v${VERSION} exists" + exit 1 + ;; + esac + git -c core.hooksPath=/dev/null push --atomic origin \ + "HEAD:refs/heads/${GITHUB_REF_NAME}" "refs/tags/v${VERSION}" + + notify: + needs: tag + if: ${{ failure() }} + runs-on: ubuntu-latest + permissions: {} + steps: + - name: Compose the notification + id: notification + env: + REPOSITORY: ${{ github.repository }} + VERSION: ${{ inputs.version }} + RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} + run: | + echo "message=❌ Tagging v${VERSION} of ${REPOSITORY} failed: ${RUN_URL}" >> "$GITHUB_OUTPUT" + + - name: Notify Matrix + continue-on-error: true + uses: $/.github/actions/matrix-notify + with: + message: ${{ steps.notification.outputs.message }} + homeserver: ${{ vars.MATRIX_HOMESERVER }} + room-id: ${{ vars.MATRIX_ROOM_ID }} + token: ${{ secrets.MATRIX_ACCESS_TOKEN }} diff --git a/README.md b/README.md index 3eb6729..dc5236e 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ Reusable GitHub workflows and actions for MyCoRe projects. | [`maven-version-branch.yml`](docs/maven-version-branch.md) | Create a version branch and set the next development version | | [`node-build.yml`](docs/node-build.md) | Install with Yarn and run package scripts | | [`node-release.yml`](docs/node-release.md) | Build, publish and create a GitHub release | +| [`node-release-tag.yml`](docs/node-release-tag.md) | Set the version and push a release tag, by hand | | [`forward-merge.yml`](docs/forward-merge.md) | Open a forward-merge PR from a version branch into the next one | | [`dispatch-downstream.yml`](docs/dispatch-downstream.md) | Start the snapshot deploy of dependent repositories | | [`require-permission.yml`](docs/require-permission.md) | Fail unless the triggering user has a minimum permission | @@ -71,15 +72,15 @@ read it: Pull requests from forks get no secrets, except in workflows started by `pull_request_target`, which must therefore never build their code. -| Secret | Used by | Content | -|--------------------------------------------------|---------------------------------------------------------------------------------------------|----------------------------------------------------------------------------| -| `MAVEN_USERNAME` / `MAVEN_PASSWORD` | `maven-deploy.yml`, `maven-deploy-pr.yml`, `maven-release.yml` | Sonatype Central token for the `org.mycore` namespace | -| `MAVEN_GPG_PRIVATE_KEY` / `MAVEN_GPG_PASSPHRASE` | `maven-release.yml` | Signing key | -| `DOCKERHUB_USERNAME` / `DOCKERHUB_TOKEN` | `maven-deploy.yml`, `maven-release.yml` | Docker Hub login, needed with `docker-images` | -| `RELEASE_APP_PRIVATE_KEY` | `maven-release.yml`, `maven-version-branch.yml` | Private key of the GitHub App that pushes, see [Push access](#push-access) | -| `PR_TOKEN` | `forward-merge.yml` | Token creating the forward-merge PR | -| `DISPATCH_TOKEN` | `dispatch-downstream.yml`, `snapshot-refresh.yml` | Token with `actions: write` on the target repositories | -| `MATRIX_ACCESS_TOKEN` | `maven-release.yml`, `maven-version-branch.yml`, `node-release.yml`, `snapshot-refresh.yml` | Access token of the Matrix account sending notifications | +| Secret | Used by | Content | +|--------------------------------------------------|---------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------| +| `MAVEN_USERNAME` / `MAVEN_PASSWORD` | `maven-deploy.yml`, `maven-deploy-pr.yml`, `maven-release.yml` | Sonatype Central token for the `org.mycore` namespace | +| `MAVEN_GPG_PRIVATE_KEY` / `MAVEN_GPG_PASSPHRASE` | `maven-release.yml` | Signing key | +| `DOCKERHUB_USERNAME` / `DOCKERHUB_TOKEN` | `maven-deploy.yml`, `maven-release.yml` | Docker Hub login, needed with `docker-images` | +| `RELEASE_APP_PRIVATE_KEY` | `maven-release.yml`, `maven-version-branch.yml`, `node-release-tag.yml` | Private key of the GitHub App that pushes, see [Push access](#push-access) | +| `PR_TOKEN` | `forward-merge.yml` | Token creating the forward-merge PR | +| `DISPATCH_TOKEN` | `dispatch-downstream.yml`, `snapshot-refresh.yml` | Token with `actions: write` on the target repositories | +| `MATRIX_ACCESS_TOKEN` | `maven-release.yml`, `maven-version-branch.yml`, `node-release.yml`, `node-release-tag.yml`, `snapshot-refresh.yml` | Access token of the Matrix account sending notifications | The secrets of releases are secrets of the [environment `release`](#the-environment-release) in each repository that releases: the Central token, the signing key, the key of the GitHub App and, with release images, the Docker Hub login. @@ -124,6 +125,7 @@ Releases and version branches push to protected branches with a GitHub App of th The workflows create a token right before each push, valid for one hour and only for the repository of the run. Without `RELEASE_APP_CLIENT_ID` they push with `GITHUB_TOKEN`, which fails on protected branches and starts no workflows. +`node-release-tag.yml` needs the tag it pushes to start the release, it fails without `RELEASE_APP_CLIENT_ID`. Do not make GitHub Actions itself a bypass actor of a ruleset, then every workflow on every branch could push past it. ### Variables @@ -131,10 +133,10 @@ Do not make GitHub Actions itself a bypass actor of a ruleset, then every workfl The workflows read these organization variables from the calling repository, like the ones of the [notifications](#notifications): -| Variable | Used by | Content | -|-------------------------|-------------------------------------------------|---------------------------------------------------------------------------------| -| `RELEASE_APP_CLIENT_ID` | `maven-release.yml`, `maven-version-branch.yml` | Client ID of the GitHub App that pushes | -| `DISPATCH_ACTOR` | `maven-release.yml`, `maven-version-branch.yml` | Login of the account of `DISPATCH_TOKEN`, refused as starter of these workflows | +| Variable | Used by | Content | +|-------------------------|-------------------------------------------------------------------------|---------------------------------------------------------------------------------| +| `RELEASE_APP_CLIENT_ID` | `maven-release.yml`, `maven-version-branch.yml`, `node-release-tag.yml` | Client ID of the GitHub App that pushes | +| `DISPATCH_ACTOR` | `maven-release.yml`, `maven-version-branch.yml`, `node-release-tag.yml` | Login of the account of `DISPATCH_TOKEN`, refused as starter of these workflows | ## Notifications @@ -145,6 +147,7 @@ Releases, version branches and failures of the snapshot refresh are reported to | `maven-release.yml` | Released, released with a failing Docker push or GitHub release, failed or cancelled, rolled back | | `maven-version-branch.yml` | Branch created or failed | | `node-release.yml` | Published, published without GitHub release, failed | +| `node-release-tag.yml` | Failed | | `snapshot-refresh.yml` | Failed, with the failing targets | The room is set by organization variables, which the workflows read from the calling repository: diff --git a/docs/node-release-tag.md b/docs/node-release-tag.md new file mode 100644 index 0000000..dd6f4ab --- /dev/null +++ b/docs/node-release-tag.md @@ -0,0 +1,92 @@ +# node-release-tag.yml + +Releases a Node.js package by hand: sets the version in the package metadata, commits it, and pushes the commit +and the tag `v` with the GitHub App. The tag starts the release workflow of the project, which publishes +the package with [`node-release.yml`](node-release.md). + +## Usage + +One caller handles both, the dispatch and the tag it pushes. Projects publishing with trusted publishing keep +the file name the trusted publisher on npmjs.com is configured with, see [OIDC](node-release.md#publishing-with-oidc). + +```yaml +name: Release + +on: + push: + tags: + - 'v*.*.*' + workflow_dispatch: + inputs: + version: + description: 'Version to release (e.g. 1.2.3)' + required: true + +permissions: {} + +jobs: + tag: + if: github.event_name == 'workflow_dispatch' + permissions: + contents: read + uses: MyCoRe-Org/github-workflows/.github/workflows/node-release-tag.yml@v1 + with: + version: ${{ inputs.version }} + secrets: + RELEASE_APP_PRIVATE_KEY: ${{ secrets.RELEASE_APP_PRIVATE_KEY }} + MATRIX_ACCESS_TOKEN: ${{ secrets.MATRIX_ACCESS_TOKEN }} + + publish: + if: github.event_name == 'push' + permissions: + contents: write + id-token: write + uses: MyCoRe-Org/github-workflows/.github/workflows/node-release.yml@v1 + with: + node-version: '24' + secrets: + MATRIX_ACCESS_TOKEN: ${{ secrets.MATRIX_ACCESS_TOKEN }} +``` + +To release, open the workflow in the Actions tab, choose the branch to release from and enter the version without +a leading `v`. Pushing a tag by hand still works as before. + +## Before the first release + +- The [GitHub App](../README.md#push-access), installed on the repository, and `RELEASE_APP_CLIENT_ID`. A tag pushed + with `GITHUB_TOKEN` starts no workflow, so the workflow fails right away without the App. On a protected branch, + the App needs to be a bypass actor of its ruleset. +- The [environment `release`](../README.md#the-environment-release) with `RELEASE_APP_PRIVATE_KEY`, its deployment + branches decide which branches may be released. + +## Inputs + +| Input | Required | Default | Description | +|----------------|----------|----------------|----------------------------------------------------------------------------| +| `version` | yes | | Version to release, e.g. `1.2.3` or `1.2.3-beta.1`, without a leading `v` | +| `version-file` | no | `package.json` | JSON file holding the version, e.g. `jsr.json`, the same as `node-release` | +| `environment` | no | `release` | Environment holding `RELEASE_APP_PRIVATE_KEY` | + +| Secret | Description | +|---------------------------|----------------------------------------------------------------------------| +| `RELEASE_APP_PRIVATE_KEY` | Private key of the GitHub App, see [Push access](../README.md#push-access) | +| `MATRIX_ACCESS_TOKEN` | Optional, see [notifications](../README.md#notifications) | + +## How it works + +The workflow runs a single job in the environment, it runs no code of the project: + +1. [`check-trigger`](../.github/actions/check-trigger/action.yml) refuses runs on tags and runs started by the account + of `DISPATCH_TOKEN`, a missing `RELEASE_APP_CLIENT_ID` stops the run as well. +2. The version is set in `version-file` with a short Node.js script instead of `npm version`, which would run the + version scripts of the project. Indentation, key order and the end of the file are kept. If the file would change + in more than the version line, e.g. as it is written on a single line, the run fails and the version has to be set + by hand. +3. The change is committed as `chore(release): v` by the account that started the run, and tagged with an + annotated tag `v`. +4. With a token of the GitHub App, created right before, the commit and the tag are pushed atomically, after + checking that the tag does not exist. If the branch moved in the meantime, the push fails and nothing is pushed. +5. The tag starts the release workflow of the project. Failures of this workflow are reported to Matrix, if + configured, the result of the publishing comes from `node-release.yml`. + +Lock files keep no version of the package itself with Yarn, a `package-lock.json` of npm would not be updated. diff --git a/workflow-templates/node-release.properties.json b/workflow-templates/node-release.properties.json index fac8658..3776253 100644 --- a/workflow-templates/node-release.properties.json +++ b/workflow-templates/node-release.properties.json @@ -1,6 +1,6 @@ { "name": "MyCoRe Node Release", - "description": "Publishes the package with npm trusted publishing and creates a GitHub release for version tags.", + "description": "Publishes the package with npm trusted publishing and creates a GitHub release for version tags, which it can also set and push by hand.", "categories": [ "JavaScript", "TypeScript" diff --git a/workflow-templates/node-release.yml b/workflow-templates/node-release.yml index 2135bd5..d6e6163 100644 --- a/workflow-templates/node-release.yml +++ b/workflow-templates/node-release.yml @@ -5,13 +5,32 @@ on: push: tags: - 'v*.*.*' + # sets the version and pushes the tag, needs the GitHub App and the environment release + workflow_dispatch: + inputs: + version: + description: 'Version to release (e.g. 1.2.3)' + required: true -permissions: - contents: write - id-token: write +permissions: {} jobs: + tag: + if: github.event_name == 'workflow_dispatch' + permissions: + contents: read + uses: MyCoRe-Org/github-workflows/.github/workflows/node-release-tag.yml@v1 + with: + version: ${{ inputs.version }} + secrets: + RELEASE_APP_PRIVATE_KEY: ${{ secrets.RELEASE_APP_PRIVATE_KEY }} + MATRIX_ACCESS_TOKEN: ${{ secrets.MATRIX_ACCESS_TOKEN }} + publish: + if: github.event_name == 'push' + permissions: + contents: write + id-token: write uses: MyCoRe-Org/github-workflows/.github/workflows/node-release.yml@v1 with: node-version: '24'