Skip to content

Commit 6308e5b

Browse files
authored
ci: add npm publishing to publish-release workflow (#237)
1 parent d2ea3fb commit 6308e5b

File tree

4 files changed

+138
-31
lines changed

4 files changed

+138
-31
lines changed
Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
name: Build, Lint, and Test
22

33
on:
4-
push:
5-
branches: [main]
6-
pull_request:
4+
workflow_call:
75

86
jobs:
97
build-lint-test:
@@ -15,7 +13,7 @@ jobs:
1513
steps:
1614
- uses: actions/checkout@v2
1715
- name: Use Node.js ${{ matrix.node-version }}
18-
uses: actions/setup-node@v2
16+
uses: actions/setup-node@v3
1917
with:
2018
node-version: ${{ matrix.node-version }}
2119
- name: Get Yarn cache directory
@@ -40,10 +38,3 @@ jobs:
4038
- name: Validate changelog
4139
if: ${{ !startsWith(github.head_ref, 'release/') }}
4240
run: yarn auto-changelog validate
43-
all-jobs-pass:
44-
name: All jobs pass
45-
runs-on: ubuntu-20.04
46-
needs:
47-
- build-lint-test
48-
steps:
49-
- run: echo "Great success!"

.github/workflows/create-release-pr.yml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
name: Create Release Pull Request
22

3+
permissions:
4+
contents: write
5+
pull-requests: write
6+
37
on:
48
workflow_dispatch:
59
inputs:
@@ -21,20 +25,18 @@ jobs:
2125
contents: write
2226
pull-requests: write
2327
steps:
24-
- uses: actions/checkout@v2
28+
- uses: actions/checkout@v3
2529
with:
2630
# This is to guarantee that the most recent tag is fetched.
2731
# This can be configured to a more reasonable value by consumers.
2832
fetch-depth: 0
2933
# We check out the specified branch, which will be used as the base
3034
# branch for all git operations and the release PR.
3135
ref: ${{ github.event.inputs.base-branch }}
32-
- name: Get Node.js version
33-
id: nvm
34-
run: echo ::set-output name=NODE_VERSION::$(cat .nvmrc)
35-
- uses: actions/setup-node@v2
36+
- name: Setup Node.js
37+
uses: actions/setup-node@v3
3638
with:
37-
node-version: ${{ steps.nvm.outputs.NODE_VERSION }}
39+
node-version-file: '.nvmrc'
3840
- uses: MetaMask/action-create-release-pr@v1
3941
env:
4042
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/main.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: Main
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
8+
jobs:
9+
build-lint-test:
10+
name: Test
11+
uses: ./.github/workflows/build-lint-test.yml
12+
13+
all-jobs-completed:
14+
name: All jobs completed
15+
runs-on: ubuntu-latest
16+
needs:
17+
- build-lint-test
18+
outputs:
19+
PASSED: ${{ steps.set-output.outputs.PASSED }}
20+
steps:
21+
- name: Set PASSED output
22+
id: set-output
23+
run: echo "PASSED=true" >> "$GITHUB_OUTPUT"
24+
25+
all-jobs-pass:
26+
name: All jobs pass
27+
if: ${{ always() }}
28+
runs-on: ubuntu-latest
29+
needs: all-jobs-completed
30+
steps:
31+
- name: Check that all jobs have passed
32+
run: |
33+
passed="${{ needs.all-jobs-completed.outputs.PASSED }}"
34+
if [[ $passed != "true" ]]; then
35+
exit 1
36+
fi
37+
38+
is-release:
39+
# Filtering by `push` events ensures that we only release from the `main` branch, which is a
40+
# requirement for our npm publishing environment.
41+
# The commit author should always be 'github-actions' for releases created by the
42+
# 'create-release-pr' workflow, so we filter by that as well to prevent accidentally
43+
# triggering a release.
44+
if: github.event_name == 'push' && startsWith(github.event.head_commit.author.name, 'github-actions')
45+
needs: all-jobs-pass
46+
outputs:
47+
IS_RELEASE: ${{ steps.is-release.outputs.IS_RELEASE }}
48+
runs-on: ubuntu-latest
49+
steps:
50+
- uses: MetaMask/action-is-release@v1
51+
id: is-release
52+
53+
publish-release:
54+
needs: is-release
55+
if: needs.is-release.outputs.IS_RELEASE == 'true'
56+
name: Publish release
57+
permissions:
58+
contents: write
59+
uses: ./.github/workflows/publish-release.yml
60+
secrets:
61+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
name: Publish Release
22

33
on:
4-
pull_request:
5-
types: [closed]
4+
workflow_call:
5+
secrets:
6+
NPM_TOKEN:
7+
required: true
68

79
jobs:
810
publish-release:
@@ -13,17 +15,68 @@ jobs:
1315
startsWith(github.event.pull_request.head.ref, 'release/')
1416
runs-on: ubuntu-latest
1517
steps:
16-
- uses: actions/checkout@v2
17-
with:
18-
# We check out the release pull request's base branch, which will be
19-
# used as the base branch for all git operations.
20-
ref: ${{ github.event.pull_request.base.ref }}
21-
- name: Get Node.js version
22-
id: nvm
23-
run: echo ::set-output name=NODE_VERSION::$(cat .nvmrc)
24-
- uses: actions/setup-node@v2
25-
with:
26-
node-version: ${{ steps.nvm.outputs.NODE_VERSION }}
27-
- uses: MetaMask/action-publish-release@v1
18+
- uses: actions/checkout@v3
19+
with:
20+
ref: ${{ github.sha }}
21+
- name: Setup Node.js
22+
uses: actions/setup-node@v3
23+
with:
24+
node-version-file: '.nvmrc'
25+
- uses: MetaMask/action-publish-release@v2
2826
env:
2927
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
28+
- name: Install
29+
run: |
30+
yarn install
31+
yarn build
32+
- uses: actions/cache@v3
33+
id: restore-build
34+
with:
35+
path: |
36+
./dist
37+
./node_modules/.yarn-state.yml
38+
key: ${{ github.sha }}
39+
40+
publish-npm-dry-run:
41+
runs-on: ubuntu-latest
42+
needs: publish-release
43+
steps:
44+
- uses: actions/checkout@v3
45+
with:
46+
ref: ${{ github.sha }}
47+
- uses: actions/cache@v3
48+
id: restore-build
49+
with:
50+
path: |
51+
./dist
52+
./node_modules/.yarn-state.yml
53+
key: ${{ github.sha }}
54+
- name: Dry Run Publish
55+
# omit npm-token token to perform dry run publish
56+
uses: MetaMask/action-npm-publish@v2
57+
env:
58+
SKIP_PREPACK: true
59+
60+
publish-npm:
61+
environment: npm-publish
62+
runs-on: ubuntu-latest
63+
needs: publish-npm-dry-run
64+
steps:
65+
- uses: actions/checkout@v3
66+
with:
67+
ref: ${{ github.sha }}
68+
- uses: actions/cache@v3
69+
id: restore-build
70+
with:
71+
path: |
72+
./dist
73+
./node_modules/.yarn-state.yml
74+
key: ${{ github.sha }}
75+
- name: Publish
76+
uses: MetaMask/action-npm-publish@v2
77+
with:
78+
# This `NPM_TOKEN` needs to be manually set per-repository.
79+
# Look in the repository settings under "Environments", and set this token in the `npm-publish` environment.
80+
npm-token: ${{ secrets.NPM_TOKEN }}
81+
env:
82+
SKIP_PREPACK: true

0 commit comments

Comments
 (0)