diff --git a/.github/workflows/release-qa-package.yml b/.github/workflows/release-qa-package.yml new file mode 100644 index 00000000000..fc74715d9f8 --- /dev/null +++ b/.github/workflows/release-qa-package.yml @@ -0,0 +1,118 @@ +name: Release QA package + +# Builds the @shopify/cli tarball that a release of a commit would publish, so it can be tested +# before that commit is merged and released. Nothing is published. +on: + workflow_dispatch: + inputs: + sha: + description: 'Full commit SHA to build, normally the head of a "Version Packages - stable/X.Y" pull request' + type: string + required: true + +permissions: + contents: read + +concurrency: + group: release-qa-package-${{ inputs.sha }} + cancel-in-progress: false + +env: + PNPM_VERSION: '10.11.1' + +jobs: + package: + name: Build the release tarball + runs-on: ubuntu-latest + steps: + - name: Validate the SHA + env: + SHA: ${{ inputs.sha }} + run: | + set -euo pipefail + # A full SHA names one commit; a branch name or short SHA could resolve to a different one. + if [[ ! "$SHA" =~ ^[0-9a-f]{40}$ ]]; then + echo "::error::sha must be a full commit SHA: 40 lowercase hexadecimal characters." + exit 1 + fi + # Only the head of one of this repository's branches, such as a Version Packages pull + # request's branch, so the job never builds a fork's code with this repository's cache. + if ! git ls-remote --heads "https://github.com/$GITHUB_REPOSITORY" | cut -f 1 | grep -qx "$SHA"; then + echo "::error::$SHA isn't the head of a branch in $GITHUB_REPOSITORY." + exit 1 + fi + + - uses: actions/checkout@v6 + with: + ref: ${{ inputs.sha }} + persist-credentials: false + + - name: Verify the checked-out commit + env: + SHA: ${{ inputs.sha }} + run: | + set -euo pipefail + HEAD_SHA=$(git rev-parse HEAD) + if [ "$HEAD_SHA" != "$SHA" ]; then + echo "::error::Checked out $HEAD_SHA instead of $SHA." + exit 1 + fi + + - name: Setup deps + uses: ./.github/actions/setup-cli-deps + with: + node-version: 24.22.1 + + # The same build the release runs, before it publishes. + - name: Bundle + run: pnpm bundle-for-release + env: + SHOPIFY_CLI_BUILD_REPO: ${{ github.repository }} + + # The release's prepack also generates the manifest and copies the README. Its error-reporting + # source map upload is left out, and the tarball excludes source maps either way. + - name: Pack @shopify/cli + id: pack + working-directory: packages/cli + run: | + set -euo pipefail + pnpm oclif manifest + cp ../../README.md README.md + TARBALL=$(npm pack --ignore-scripts | tail -n 1) + echo "tarball=$TARBALL" >> "$GITHUB_OUTPUT" + + - name: Upload the tarball + uses: actions/upload-artifact@v4 + with: + name: shopify-cli-${{ inputs.sha }} + path: packages/cli/${{ steps.pack.outputs.tarball }} + if-no-files-found: error + # The tarball is already compressed. + compression-level: 0 + + - name: Summarize + working-directory: packages/cli + env: + SHA: ${{ inputs.sha }} + TARBALL: ${{ steps.pack.outputs.tarball }} + ARTIFACT: shopify-cli-${{ inputs.sha }} + run: | + set -euo pipefail + CHECKSUM=$(sha256sum "$TARBALL" | cut -d ' ' -f 1) + VERSION=$(tar -xzOf "$TARBALL" package/package.json | node -p 'JSON.parse(require("node:fs").readFileSync(0, "utf8")).version') + { + echo "### @shopify/cli $VERSION" + echo + echo "| | |" + echo "| --- | --- |" + echo "| Commit | \`$SHA\` |" + echo "| Tarball | \`$TARBALL\` |" + echo "| SHA-256 | \`$CHECKSUM\` |" + echo "| Version | \`$VERSION\` |" + echo + echo "Download it with:" + echo + echo '```sh' + echo "gh run download $GITHUB_RUN_ID -R $GITHUB_REPOSITORY -n $ARTIFACT" + echo '```' + } >> "$GITHUB_STEP_SUMMARY" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 41b3d37e063..2c0916f4ffe 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,8 +1,9 @@ name: Release on: - # Publishes the versions a push brings: on main, World's Version Packages commit, which the public - # mirror publishes here; on stable/*, a merged "Version Packages - stable/X.Y" pull request. + # Releases ship from stable/*: a merged "Version Packages - stable/X.Y" pull request publishes the + # versions it changed. main doesn't publish releases; its check fails if a version on it isn't on + # npm. push: branches: - main @@ -21,6 +22,9 @@ on: options: - nightly - experimental + # On stable/*: publishes the versions that the branch's head commit itself changed, for a + # release whose push started no run, or whose run can no longer be re-run. + - release concurrency: group: changeset-${{ github.head_ref || github.run_id }} @@ -31,8 +35,8 @@ env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} jobs: - # Opens or updates a stable line's Version Packages pull request. main's versions come from World - # through the public mirror, so main gets no Version Packages pull request here. + # Opens or updates a stable line's Version Packages pull request. main gets the released versions + # back after each release, so it has no Version Packages pull request. version-packages: name: Version Packages if: github.event_name == 'push' && startsWith(github.ref_name, 'stable/') @@ -60,58 +64,154 @@ jobs: createGithubReleases: false env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + # A line cut from a main that hasn't caught up with the last release would compute a version + # npm already has, and its release would then publish nothing. + - name: Refuse a version npm already has + if: ${{ !cancelled() }} + run: | + set -euo pipefail + BRANCH="changeset-release/$GITHUB_REF_NAME" + if ! git fetch --no-tags --depth=1 origin "$BRANCH"; then + echo "No $BRANCH branch, so there's no new version to check" + exit 0 + fi + CURRENT=$(node -p "require('./packages/cli/package.json').version") + VERSION=$(git show FETCH_HEAD:packages/cli/package.json | node -e 'let s = ""; process.stdin.on("data", (d) => (s += d)).on("end", () => console.log(JSON.parse(s).version))') + if [ "$VERSION" = "$CURRENT" ]; then + echo "$BRANCH has this line's current version, $CURRENT" + exit 0 + fi + if [ "$(npm view "@shopify/cli@$VERSION" version --registry=https://registry.npmjs.org/ 2>/dev/null || true)" = "$VERSION" ]; then + echo "::error::$BRANCH proposes @shopify/cli $VERSION, which npm already has. The line was cut before main got that release back: bring the release back to main, then into this line, so its next version is new." + exit 1 + fi + echo "$BRANCH proposes $VERSION, which npm doesn't have yet" - # Decides, without an npm credential, whether a push has anything to publish, so a push that - # doesn't change a version never waits for the npm-release approval. Every public package counts, - # so a partly failed publish is retried. + # Decides, without an npm credential, whether a push has anything to publish. Only versions this + # push changed count, and the publish job publishes only those. A version that an earlier push + # brought, which npm still lacks, is finished by re-running that push's run, which builds the same + # commit, or by the `release` option of a manual run while that commit is still the branch's head. + # A later commit never publishes it from a newer tree that wasn't released under that version. check: name: Check for unpublished versions needs: version-packages # Like changesets/action before it, a stable line doesn't publish while changesets are pending. - if: ${{ !cancelled() && github.event_name == 'push' && needs.version-packages.result != 'failure' && needs.version-packages.outputs.has-changesets != 'true' }} + if: ${{ !cancelled() && (github.event_name == 'push' || github.event.inputs.tag == 'release') && needs.version-packages.result != 'failure' && needs.version-packages.outputs.has-changesets != 'true' }} runs-on: ubuntu-latest permissions: contents: read outputs: unpublished: ${{ steps.npm.outputs.unpublished }} version: ${{ steps.npm.outputs.version }} + packages: ${{ steps.npm.outputs.packages }} steps: - uses: actions/checkout@v6 - - name: Compare package versions with npm + - name: Compare the pushed versions with npm id: npm + env: + BEFORE: ${{ github.event.before }} + MANUAL: ${{ github.event_name == 'workflow_dispatch' }} run: | set -euo pipefail + if [ "$MANUAL" = true ] && [ "$GITHUB_REF_NAME" = main ]; then + echo "::error::main doesn't publish releases. Releases ship from stable/*." + exit 1 + fi + if [ "$MANUAL" = true ]; then + # A manual release counts only the versions this commit changed against its first parent. + git fetch --no-tags --depth=2 origin "$GITHUB_SHA" + BEFORE=$(git rev-parse "$GITHUB_SHA^1") + elif [[ "$BEFORE" =~ ^0*$ ]]; then + # A push that creates the branch has no earlier commit, so it changes no version. + BEFORE=none + else + git fetch --no-tags --depth=1 origin "$BEFORE" + fi VERSION=$(node -p "require('./packages/cli/package.json').version") - # `shopify` is published from a copy of packages/cli (bin/create-cli-duplicate-package.js). - PACKAGES="shopify@$VERSION" - for MANIFEST in packages/*/package.json; do - PACKAGES="$PACKAGES $(node -p "const p = require('./$MANIFEST'); p.private ? '' : p.name + '@' + p.version")" - done + # One "pushed|carried name@version" line per public package. A package counts as pushed + # when this push changed its version or made it public. `shopify` is published from a + # copy of packages/cli (bin/create-cli-duplicate-package.js). + LINES=$(node -e ' + const {execFileSync} = require("node:child_process") + const fs = require("node:fs") + const before = process.argv[1] + const manifestAt = (path) => { + try { + return JSON.parse(execFileSync("git", ["show", `${before}:${path}`], {encoding: "utf8", stdio: ["ignore", "pipe", "ignore"]})) + } catch { + return undefined + } + } + for (const directory of fs.readdirSync("packages")) { + const path = `packages/${directory}/package.json` + if (!fs.existsSync(path)) continue + const manifest = JSON.parse(fs.readFileSync(path, "utf8")) + if (manifest.private) continue + let pushed = false + if (before !== "none") { + const previous = manifestAt(path) + pushed = !previous || previous.private === true || previous.version !== manifest.version + } + const names = directory === "cli" ? [manifest.name, "shopify"] : [manifest.name] + for (const name of names) console.log(`${pushed ? "pushed" : "carried"} ${name}@${manifest.version}`) + } + ' "$BEFORE") + # npm view, retried, so a brief registry error doesn't read as a missing version. + on_npm() { + for attempt in 1 2 3; do + if [ "$(npm view "$1" version 2>/dev/null || true)" = "${1##*@}" ]; then return 0; fi + [ "$attempt" = 3 ] || sleep 5 + done + return 1 + } UNPUBLISHED=false - for PACKAGE in $PACKAGES; do - # An npm error counts as unpublished: the publish job then asks for approval, and - # `changeset publish` skips every version npm already has. - if [ "$(npm view "$PACKAGE" version 2>/dev/null || true)" = "${PACKAGE##*@}" ]; then + MISSING="" + PUSHED="" + while read -r STATE PACKAGE; do + [ -n "$PACKAGE" ] || continue + if [ "$STATE" = pushed ]; then + PUSHED="$PUSHED ${PACKAGE%@*}" + fi + # An npm error counts as unpublished. `changeset publish` skips every version npm + # already has, so publishing a pushed version that npm does have is harmless. + if on_npm "$PACKAGE"; then echo "$PACKAGE is on npm" - else + continue + fi + MISSING="$MISSING $PACKAGE" + if [ "$GITHUB_REF_NAME" = main ]; then echo "$PACKAGE is not on npm" + elif [ "$STATE" = pushed ]; then + echo "$PACKAGE is not on npm, and this run publishes it" UNPUBLISHED=true + else + echo "::warning::$PACKAGE is not on npm, but an earlier push brought that version, so this run doesn't publish it. If that push's Release run failed, re-run its failed jobs; if it can't be re-run and that commit is still the branch's head, start this workflow by hand with the release option." fi - done - echo "Version $VERSION, unpublished $UNPUBLISHED" + done <<< "$LINES" + echo "Version $VERSION, unpublished $UNPUBLISHED, packages:$PUSHED" + if [ "$GITHUB_REF_NAME" = main ] && [ -n "$MISSING" ]; then + # main only gets versions back after a stable line published them. + echo "::error::main has versions that npm doesn't:$MISSING. main doesn't publish them; releases ship from stable/*." + exit 1 + fi + if [ "$MANUAL" = true ] && [ -z "$PUSHED" ]; then + echo "::error::$GITHUB_SHA didn't change any public package's version against its first parent, so a manual release has nothing it may publish. Re-run the failed jobs of the Release run for the commit that changed the version, or publish a new version." + exit 1 + fi { echo "version=$VERSION" echo "unpublished=$UNPUBLISHED" + echo "packages=${PUSHED# }" } >> "$GITHUB_OUTPUT" # The only job that publishes a release. The npm-release environment limits it to main and - # stable/* and asks Dev Experience to approve each run. + # stable/*. publish: name: Publish to npm needs: check # check's own skipped dependency (version-packages, on main) would skip this job under the # implicit success(), so the condition names check's result instead. - if: ${{ !cancelled() && needs.check.result == 'success' && needs.check.outputs.unpublished == 'true' }} + if: ${{ !cancelled() && needs.check.result == 'success' && needs.check.outputs.unpublished == 'true' && startsWith(github.ref_name, 'stable/') }} runs-on: ubuntu-latest environment: npm-release # Approved runs on one branch publish one at a time, in order; none is cancelled. @@ -122,36 +222,80 @@ jobs: permissions: contents: write id-token: write + pull-requests: read steps: - uses: actions/checkout@v6 + # The release is the merge of this line's Version Packages pull request, whose head was tested. + # A merge commit whose tree differs from that head means the line moved after the pull request + # was made, so the release would carry changes nobody tested with it. + - name: Check the release is the tested pull request head + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + set -euo pipefail + git fetch --no-tags --depth=2 origin "$GITHUB_SHA" + if [ "$(git rev-list --parents -n 1 "$GITHUB_SHA" | wc -w)" -ne 3 ]; then + echo "::error::$GITHUB_SHA isn't a merge commit. A release publishes only from merging its Version Packages pull request with a merge commit." + exit 1 + fi + HEAD_SHA=$(git rev-parse "$GITHUB_SHA^2") + PULL=$(gh api "repos/$GITHUB_REPOSITORY/commits/$GITHUB_SHA/pulls" --jq "[.[] | select(.merged_at != null and .merge_commit_sha == \"$GITHUB_SHA\" and .base.ref == \"$GITHUB_REF_NAME\" and .head.sha == \"$HEAD_SHA\" and .head.ref == \"changeset-release/$GITHUB_REF_NAME\")] | first | .number // empty") + if [ -z "$PULL" ]; then + echo "::error::$GITHUB_SHA isn't the merge of a Version Packages pull request from changeset-release/$GITHUB_REF_NAME into $GITHUB_REF_NAME, so nothing publishes from it." + exit 1 + fi + if [ "$(git rev-parse "$GITHUB_SHA^{tree}")" != "$(git rev-parse "$HEAD_SHA^{tree}")" ]; then + echo "::error::The merge's files differ from the pull request head $HEAD_SHA, so the line moved after the pull request was made. Let the Version Packages pull request update, test its new head and merge that." + exit 1 + fi + echo "Publishing the files of $HEAD_SHA, the head of #$PULL" - name: Setup deps uses: ./.github/actions/setup-cli-deps with: node-version: 24.22.1 - # After the approval, which can take hours, so a release that published meanwhile counts. + # Read when the job runs, so a release that published meanwhile counts. - name: Choose the dist-tag id: dist-tag env: VERSION: ${{ needs.check.outputs.version }} run: | set -euo pipefail - # A stable line's patch that is older than npm's `latest` goes under stable-X.Y, so it - # doesn't move `latest` backwards. main always publishes under `latest`. + # A release older than npm's `latest`, such as an older line's patch, goes under stable-X.Y, + # so it doesn't move `latest` backwards. DIST_TAG=latest - if [ "$GITHUB_REF_NAME" != main ]; then - LATEST=$(npm view @shopify/cli dist-tags.latest) - if ! node -e ' - const [version, latest] = process.argv.slice(1).map((v) => v.split(".").map(Number)) - const difference = version.map((part, i) => part - latest[i]).find((d) => d !== 0) ?? 0 - process.exit(difference >= 0 ? 0 : 1) - ' "$VERSION" "$LATEST"; then - DIST_TAG="stable-${VERSION%.*}" - fi + LATEST=$(npm view @shopify/cli dist-tags.latest) + if ! node -e ' + const [version, latest] = process.argv.slice(1).map((v) => v.split(".").map(Number)) + const difference = version.map((part, i) => part - latest[i]).find((d) => d !== 0) ?? 0 + process.exit(difference >= 0 ? 0 : 1) + ' "$VERSION" "$LATEST"; then + DIST_TAG="stable-${VERSION%.*}" fi echo "Publishing $VERSION under $DIST_TAG" echo "dist-tag=$DIST_TAG" >> "$GITHUB_OUTPUT" + # Leaves every other package out of this publish, so a version that an earlier push brought + # is never published from this commit's tree. + - name: Publish only the checked packages + env: + PACKAGES: ${{ needs.check.outputs.packages }} + run: | + set -euo pipefail + node -e ' + const fs = require("node:fs") + const keep = new Set(process.argv[1].split(" ").filter(Boolean)) + for (const directory of fs.readdirSync("packages")) { + const path = `packages/${directory}/package.json` + if (!fs.existsSync(path)) continue + const manifest = JSON.parse(fs.readFileSync(path, "utf8")) + if (manifest.private || keep.has(manifest.name)) continue + manifest.private = true + fs.writeFileSync(path, `${JSON.stringify(manifest, null, 2)}\n`) + console.log(`Not publishing ${manifest.name}@${manifest.version} from this commit`) + } + ' "$PACKAGES" + - name: Publish packages run: pnpm release "$DIST_TAG" env: @@ -174,43 +318,6 @@ jobs: git push origin "$TAG" echo "Created tag $TAG" - - name: Create stable branch - id: stable-branch - if: github.ref_name == 'main' && endsWith(needs.check.outputs.version, '.0') - # Only Dev Experience members can create stable/* branches, so this push can be refused. - # The release goes on, and the next step asks Dev Experience to create the branch. - continue-on-error: true - env: - VERSION: ${{ needs.check.outputs.version }} - run: | - set -euo pipefail - MINOR=${VERSION%.0} - BRANCH="stable/$MINOR" - if git ls-remote --exit-code --heads origin "$BRANCH" >/dev/null 2>&1; then - echo "Branch $BRANCH already exists, skipping" - exit 0 - fi - git push origin "HEAD:refs/heads/$BRANCH" - echo "Created branch $BRANCH" - - - name: Ask Dev Experience to create the stable branch - if: steps.stable-branch.outcome == 'failure' - env: - VERSION: ${{ needs.check.outputs.version }} - run: | - MINOR=${VERSION%.0} - MESSAGE="Create stable/$MINOR from tag $VERSION; only Dev Experience can." - echo "::warning title=stable/$MINOR was not created::$MESSAGE" - { - echo "### stable/$MINOR was not created" - echo - echo "$MESSAGE" - echo - echo '```sh' - echo "git push origin refs/tags/$VERSION:refs/heads/stable/$MINOR" - echo '```' - } >> "$GITHUB_STEP_SUMMARY" - - name: Create GitHub release if: ${{ !endsWith(needs.check.outputs.version, '.0.0') }} env: @@ -230,8 +337,8 @@ jobs: echo "Release $TAG already exists; ensured make_latest=legacy" exit 0 fi - # The public repository's pull requests are mirror publishes from World, so the notes come - # from the version's changelog entries rather than from GitHub's generated notes. + # The notes come from the version's changelog entries. GitHub's generated notes would list + # only the Version Packages pull request. node .github/scripts/release-notes.js "$TAG" > "$RUNNER_TEMP/release-notes.md" gh api \ --method POST \ @@ -269,11 +376,11 @@ jobs: fi # Nightly on schedule, nightly or experimental by hand, from main only. A nightly runs in the - # npm-nightly environment; an experimental release, which is rare, waits for the npm-release approval. + # npm-nightly environment; an experimental release, which is rare, runs in npm-release. manual-cron-release: name: Manual & Cron Release needs: nightly-changes - if: ${{ !cancelled() && (github.event_name == 'schedule' || github.event_name == 'workflow_dispatch') && github.ref_name == 'main' && needs.nightly-changes.outputs.changed != 'false' }} + if: ${{ !cancelled() && (github.event_name == 'schedule' || (github.event_name == 'workflow_dispatch' && github.event.inputs.tag != 'release')) && github.ref_name == 'main' && needs.nightly-changes.outputs.changed != 'false' }} runs-on: ubuntu-latest environment: ${{ github.event.inputs.tag == 'experimental' && 'npm-release' || 'npm-nightly' }} permissions: