diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a840e9fd9..ac5c0f41b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,29 +1,93 @@ +# This file must be named release.yml: npm's trusted publishing configuration for our packages +# names this workflow file. name: Release Workflow +# Publishes only. A release reaches main as a commit that has already consumed its changesets and +# bumped versions; this workflow publishes those versions and opens no pull request. on: push: branches: - main -concurrency: ${{ github.workflow }}-${{ github.ref }} +permissions: + contents: read + +concurrency: + group: release + # Queue runs instead of cancelling: a cancel that lands between the npm publish and the tag + # creation leaves state that a rerun cannot rebuild. Without `queue: max`, a newer push would + # still replace a pending run. + cancel-in-progress: false + queue: max env: npm_config_registry: "https://registry.npmjs.org" jobs: - publish-packages: + select-mode: runs-on: ubuntu-latest + timeout-minutes: 15 outputs: - hasChangesets: ${{ steps.changesets.outputs.has-changesets }} + mode: ${{ steps.mode.outputs.mode }} + + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + # Changesets reads the existing release tags to decide what still needs one + fetch-depth: 0 + persist-credentials: false + + - name: Setup pnpm + uses: pnpm/action-setup@73e46992e05b19abd31f2f3c20e0e7370d762115 # v6.1.1 + with: + run_install: false + + - name: Setup Node.js + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: 24 + cache: "pnpm" + cache-dependency-path: "pnpm-lock.yaml" + + - name: Install the packages + run: pnpm install --frozen-lockfile + + - name: Check for versions to publish + id: mode + # "publish" when no changeset is pending and a package version isn't on npm yet, + # "version" while changesets are pending, "none" otherwise + uses: changesets/action/select-mode@ae32849d5ba541f9ae29e40e22a623bc13562f51 # v2.1.2 + + - name: Fail on a version bump with changesets pending + if: steps.mode.outputs.mode == 'version' + # Publishing that commit would ship changes its changelog doesn't list, and skipping it + # quietly would leave main ahead of npm with nothing to show for it + run: | + pnpm changeset publish-plan --output "$RUNNER_TEMP/publish-plan.json" + node -e ' + const { plan } = JSON.parse(require("node:fs").readFileSync(process.argv[1], "utf8")); + const unpublished = plan.flat().filter((release) => release.kind === "publish"); + if (unpublished.length > 0) { + const names = unpublished.map((release) => release.name + "@" + release.version).join(", "); + console.log("::error::Changesets are still pending, so nothing was published. Not on npm yet: " + names); + process.exit(1); + } + ' "$RUNNER_TEMP/publish-plan.json" + + publish-npm: + needs: select-mode + if: needs.select-mode.outputs.mode == 'publish' + runs-on: ubuntu-latest + timeout-minutes: 30 permissions: + # The publish action pushes the tags and creates the GitHub releases through the API contents: write - pull-requests: write + # npm trusted publishing id-token: write steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: - submodules: true fetch-depth: 0 persist-credentials: false @@ -48,59 +112,73 @@ jobs: env: NODE_ENV: production - - name: Grab date as YYYY-MM-DD - id: date - run: echo "date=$(date +'%Y-%m-%d')" >> $GITHUB_OUTPUT + - name: Publish to npm + uses: changesets/action/publish@ae32849d5ba541f9ae29e40e22a623bc13562f51 # v2.1.2 + with: + script: pnpm changeset publish + env: + PNPM_CONFIG_PROVENANCE: true + + # The extension is private to npm, so select-mode doesn't see it. Its version is compared with + # the Marketplace instead. + publish-extension: + needs: select-mode + if: needs.select-mode.outputs.mode != 'version' + runs-on: ubuntu-latest + timeout-minutes: 30 - - name: Query VS Code Marketplace version - id: marketplace-version - run: echo "version=$(pnpm --dir packages/vscode-extension exec vsce show --json Shopify.theme-check-vscode | jq -r .versions[0].version)" >> $GITHUB_OUTPUT + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + submodules: true + persist-credentials: false - - name: Query VS Code Package version (Before running changeset version) - id: package-version - run: echo "version=$(jq -r .version packages/vscode-extension/package.json)" >> $GITHUB_OUTPUT + - name: Setup pnpm + uses: pnpm/action-setup@73e46992e05b19abd31f2f3c20e0e7370d762115 # v6.1.1 + with: + run_install: false - - name: Test OIDC Token - run: | - TOKEN=$(curl -s -H "Authorization: bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \ - "$ACTIONS_ID_TOKEN_REQUEST_URL&audience=npm" | jq -r '.value') - [ -n "$TOKEN" ] && echo "✅ OIDC working" || exit 1 - - - name: Create Release Pull Request or Publish to npm - id: changesets - # v2 is the version that runs Changesets 3 - uses: changesets/action@ae32849d5ba541f9ae29e40e22a623bc13562f51 # v2.1.2 + - name: Setup Node.js + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: - # Put date in the title and commit message - pr-title: "Theme Tools Release — ${{ steps.date.outputs.date }}" - commit-message: "Theme Tools Release — ${{ steps.date.outputs.date }}" - # When there are no changesets, this gets called - publish-script: pnpm changeset publish - # When there are changesets, this gets called and then a PR is opened/updated - version-script: pnpm release - env: - PNPM_CONFIG_PROVENANCE: true + node-version: 24 + cache: "pnpm" + cache-dependency-path: "pnpm-lock.yaml" + + - name: Install the packages + run: pnpm install --frozen-lockfile + + - name: Compare the extension's version with the Marketplace + id: versions + run: | + echo "marketplace=$(pnpm --dir packages/vscode-extension exec vsce show --json Shopify.theme-check-vscode | jq -r '.versions[0].version')" >> "$GITHUB_OUTPUT" + echo "package=$(jq -r .version packages/vscode-extension/package.json)" >> "$GITHUB_OUTPUT" - - name: Clear GitHub credentials before marketplace publish - run: rm -f "$HOME/.netrc" + - name: Build the code + if: steps.versions.outputs.marketplace != steps.versions.outputs.package + run: pnpm build + env: + NODE_ENV: production - name: VS Code Marketplace publish - if: steps.changesets.outputs.has-changesets == 'false' && steps.marketplace-version.outputs.version != steps.package-version.outputs.version + if: steps.versions.outputs.marketplace != steps.versions.outputs.package run: pnpm publish:vsce env: VSCE_PAT: ${{ secrets.VSCE_PAT }} - name: Open VSX Registry publish - if: steps.changesets.outputs.has-changesets == 'false' && steps.marketplace-version.outputs.version != steps.package-version.outputs.version + if: steps.versions.outputs.marketplace != steps.versions.outputs.package run: pnpm publish:ovsx env: OVSX_PAT: ${{ secrets.OPENVSX_TOKEN }} - # Its own job, so no npm package runs where a crates.io token can be had. + # Its own job, so no npm package runs where a crates.io token can be had, and a crates.io + # failure doesn't stop the npm or extension publishes. publish-crates: - needs: publish-packages - if: needs.publish-packages.outputs.hasChangesets == 'false' + needs: select-mode + if: needs.select-mode.outputs.mode != 'version' runs-on: ubuntu-latest + timeout-minutes: 30 environment: crates-io permissions: contents: read @@ -132,7 +210,7 @@ jobs: on_crates_io "$crate/$version" || list="$list $crate" done fi - echo "list=$list" >> $GITHUB_OUTPUT + echo "list=$list" >> "$GITHUB_OUTPUT" - name: Exchange the Actions identity token for a crates.io token id: crates-io-auth