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
69 changes: 63 additions & 6 deletions .github/workflows/prepare-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ on:
description: 'Days to retain build artifacts'
type: number
required: true
pack:
description: 'Whether to upload packed tarballs of the packages to publish ("preview-build-tarballs"), instead of the workspace ("preview-build-artifacts").'
type: boolean
required: false
default: false
outputs:
is-fork:
description: 'Whether the pull request is from a fork. The build is skipped when it is.'
Expand Down Expand Up @@ -185,15 +190,20 @@ jobs:
locations=()

if [[ -n "$PACKAGES" ]]; then
mapfile -t names < <(jq -rn --arg input "$PACKAGES" --arg scope "$SOURCE_SCOPE" '
selected_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
| .[]
')
')"

names=()
if [[ -n "$selected_names" ]]; then
mapfile -t names <<< "$selected_names"
fi

if [[ ${#names[@]} -eq 0 ]]; then
echo "::error::No package names found in \"$PACKAGES\"."
Expand Down Expand Up @@ -228,10 +238,14 @@ jobs:
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" '
changed_locations="$(jq -r --arg files "$changed_files" '
select(.location as $location | $files | split("\n") | any(startswith($location + "/")))
| .location
' <<< "$workspaces")
' <<< "$workspaces")"

if [[ -n "$changed_locations" ]]; then
mapfile -t locations <<< "$changed_locations"
fi

if [[ ${#locations[@]} -eq 0 ]]; then
echo "::warning::No packages changed compared to ${base}."
Expand Down Expand Up @@ -323,8 +337,51 @@ jobs:
env: ${{ fromJSON(secrets.BUILD_ENV || '{}') }}
run: ${{ inputs.build-command }}

- name: Pack packages
if: ${{ inputs.pack }}
env:
IS_MONOREPO: ${{ inputs.is-monorepo }}
PUBLISH_LOCATIONS: ${{ steps.select-packages.outputs.publish-locations }}
run: |
if [[ "$IS_MONOREPO" == "true" ]]; then
workspaces="$(yarn workspaces list --no-private --json)"
selected="$(jq -r --argjson selected "${PUBLISH_LOCATIONS:-null}" '
select($selected == null or (.location | IN($selected[])))
| .location
' <<< "$workspaces")"

locations=()
if [[ -n "$selected" ]]; then
mapfile -t locations <<< "$selected"
fi
else
locations=(.)
fi

tarballs="${RUNNER_TEMP}/preview-build-tarballs"
mkdir -p "$tarballs"

for location in "${locations[@]}"; do
manifest="$location/package.json"
jq 'if .scripts then .scripts |= with_entries(select(.key | test("^(pre|post)?(pack|publish|prepare)$") | not)) else . end' "$manifest" > temp.json

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We previously did this in the publish workflow, but since we're now packing here, we need to do this here.

mv temp.json "$manifest"

name="$(jq -r '.name' "$manifest")"
echo "::group::Packing $name"
(cd "$location" && yarn pack --out "${tarballs}/${name//\//-}.tgz")
echo "::endgroup::"
done

- name: Upload tarballs
if: ${{ inputs.pack }}
uses: actions/upload-artifact@v7
with:
name: preview-build-tarballs
retention-days: ${{ inputs.artifact-retention-days }}
path: ${{ runner.temp }}/preview-build-tarballs/*.tgz

- name: Upload build artifacts (monorepo)
if: ${{ inputs.is-monorepo }}
if: ${{ !inputs.pack && inputs.is-monorepo }}
uses: actions/upload-artifact@v7
with:
name: preview-build-artifacts
Expand All @@ -340,7 +397,7 @@ jobs:
!./packages/**/*.test.*

- name: Upload build artifacts (polyrepo)
if: ${{ !inputs.is-monorepo }}
if: ${{ !inputs.pack && !inputs.is-monorepo }}
uses: actions/upload-artifact@v7
with:
name: preview-build-artifacts
Expand Down
195 changes: 98 additions & 97 deletions .github/workflows/publish-preview-oidc.yml

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommended to review this without whitespace.

Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ jobs:
is-monorepo: ${{ inputs.is-monorepo }}
rename-after-install-and-build: ${{ inputs.rename-after-install-and-build }}
artifact-retention-days: ${{ inputs.artifact-retention-days }}
pack: true
secrets:
BUILD_ENV: ${{ secrets.BUILD_ENV }}

Expand All @@ -101,123 +102,119 @@ jobs:
environment: ${{ inputs.environment }}
runs-on: ubuntu-latest
steps:
- name: Checkout and setup environment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI pointed this:

This job doesn't set up Node any more, so npm publish uses the runner's npm. OIDC publishing needs npm 11.5.1 or newer.

With an older npm, the publish fails at the token exchange with an auth error. Can we add actions/setup-node with node-version: 24 before the publish steps? so we don't rely on the runner's node version

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially added actions/setup-node here for that reason, but removed it later because I noticed GitHub includes NPM 11.19.1 by default in the latest image.

If we add something like node-version: '24', it's another place where we need to maintain a Node.js version. We already have some actions in this repository still using Node.js 18 etc. 😅 And we can't reference an .nvmrc without doing a checkout, which I tried to avoid.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we can rely on github keeping everything in sync then 👍

uses: MetaMask/action-checkout-and-setup@v3
with:
is-high-risk-environment: true

- name: Restore build artifacts
- name: Download tarballs
if: ${{ needs.build.outputs.publish-locations != '[]' }}
uses: actions/download-artifact@v8
with:
name: preview-build-artifacts
name: preview-build-tarballs
path: ${{ runner.temp }}/preview-build-tarballs

# The artifact package.json files come from the PR branch.
# A malicious PR could inject lifecycle scripts (prepack/postpack) that
# execute during `yarn npm publish` with the NPM token in the environment
# (enableScripts: false does NOT prevent pack/publish lifecycle scripts).
# It could also override publishConfig.registry to exfiltrate the token.
# We strip dangerous lifecycle scripts (they already ran during build)
# and block unexpected registries outright.
- name: Sanitize and validate artifact manifests
- name: Validate tarballs
env:
IS_MONOREPO: ${{ inputs.is-monorepo }}
NPM_SCOPE: ${{ inputs.npm-scope }}
run: |
bad=0
if [[ "$IS_MONOREPO" == "true" ]]; then
mapfile -t manifests < <(find packages -name package.json -not -path '*/node_modules/*')
else
manifests=(package.json)
fi
if [[ ${#manifests[@]} -eq 0 ]]; then
echo "::error::No package.json files found to validate"
exit 1
fi
# Strip registry overrides from .yarnrc.yml to prevent registry
# redirects that could exfiltrate the NPM token. npmPublishRegistry
# takes precedence over npmRegistryServer for yarn npm publish, and
# npmScopes can override per-scope. YARN_NPM_REGISTRY_SERVER env var
# only overrides npmRegistryServer, not the others.
if [[ -f .yarnrc.yml ]]; then
echo "Stripping registry config from .yarnrc.yml"
yq -i 'del(.npmRegistryServer) | del(.npmPublishRegistry) | del(.npmScopes)' .yarnrc.yml
fi
for f in "${manifests[@]}"; do
# Strip lifecycle scripts that run during pack/publish
if jq -e '.scripts // {} | keys[] | select(test("^(pre|post)?(pack|publish|prepare)$"))' "$f" > /dev/null 2>&1; then
echo "Stripping lifecycle scripts from $f"
jq 'if .scripts then .scripts |= with_entries(select(.key | test("^(pre|post)?(pack|publish|prepare)$") | not)) else . end' "$f" > "${f}.tmp"
mv "${f}.tmp" "$f"
shopt -s nullglob
has_invalid_tarballs=false

for tarball in "$RUNNER_TEMP"/preview-build-tarballs/*.tgz; do
file="$(basename "$tarball")"

if ! entries="$(tar -tzf "$tarball")"; then
echo "::error::${file} isn't a valid tarball."
has_invalid_tarballs=true
continue
fi
# Block unexpected registries
reg=$(jq -r '.publishConfig.registry // ""' "$f")
if [[ -n "$reg" && "$reg" != "https://registry.npmjs.org/" ]]; then
echo "::error::Unexpected registry in $f: $reg"
bad=1

if grep -qv '^package/' <<< "$entries" || grep -qE '//|(^|/)\.\.?(/|$)' <<< "$entries"; then
echo "::error::${file} contains unexpected paths."
has_invalid_tarballs=true
continue
fi

if [[ "$(grep -cx 'package/package.json' <<< "$entries")" -ne 1 ]]; then
echo "::error::${file} must contain exactly one package/package.json."
has_invalid_tarballs=true
continue
fi

# The name must be in the preview scope, and the version must be a
# preview version. publishConfig may only set the access and the
# npm registry, since npm applies it when publishing.
if ! tar -xzOf "$tarball" package/package.json | jq -e --arg scope "${NPM_SCOPE}/" '
(.name | type == "string" and startswith($scope))
Comment thread
GuillaumeRx marked this conversation as resolved.
Resolved
and (.version | type == "string" and test("-preview-[0-9a-f]+$"))
and ((.publishConfig // {}) | type == "object"
and (keys - ["access", "registry"] | length == 0)
and ((.registry // "https://registry.npmjs.org/") | IN("https://registry.npmjs.org", "https://registry.npmjs.org/")))
' > /dev/null; then
echo "::error::${file} has an invalid name, version, or publishConfig."
has_invalid_tarballs=true
fi
done
exit "$bad"

- name: Reconcile workspace state
run: yarn install --no-immutable
if [[ "$has_invalid_tarballs" == "true" ]]; then
exit 1
fi

- name: Publish preview builds (monorepo)
if: ${{ inputs.is-monorepo && !inputs.dry-run }}
run: |
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
'
)
shopt -s nullglob

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
echo "${name}@${version}" >> "$RUNNER_TEMP/published.txt"
for tarball in "$RUNNER_TEMP"/preview-build-tarballs/*.tgz; do
package="$(tar -xzOf "$tarball" package/package.json | jq -r '"\(.name)@\(.version)"')"
echo "::group::Publishing $package"
if npm publish "$tarball" \
--tag preview \
--ignore-scripts \
--provenance=false \
--registry https://registry.npmjs.org/; then
echo "$package" >> "$RUNNER_TEMP/published.txt"
else
echo "::warning::Failed to publish $name"
echo "${name}@${version}" >> "$RUNNER_TEMP/failed.txt"
echo "::warning::Failed to publish $package"
echo "$package" >> "$RUNNER_TEMP/failed.txt"
fi
echo "::endgroup::"
done
env:
PUBLISH_LOCATIONS: ${{ needs.build.outputs.publish-locations }}
YARN_NPM_REGISTRY_SERVER: 'https://registry.npmjs.org'

- name: Publish preview builds (polyrepo)
- name: Publish preview build (polyrepo)
if: ${{ !inputs.is-monorepo && !inputs.dry-run }}
run: yarn npm publish --tag preview
env:
YARN_NPM_REGISTRY_SERVER: 'https://registry.npmjs.org'
run: |
tarballs=("$RUNNER_TEMP"/preview-build-tarballs/*.tgz)
if [[ ${#tarballs[@]} -ne 1 ]]; then
echo "::error::Expected exactly one tarball, found ${#tarballs[@]}."
exit 1
fi

npm publish "${tarballs[0]}" \
--tag preview \
--ignore-scripts \
--provenance=false \
--registry https://registry.npmjs.org/

- name: Dry run notice
if: ${{ inputs.dry-run }}
run: echo "Dry run — skipping publish"

- name: Generate preview build message
- name: Generate preview build message (monorepo)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Split this into a separate step for monorepos and polyrepos, so it's a bit more readable.

if: ${{ inputs.is-monorepo }}
env:
IS_MONOREPO: ${{ inputs.is-monorepo }}
DOCS_URL: ${{ inputs.docs-url }}
run: |
docs_link=""
if [[ -n "$DOCS_URL" ]]; then
docs_link="[Learn how to use preview builds in other projects](${DOCS_URL})."
fi

if [[ "$IS_MONOREPO" == "true" ]]; then
published="$(cat "$RUNNER_TEMP/published.txt" 2>/dev/null || true)"
failed="$(cat "$RUNNER_TEMP/failed.txt" 2>/dev/null || true)"
published="$(cat "$RUNNER_TEMP/published.txt" 2>/dev/null || true)"
failed="$(cat "$RUNNER_TEMP/failed.txt" 2>/dev/null || true)"

if [[ -n "$published" ]]; then
echo -n "Preview builds have been published." > preview-build-message.txt
if [[ -n "$docs_link" ]]; then
echo -n " ${docs_link}" >> preview-build-message.txt
fi
cat <<-MSGEOF >> preview-build-message.txt
if [[ -n "$published" ]]; then
echo -n "Preview builds have been published." > preview-build-message.txt
if [[ -n "$docs_link" ]]; then
echo -n " ${docs_link}" >> preview-build-message.txt
fi
cat <<-MSGEOF >> preview-build-message.txt

<details>
<summary>Expand for full list of packages and versions.</summary>
Expand All @@ -228,33 +225,36 @@ jobs:

</details>
MSGEOF
else
echo "No preview builds were published." > preview-build-message.txt
fi
else
echo "No preview builds were published." > preview-build-message.txt
fi

if [[ -n "$failed" ]]; then
cat <<-MSGEOF >> preview-build-message.txt
if [[ -n "$failed" ]]; then
cat <<-MSGEOF >> preview-build-message.txt

The following packages failed to publish. [See the workflow run for details](https://github.com/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}/attempts/${GITHUB_RUN_ATTEMPT}).

\`\`\`
${failed}
\`\`\`
MSGEOF
fi
else
name="$(jq -r '.name' package.json)"
version="$(jq -r '.version' package.json)"
cat <<-MSGEOF > preview-build-message.txt
fi

- name: Generate preview build message (polyrepo)
if: ${{ !inputs.is-monorepo }}
env:
DOCS_URL: ${{ inputs.docs-url }}
run: |
package="$(tar -xzOf "$RUNNER_TEMP"/preview-build-tarballs/*.tgz package/package.json | jq -r '"\(.name)@\(.version)"')"
cat <<-MSGEOF > preview-build-message.txt
The following preview build has been published:

\`\`\`
${name}@${version}
${package}
\`\`\`
MSGEOF
if [[ -n "$docs_link" ]]; then
printf '\n%s\n' "$docs_link" >> preview-build-message.txt
fi
if [[ -n "$DOCS_URL" ]]; then
printf '\n%s\n' "[Learn how to use preview builds in other projects](${DOCS_URL})." >> preview-build-message.txt
fi

- name: Add build preview to the job summary
Expand All @@ -264,5 +264,6 @@ jobs:
if: ${{ inputs.pull-request }}
run: gh pr comment "${PR_NUMBER}" --body-file preview-build-message.txt
env:
GH_REPO: ${{ github.repository }}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without a checkout, the gh CLI needs this specified explicitly.

GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ inputs.pull-request }}
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- Publish tarballs packed in the build job in the `publish-preview-oidc` workflow, instead of the workspace ([#348](https://github.com/MetaMask/github-tools/pull/348))
- The publish job no longer checks out the repository or runs Yarn. It publishes the tarballs with `npm publish`, after checking that each package is in the preview scope and has a preview version.
- Add a `pack` input to the `prepare-preview` workflow, to upload the tarballs instead of the workspace.

## [1.27.0]

### Added
Expand Down