From 2a687324fad16c94319a0e8f4921c90c047c22b8 Mon Sep 17 00:00:00 2001 From: Maarten Zuidhoorn Date: Thu, 19 Nov 2026 13:00:29 +0100 Subject: [PATCH 1/3] Allow publishing a subset of packages in preview builds Add a `packages` input that takes a comma-separated list of package names, and an `only-changed-packages` input that selects the packages changed compared to the base branch. Names without a scope get the source scope, and unknown names fail the build early. All packages are still built, and the selection only changes which ones are published. --- .github/workflows/prepare-preview.yml | 99 ++++++++++++++++++++++ .github/workflows/publish-preview-oidc.yml | 27 +++++- CHANGELOG.md | 7 ++ 3 files changed, 131 insertions(+), 2 deletions(-) diff --git a/.github/workflows/prepare-preview.yml b/.github/workflows/prepare-preview.yml index 9303a7be..dc71787d 100644 --- a/.github/workflows/prepare-preview.yml +++ b/.github/workflows/prepare-preview.yml @@ -28,6 +28,16 @@ on: required: false type: string default: '' + packages: + description: 'Comma-separated list of packages to publish (monorepo only). Names without a scope get the source scope. Mutually exclusive with "only-changed-packages". Empty = all packages.' + type: string + required: false + default: '' + only-changed-packages: + description: 'Only publish packages changed compared to the base branch of the pull request, or the default branch (monorepo only). Mutually exclusive with "packages".' + type: boolean + required: false + default: false rename-after-install-and-build: description: 'Whether packages are renamed to the preview build scope after (true) or before (false) the install and build steps.' type: boolean @@ -40,6 +50,9 @@ on: is-fork: description: 'Whether the pull request is from a fork. The build is skipped when it is.' value: ${{ jobs.is-fork-pull-request.outputs.IS_FORK }} + publish-locations: + description: 'JSON array of the workspace locations to publish. Empty = all workspaces.' + value: ${{ jobs.build-preview.outputs.publish-locations }} secrets: BUILD_ENV: description: 'JSON object of environment variables to pass to the build step.' @@ -59,12 +72,25 @@ jobs: env: PR_NUMBER: ${{ inputs.pull-request || github.event.issue.number }} BRANCH: ${{ inputs.branch }} + PACKAGES: ${{ inputs.packages }} + ONLY_CHANGED_PACKAGES: ${{ inputs.only-changed-packages }} + IS_MONOREPO: ${{ inputs.is-monorepo }} run: | if [[ -n "$PR_NUMBER" && -n "$BRANCH" ]]; then echo "::error::Provide either a pull request number or a branch, not both." exit 1 fi + if [[ -n "$PACKAGES" && "$ONLY_CHANGED_PACKAGES" == "true" ]]; then + echo "::error::Provide either a list of packages or only publish changed packages, not both." + exit 1 + fi + + if [[ "$IS_MONOREPO" != "true" && ( -n "$PACKAGES" || "$ONLY_CHANGED_PACKAGES" == "true" ) ]]; then + echo "::error::Selecting packages is only supported in monorepos." + exit 1 + fi + if [[ -z "$PR_NUMBER" && -z "$BRANCH" ]]; then echo "::error::Provide a pull request number or a branch." exit 1 @@ -117,6 +143,8 @@ jobs: needs: is-fork-pull-request if: ${{ needs.is-fork-pull-request.outputs.IS_FORK == 'false' }} runs-on: ubuntu-latest + outputs: + publish-locations: ${{ steps.select-packages.outputs.publish-locations }} steps: - uses: actions/checkout@v6 @@ -144,6 +172,77 @@ jobs: id: commit-sha run: echo "COMMIT_SHA=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT" + - name: Select packages to publish + id: select-packages + if: ${{ inputs.is-monorepo && (inputs.packages != '' || inputs.only-changed-packages) }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + PR_NUMBER: ${{ inputs.pull-request || github.event.issue.number }} + PACKAGES: ${{ inputs.packages }} + SOURCE_SCOPE: ${{ inputs.source-scope }} + run: | + workspaces="$(yarn workspaces list --no-private --json)" + locations=() + + if [[ -n "$PACKAGES" ]]; then + mapfile -t names < <(jq -rn --arg input "$PACKAGES" --arg scope "$SOURCE_SCOPE" ' + $input + | split(",") + | map(gsub("^\\s+|\\s+$"; "")) + | map(select(. != "")) + | map(if startswith("@") then . else $scope + . end) + | unique + | .[] + ') + + if [[ ${#names[@]} -eq 0 ]]; then + echo "::error::No package names found in \"$PACKAGES\"." + exit 1 + fi + + has_unknown_packages=false + for name in "${names[@]}"; do + location="$(jq -r --arg name "$name" 'select(.name == $name) | .location' <<< "$workspaces")" + if [[ -z "$location" ]]; then + echo "::error::Unknown package: ${name}." + has_unknown_packages=true + else + locations+=("$location") + fi + done + + if [[ "$has_unknown_packages" == "true" ]]; then + exit 1 + fi + else + if [[ -n "$PR_NUMBER" ]]; then + base="$(gh pr view "$PR_NUMBER" --json baseRefName --jq '.baseRefName')" + else + base="$(gh api "repos/${GITHUB_REPOSITORY}" --jq '.default_branch')" + fi + + # Get the merge base from the GitHub API, so only that commit has to + # be fetched instead of the history leading up to it. + head="$(git rev-parse HEAD)" + merge_base="$(gh api "repos/${GITHUB_REPOSITORY}/compare/${base}...${head}" --jq '.merge_base_commit.sha')" + git fetch --no-tags --depth=1 origin "$merge_base" + + changed_files="$(git diff --name-only --no-renames "$merge_base" "$head")" + mapfile -t locations < <(jq -r --arg files "$changed_files" ' + select(.location as $location | $files | split("\n") | any(startswith($location + "/"))) + | .location + ' <<< "$workspaces") + + if [[ ${#locations[@]} -eq 0 ]]; then + echo "::warning::No packages changed compared to ${base}." + fi + fi + + for location in "${locations[@]}"; do + echo "- $location" + done + echo "publish-locations=$(jq -cn '$ARGS.positional' --args "${locations[@]}")" >> "$GITHUB_OUTPUT" + - name: Install dependencies (pre-build) if: ${{ inputs.rename-after-install-and-build }} run: yarn install --no-immutable diff --git a/.github/workflows/publish-preview-oidc.yml b/.github/workflows/publish-preview-oidc.yml index ffac1280..dee2f8d7 100644 --- a/.github/workflows/publish-preview-oidc.yml +++ b/.github/workflows/publish-preview-oidc.yml @@ -12,6 +12,16 @@ on: required: false type: string default: '' + packages: + description: 'Comma-separated list of packages to publish (monorepo only), e.g., "@metamask/network-controller, permission-controller". Names without a scope get the source scope. Mutually exclusive with "only-changed-packages".' + type: string + required: false + default: '' + only-changed-packages: + description: 'Only publish packages changed compared to the base branch of the pull request, or the default branch (monorepo only). Mutually exclusive with "packages".' + type: boolean + required: false + default: false npm-scope: description: 'Target NPM scope for preview packages' type: string @@ -69,6 +79,8 @@ jobs: with: pull-request: ${{ inputs.pull-request }} branch: ${{ inputs.branch }} + packages: ${{ inputs.packages }} + only-changed-packages: ${{ inputs.only-changed-packages }} npm-scope: ${{ inputs.npm-scope }} source-scope: ${{ inputs.source-scope }} build-command: ${{ inputs.build-command }} @@ -151,7 +163,17 @@ jobs: - name: Publish preview builds (monorepo) if: ${{ inputs.is-monorepo && !inputs.dry-run }} run: | - while IFS=$'\t' read -r name location; do + mapfile -t workspaces < <( + yarn workspaces list --no-private --json \ + | jq -r --argjson selected "${PUBLISH_LOCATIONS:-null}" ' + select($selected == null or (.location | IN($selected[]))) + | [.name, .location] + | @tsv + ' + ) + + for workspace in "${workspaces[@]}"; do + IFS=$'\t' read -r name location <<< "$workspace" version="$(jq -r '.version' "$location/package.json")" echo "::group::Publishing $name" if (cd "$location" && yarn npm publish --tag preview); then @@ -161,8 +183,9 @@ jobs: echo "${name}@${version}" >> "$RUNNER_TEMP/failed.txt" fi echo "::endgroup::" - done < <(yarn workspaces list --no-private --json | jq -r '[.name, .location] | @tsv') + done env: + PUBLISH_LOCATIONS: ${{ needs.build.outputs.publish-locations }} YARN_NPM_REGISTRY_SERVER: 'https://registry.npmjs.org' - name: Publish preview builds (polyrepo) diff --git a/CHANGELOG.md b/CHANGELOG.md index c6443b27..12d6550d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Add `packages` and `only-changed-packages` inputs to the `publish-preview-oidc` workflow, to publish a subset of the packages in a monorepo + - `packages` takes a comma-separated list of package names. Names without a scope get the source scope, e.g., `foo` becomes `@metamask/foo`. + - `only-changed-packages` publishes only the packages changed compared to the base branch of the pull request, or the default branch. + - All packages are still built. + ## [1.26.1] ### Fixed From 69d49898d0905eb2e72c9b37bf8bef36ba2ef5d9 Mon Sep 17 00:00:00 2001 From: Maarten Zuidhoorn Date: Thu, 19 Nov 2026 13:17:46 +0100 Subject: [PATCH 2/3] Add a header above the selected packages list The "Select packages to publish" step now prints "Packages to publish:" before the list of selected packages. This makes it clear in the log what the list is. --- .github/workflows/prepare-preview.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/prepare-preview.yml b/.github/workflows/prepare-preview.yml index dc71787d..a02180e5 100644 --- a/.github/workflows/prepare-preview.yml +++ b/.github/workflows/prepare-preview.yml @@ -238,6 +238,7 @@ jobs: fi fi + echo "Packages to publish:" for location in "${locations[@]}"; do echo "- $location" done From adbc3296d39e1ce74087b33e0d6fc77e266897e0 Mon Sep 17 00:00:00 2001 From: Maarten Zuidhoorn Date: Thu, 19 Nov 2026 13:19:54 +0100 Subject: [PATCH 3/3] Update description --- .github/workflows/prepare-preview.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/prepare-preview.yml b/.github/workflows/prepare-preview.yml index a02180e5..af4e5c70 100644 --- a/.github/workflows/prepare-preview.yml +++ b/.github/workflows/prepare-preview.yml @@ -29,7 +29,7 @@ on: type: string default: '' packages: - description: 'Comma-separated list of packages to publish (monorepo only). Names without a scope get the source scope. Mutually exclusive with "only-changed-packages". Empty = all packages.' + description: 'Comma-separated list of packages to publish (monorepo only). Names without a scope get the source scope. Mutually exclusive with "only-changed-packages".' type: string required: false default: ''