Skip to content

Commit e0a7654

Browse files
committed
ci: enable preview builds
1 parent c2a9577 commit e0a7654

File tree

5 files changed

+113
-0
lines changed

5 files changed

+113
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
name: Publish a preview build
2+
3+
on:
4+
issue_comment:
5+
types: created
6+
7+
jobs:
8+
is-fork-pull-request:
9+
name: Determine whether this issue comment was on a pull request from a fork
10+
if: ${{ github.event.issue.pull_request && startsWith(github.event.comment.body, '@metamaskbot publish-preview') }}
11+
runs-on: ubuntu-latest
12+
outputs:
13+
IS_FORK: ${{ steps.is-fork.outputs.IS_FORK }}
14+
steps:
15+
- uses: actions/checkout@v5
16+
- name: Determine whether this PR is from a fork
17+
id: is-fork
18+
run: echo "IS_FORK=$(gh pr view --json isCrossRepository --jq '.isCrossRepository' "${PR_NUMBER}" )" >> "$GITHUB_OUTPUT"
19+
env:
20+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
21+
PR_NUMBER: ${{ github.event.issue.number }}
22+
23+
publish-preview:
24+
name: Publish build preview
25+
needs: is-fork-pull-request
26+
permissions:
27+
pull-requests: write
28+
# This ensures we don't publish on forks. We can't trust forks with this token.
29+
if: ${{ needs.is-fork-pull-request.outputs.IS_FORK == 'false' }}
30+
runs-on: ubuntu-latest
31+
steps:
32+
- uses: actions/checkout@v5
33+
- name: Check out pull request
34+
run: gh pr checkout "${PR_NUMBER}"
35+
env:
36+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
37+
PR_NUMBER: ${{ github.event.issue.number }}
38+
- name: Checkout and setup environment
39+
uses: MetaMask/action-checkout-and-setup@v2
40+
with:
41+
is-high-risk-environment: true
42+
- name: Build
43+
- run: |
44+
export NODE_OPTIONS="--max_old_space_size=4096"
45+
yarn build
46+
- name: Get commit SHA
47+
id: commit-sha
48+
run: echo "COMMIT_SHA=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"
49+
- name: Prepare preview builds
50+
run: yarn prepare-preview-builds @metamask-previews ${{ steps.commit-sha.outputs.COMMIT_SHA }}
51+
- name: Publish preview build
52+
run: yarn publish-previews
53+
env:
54+
YARN_NPM_AUTH_TOKEN: ${{ secrets.PUBLISH_PREVIEW_NPM_TOKEN }}
55+
- name: Generate preview build message
56+
run: yarn tsx scripts/generate-preview-build-message.ts
57+
- name: Post build preview in comment
58+
run: gh pr comment "${PR_NUMBER}" --body-file preview-build-message.txt
59+
env:
60+
COMMIT_SHA: ${{ steps.commit-sha.outputs.COMMIT_SHA }}
61+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
62+
PR_NUMBER: ${{ github.event.issue.number }}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
"lint:fix": "yarn workspaces foreach --all --parallel --verbose run lint:fix",
2828
"lint:misc": "yarn workspaces foreach --all --parallel --verbose run lint:misc",
2929
"lint:types": "yarn workspaces foreach --all --parallel --verbose run lint:types",
30+
"prepare-preview-builds": "./scripts/prepare-preview-builds.sh",
31+
"publish-previews": "yarn workspaces foreach --all --no-private --parallel --verbose run publish:preview",
3032
"start": "yarn workspaces foreach --all --parallel --verbose --interlaced run start",
3133
"test": "yarn workspace @metamask/bitcoin-wallet-snap run test",
3234
"test:integration": "yarn workspace @metamask/bitcoin-wallet-snap run test:integration"

packages/snap/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write",
3232
"lint:misc": "prettier '**/*.json' '**/*.md' --check",
3333
"lint:types": "tsc --noEmit",
34+
"publish:preview": "yarn npm publish --tag preview",
3435
"prepublishOnly": "mm-snap manifest",
3536
"serve": "mm-snap serve",
3637
"start": "concurrently \"mm-snap watch\" \"yarn build:locale:watch\"",

scripts/prepare-preview-builds.jq

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# The name is overwritten, causing the package to get published under a
2+
# different NPM scope than non-preview builds.
3+
.name |= sub("@metamask/"; "\($npm_scope)/") |
4+
5+
# The prerelease version is overwritten, preserving the non-prerelease portion
6+
# of the version. Technically we'd want to bump the non-prerelease portion as
7+
# well if we wanted this to be SemVer-compliant, but it was simpler not to.
8+
# This is just for testing, it doesn't need to strictly follow SemVer.
9+
.version |= split("-")[0] + "-preview-\($hash)"

scripts/prepare-preview-builds.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
# This script prepares a package to be published as a preview build
6+
# to GitHub Packages.
7+
8+
if [[ $# -eq 0 ]]; then
9+
echo "Missing commit hash."
10+
exit 1
11+
fi
12+
13+
# We don't want to assume that preview builds will be published alongside
14+
# "production" versions. There are security- and aesthetic-based advantages to
15+
# keeping them separate.
16+
npm_scope="$1"
17+
18+
# We use the short commit hash as the prerelease version. This ensures each
19+
# preview build is unique and can be linked to a specific commit.
20+
shorthash="$2"
21+
22+
prepare-preview-manifest() {
23+
local manifest_file="$1"
24+
25+
# jq does not support in-place modification of files, so a temporary file is
26+
# used to store the result of the operation. The original file is then
27+
# overwritten with the temporary file.
28+
jq --raw-output --arg npm_scope "$npm_scope" --arg hash "$shorthash" --from-file scripts/prepare-preview-builds.jq "$manifest_file" > temp.json
29+
mv temp.json "$manifest_file"
30+
}
31+
32+
echo "Preparing manifests..."
33+
while IFS=$'\t' read -r location name; do
34+
echo "- $name"
35+
prepare-preview-manifest "$location/package.json"
36+
done < <(yarn workspaces list --no-private --json | jq --slurp --raw-output 'map(select(.location != ".")) | map([.location, .name]) | map(@tsv) | .[]')
37+
38+
echo "Installing dependencies..."
39+
yarn install --no-immutable

0 commit comments

Comments
 (0)