name: Promote a release to latest # A release is published under the `next` dist-tag (publish.yml); a bare install # resolves `latest`. This workflow moves `latest` for `prisma` and `@prisma/cli` # to a published release, by hand, once the Prisma ORM, Composer and CLI # versions that work together are all released and checked, and refuses while # the ORM and Composer versions the release pins are not `latest` yet. See # docs/oss/versioning.md. The same shape as prisma/orm's promote.yml. # # Three jobs, so that the npm token meets no code from the repository and no # repository write access: # - check: no token, read-only. Runs this workflow's own scripts on the # release's tag, checked out as data only, and outputs what to move. A dry # run ends here, from any branch. # - promote: main only, not a dry run. Holds the token from the npm-promote # environment, checks out nothing, and has no repository permissions. # - mark-latest: marks the GitHub Release as Latest, for a stable release. on: workflow_dispatch: inputs: version: description: "Release version to promote, such as 8.0.0-rc.37" required: true type: string dry-run: description: "Dry run: check the release and print the moves, without changing any tag" required: false default: true type: boolean allow-downgrade: description: "Allow moving latest back to an older release (a rollback)" required: false default: false type: boolean # A group keeps only one pending run, and a newer one replaces it. So any run # that is not a real promotion from main (a dry run, or a run from another # branch, which fails its input check) gets a group of its own, and cannot # replace a real promotion waiting behind a running one. concurrency: group: ${{ (inputs.dry-run || github.ref != 'refs/heads/main') && format('npm-promote-dry-run-{0}', github.run_id) || 'npm-promote' }} cancel-in-progress: false permissions: {} jobs: check: name: Check the release runs-on: ubuntu-latest permissions: contents: read outputs: plan: ${{ steps.plan.outputs.plan }} steps: - name: Check the inputs env: VERSION: ${{ inputs.version }} DRY_RUN: ${{ inputs.dry-run }} run: | if ! printf '%s' "$VERSION" | grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+(-rc\.[0-9]+)?$'; then echo "::error::\"$VERSION\" is not a release version, such as 8.0.0-rc.37 or 8.1.0." exit 1 fi if [ "$DRY_RUN" != "true" ] && [ "$GITHUB_REF" != "refs/heads/main" ]; then echo "::error::A real promotion runs only from main. Run from $GITHUB_REF as a dry run, or dispatch from main." exit 1 fi - name: Checkout this workflow's ref uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 with: fetch-depth: 0 persist-credentials: false # Data only: nothing in this folder is run. - name: Checkout the release's tag as data uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 with: ref: v${{ inputs.version }} path: release-data persist-credentials: false - name: Check the release's tag is on main env: VERSION: ${{ inputs.version }} run: | tagged="$(git rev-parse "v$VERSION^{commit}")" if [ "$(git -C release-data rev-parse HEAD)" != "$tagged" ]; then echo "::error::The two checkouts of v$VERSION differ; the tag moved during the run." exit 1 fi if ! git merge-base --is-ancestor "$tagged" origin/main; then echo "::error::v$VERSION points at $tagged, which is not on main. Only a release published from main can be promoted." exit 1 fi # Checked before npm changes, so marking a stable release Latest cannot fail # after `latest` has moved. A tag can exist with no release. - name: Check a stable release's GitHub Release exists if: ${{ !contains(inputs.version, '-') }} env: GH_TOKEN: ${{ github.token }} VERSION: ${{ inputs.version }} run: | if ! gh release view "v$VERSION" --repo "$GITHUB_REPOSITORY" --json isDraft --jq '.isDraft == false' | grep -qx true; then echo "::error::GitHub Release v$VERSION is missing or still a draft. Publish it first; nothing was changed." exit 1 fi - name: Set up Node.js uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0 with: node-version-file: .node-version - name: Plan the promotion id: plan env: VERSION: ${{ inputs.version }} ALLOW_DOWNGRADE: ${{ inputs.allow-downgrade }} run: | flags=() if [ "$ALLOW_DOWNGRADE" = "true" ]; then flags+=(--allow-downgrade); fi node scripts/promote-release.ts "$VERSION" --release-root release-data --plan-out "$RUNNER_TEMP/plan.json" "${flags[@]}" echo "plan=$(jq -c . "$RUNNER_TEMP/plan.json")" >> "$GITHUB_OUTPUT" promote: name: Move latest to the release needs: check if: ${{ github.ref == 'refs/heads/main' && !inputs.dry-run }} runs-on: ubuntu-latest environment: npm-promote permissions: {} outputs: version: ${{ steps.move.outputs.version }} steps: - name: Check the npm token env: NPM_TOKEN: ${{ secrets.NPM_PROMOTE_TOKEN }} run: | if [ -z "$NPM_TOKEN" ]; then echo "::error::The npm-promote environment has no NPM_PROMOTE_TOKEN secret. Create a short-lived npm token, add it to that environment as NPM_PROMOTE_TOKEN, run again, then delete the secret and revoke the token. See docs/oss/versioning.md." exit 1 fi echo "::add-mask::$NPM_TOKEN" # Runs no code from the repository: the plan comes from the check job, and # each name and version in it is checked before use. The token reaches npm # only through this step's environment, by a config file outside any # checkout that refers to NODE_AUTH_TOKEN rather than holding it. - name: Move latest id: move env: NODE_AUTH_TOKEN: ${{ secrets.NPM_PROMOTE_TOKEN }} PLAN: ${{ needs.check.outputs.plan }} run: | set -euo pipefail version_re='^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$' name_re='^(prisma|@prisma/cli)$' release="$(jq -r '.version' <<<"$PLAN")" entries="$(jq -r '.packages[] | "\(.name) \(.version)"' <<<"$PLAN")" [[ $release =~ $version_re ]] || { echo "::error::The plan's version \"$release\" is not a version."; exit 1; } [ -n "$entries" ] || { echo "::error::The plan lists no package."; exit 1; } while read -r name version <&3; do [[ $name =~ $name_re ]] || { echo "::error::The plan names \"$name\", which is not one of ours."; exit 1; } [[ $version =~ $version_re ]] || { echo "::error::The plan gives $name \"$version\", which is not a version."; exit 1; } done 3<<<"$entries" npmrc="$RUNNER_TEMP/promote.npmrc" printf '%s\n' 'registry=https://registry.npmjs.org/' "//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}" > "$npmrc" export NPM_CONFIG_USERCONFIG="$npmrc" while read -r name version <&3; do if [ "$(npm view "$name@$version" version 2>/dev/null || true)" != "$version" ]; then echo "::error::$name@$version is not on the registry. Nothing moved." exit 1 fi done 3<<<"$entries" moved=() while read -r name version <&3; do if [ "$(npm view "$name" dist-tags.latest)" = "$version" ]; then echo "$name: latest is already $version" continue fi if npm dist-tag add "$name@$version" latest || npm dist-tag add "$name@$version" latest; then moved+=("$name") echo "$name: latest moved to $version" else echo "::error::Moving latest failed for $name@$version. Moved in this run: ${moved[*]:-none}. Run the workflow again to move the rest; packages already moved are left alone." exit 1 fi done 3<<<"$entries" echo "version=$release" >> "$GITHUB_OUTPUT" mark-latest: name: Mark the GitHub Release as Latest needs: promote runs-on: ubuntu-latest permissions: contents: write steps: # publish.yml creates the GitHub Release without the Latest mark. GitHub # does not let a pre-release be Latest, so a release candidate stays as it is. - name: Mark the GitHub Release as Latest env: GH_TOKEN: ${{ github.token }} VERSION: ${{ needs.promote.outputs.version }} run: | if [ "$(gh release view "v$VERSION" --repo "$GITHUB_REPOSITORY" --json isPrerelease --jq .isPrerelease)" = "true" ]; then echo "v$VERSION is a pre-release, which GitHub does not let be Latest; leaving it as it is." else gh release edit "v$VERSION" --repo "$GITHUB_REPOSITORY" --latest fi