Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions .github/workflows/prepare-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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".'
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
Expand All @@ -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.'
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -144,6 +172,78 @@ 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

echo "Packages to publish:"
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
Expand Down
27 changes: 25 additions & 2 deletions .github/workflows/publish-preview-oidc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }}
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down