-
-
Notifications
You must be signed in to change notification settings - Fork 6
Add preview builds #250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add preview builds #250
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| name: Publish a preview build | ||
|
|
||
| on: | ||
| issue_comment: | ||
| types: created | ||
|
|
||
| jobs: | ||
| is-fork-pull-request: | ||
| name: Determine whether this issue comment was on a pull request from a fork | ||
| if: ${{ github.event.issue.pull_request && startsWith(github.event.comment.body, '@metamaskbot publish-preview') }} | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| contents: read | ||
| pull-requests: read | ||
| outputs: | ||
| IS_FORK: ${{ steps.is-fork.outputs.IS_FORK }} | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - name: Determine whether this PR is from a fork | ||
| id: is-fork | ||
| run: echo "IS_FORK=$(gh pr view --json isCrossRepository --jq '.isCrossRepository' "${PR_NUMBER}" )" >> "$GITHUB_OUTPUT" | ||
| env: | ||
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| PR_NUMBER: ${{ github.event.issue.number }} | ||
|
|
||
| react-to-comment: | ||
| name: React to the comment | ||
| needs: is-fork-pull-request | ||
| # This ensures we don't publish on forks. We can't trust forks with this token. | ||
| if: ${{ needs.is-fork-pull-request.outputs.IS_FORK == 'false' }} | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| contents: read | ||
| pull-requests: write | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - name: React to the comment | ||
| run: | | ||
| gh api \ | ||
| --method POST \ | ||
| -H "Accept: application/vnd.github+json" \ | ||
| -H "X-GitHub-Api-Version: 2022-11-28" \ | ||
| "/repos/${REPO}/issues/comments/${COMMENT_ID}/reactions" \ | ||
| -f content='+1' | ||
| env: | ||
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| COMMENT_ID: ${{ github.event.comment.id }} | ||
| REPO: ${{ github.repository }} | ||
|
|
||
| publish-preview: | ||
| name: Publish build preview | ||
| needs: react-to-comment | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| contents: read | ||
| pull-requests: write | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - name: Check out pull request | ||
| run: gh pr checkout "${PR_NUMBER}" | ||
| env: | ||
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| PR_NUMBER: ${{ github.event.issue.number }} | ||
| - name: Checkout and setup environment | ||
| uses: MetaMask/action-checkout-and-setup@v1 | ||
| with: | ||
| is-high-risk-environment: true | ||
| - name: Get commit SHA | ||
| id: commit-sha | ||
| run: echo "COMMIT_SHA=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT" | ||
| - run: ./scripts/prepare-preview-builds.sh @metamask-previews ${{ steps.commit-sha.outputs.COMMIT_SHA }} | ||
| - run: yarn build | ||
| - name: Publish preview build | ||
| run: yarn npm publish --tag preview | ||
| env: | ||
| YARN_NPM_AUTH_TOKEN: ${{ secrets.PUBLISH_PREVIEW_NPM_TOKEN }} | ||
| - name: Post build preview in comment | ||
| run: ./scripts/generate-preview-build-message.sh | gh pr comment "${PR_NUMBER}" --body-file - | ||
| env: | ||
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| COMMIT_SHA: ${{ steps.commit-sha.outputs.COMMIT_SHA }} | ||
| PR_NUMBER: ${{ github.event.issue.number }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| main() { | ||
| local package_name | ||
| local package_version | ||
|
|
||
| package_name="$(jq -r '.name' < package.json)" | ||
| package_version="$(jq -r '.version' < package.json)" | ||
|
|
||
| local preview_build_message="\ | ||
| A preview build for this branch has been published. | ||
|
|
||
| You can configure your project to use the preview build with this identifier: | ||
|
|
||
| npm:${package_name}@${package_version} | ||
|
|
||
| [See these instructions](https://github.com/MetaMask/ppom-validator/blob/main/README.md#testing-changes-in-other-projects-using-preview-builds) for more information about preview builds.\ | ||
| " | ||
|
|
||
| echo "$preview_build_message" | ||
| } | ||
|
|
||
| main "$@" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # The name is overwritten, causing the package to get published under a | ||
| # different NPM scope than non-preview builds. | ||
| .name |= sub("@metamask/"; "\($npm_scope)/") | | ||
|
|
||
| # The prerelease version is overwritten, preserving the non-prerelease portion | ||
| # of the version. Technically we'd want to bump the non-prerelease portion as | ||
| # well if we wanted this to be SemVer-compliant, but it was simpler not to. | ||
| # This is just for testing, it doesn't need to strictly follow SemVer. | ||
| .version |= split("-")[0] + "-preview-\($short_commit_id)" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| # This script prepares a package to be published as a preview build to NPM. | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| prepare-preview-manifest() { | ||
| local manifest_file="$1" | ||
| local npm_scope="$2" | ||
| local short_commit_id="$3" | ||
|
|
||
| # jq does not support in-place modification of files, so a temporary file is | ||
| # used to store the result of the operation. The original file is then | ||
| # overwritten with the temporary file. | ||
| jq --raw-output --arg npm_scope "$npm_scope" --arg short_commit_id "$short_commit_id" --from-file scripts/prepare-preview-builds.jq "$manifest_file" > temp.json | ||
| mv temp.json "$manifest_file" | ||
| } | ||
|
|
||
| main() { | ||
| if [[ $# -lt 2 ]]; then | ||
| echo "USAGE: $0 NPM_SCOPE SHORT_GIT_COMMIT_HASH" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # We don't want to assume that preview builds will be published alongside | ||
| # "production" versions. There are security- and aesthetic-based advantages to | ||
| # keeping them separate. | ||
| local npm_scope="$1" | ||
|
|
||
| # We use the short commit ID as the prerelease version. This ensures each | ||
| # preview build is unique and can be linked to a specific commit. | ||
| local short_commit_id="$2" | ||
|
|
||
| echo "Preparing manifest..." | ||
| prepare-preview-manifest "package.json" "$npm_scope" "$short_commit_id" | ||
|
|
||
| echo "Installing dependencies..." | ||
| yarn install --no-immutable | ||
| } | ||
|
|
||
| main "$@" | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Script Argument Handling and File Creation Issues
The script expects exactly two arguments but only checks if zero are provided. If the argument count isn't exactly two, the script continues, potentially leaving
$2empty and causingjqto fail. Additionally,temp.jsonis created in the current directory without handling existing files or concurrent runs, risking race conditions or overwrites.