Skip to content

Commit 1653b99

Browse files
mkernohanbcCopilot
andauthored
Publish npm packages to GitHub registry (#677)
* add GitHub Packages publishing step Co-authored-by: Copilot <copilot@github.com> * refactor publish GHA to handle publishing to npm and GH registries in separate jobs Co-authored-by: Copilot <copilot@github.com> * assign write permissions at job level * update workflow name * set registry-url at publish instead of in setup-node --------- Co-authored-by: Copilot <copilot@github.com>
1 parent 5b31ebb commit 1653b99

1 file changed

Lines changed: 92 additions & 28 deletions

File tree

.github/workflows/publish_react_component_library.yaml

Lines changed: 92 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Publish @bcgov/design-system-react-components to npm
1+
name: Publish @bcgov/design-system-react-components to npm and GitHub Packages
22

33
on:
44
pull_request:
@@ -10,16 +10,15 @@ on:
1010
types: [published]
1111

1212
permissions:
13-
id-token: write # Required for OIDC
1413
contents: read
1514

1615
jobs:
17-
publish-react-component-library-next:
18-
name: Publish @bcgov/design-system-react-components@next
19-
# Publish a `next` version on PR merge to main branch.
16+
publish-npm-next:
17+
# Publish a `next` version to npm on PR merge to `main`
2018
if: ${{ github.event.pull_request.merged == true && github.repository == 'bcgov/design-system' }}
19+
permissions:
20+
id-token: write # Required for OIDC
2121
runs-on: ubuntu-latest
22-
2322
steps:
2423
- name: Checkout code
2524
uses: actions/checkout@v6
@@ -41,17 +40,7 @@ jobs:
4140
- name: Install jq
4241
run: sudo apt-get install -y jq
4342

44-
# Use `jq` to change the package.json version to look like:
45-
#
46-
# <version in package.json>-pr<PR # that caused the workflow run>
47-
#
48-
# So package.json version v1.2.3 with a run caused by merging PR #456 to
49-
# `main` causes the version of the package on npm to look like:
50-
#
51-
# 1.2.3-pr456
52-
#
53-
# This is to ensure that it's easy to map an npm version to a particular PR.
54-
#
43+
# Append PR number to version number in package.json
5544
- name: Update version in package.json
5645
run: |
5746
PR_NUMBER=${{ github.event.pull_request.number }}
@@ -66,17 +55,61 @@ jobs:
6655
working-directory: ./packages/react-components
6756

6857
- name: Publish to npm @next
69-
run: npm publish --tag next
58+
run: npm publish --tag next --provenance
7059
working-directory: ./packages/react-components
7160

72-
publish-react-component-library-latest:
73-
name: Publish @bcgov/design-system-react-components@latest
74-
# Publish a `latest` version only when a release is published that targets
75-
# the React components package via a tag like:
76-
# @bcgov/design-system-react-components@v1.2.3
77-
if: ${{ github.event_name == 'release' && contains(github.event.release.tag_name, 'bcgov/design-system-react-components') }}
61+
publish-github-packages-next:
62+
# Publish a `next` version to GitHub Packages on PR merge to `main`
63+
if: ${{ github.event.pull_request.merged == true && github.repository == 'bcgov/design-system' }}
64+
permissions:
65+
packages: write
7866
runs-on: ubuntu-latest
67+
steps:
68+
- name: Checkout code
69+
uses: actions/checkout@v6
70+
71+
- name: Read .nvmrc
72+
run: echo "GITHUB_NVMRC_VERSION=$(cat .nvmrc)" >> $GITHUB_ENV
73+
working-directory: ./packages/react-components
74+
75+
- name: Set up Node.js for GitHub Packages
76+
uses: actions/setup-node@v6
77+
with:
78+
node-version: ${{ env.GITHUB_NVMRC_VERSION }}
79+
80+
- name: Install dependencies
81+
run: npm install
82+
working-directory: ./packages/react-components
83+
84+
- name: Install jq
85+
run: sudo apt-get install -y jq
86+
87+
# Append PR number to version number in package.json
88+
- name: Update version in package.json
89+
run: |
90+
PR_NUMBER=${{ github.event.pull_request.number }}
91+
CURRENT_VERSION=$(jq -r '.version' package.json)
92+
NEW_VERSION="${CURRENT_VERSION}-pr${PR_NUMBER}"
93+
jq --arg new_version "$NEW_VERSION" '.version = $new_version' package.json > tmp.json && mv tmp.json package.json
94+
shell: bash
95+
working-directory: ./packages/react-components
96+
97+
- name: Build components for publishing
98+
run: npm run build
99+
working-directory: ./packages/react-components
100+
101+
- name: Publish to GitHub Packages @next
102+
run: npm publish --tag next --registry=https://npm.pkg.github.com/
103+
working-directory: ./packages/react-components
104+
env:
105+
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
79106

107+
publish-npm-latest:
108+
# Publish a `latest` version to npm when a release is published that targets `@bcgov/design-system-react-components`
109+
if: ${{ github.event_name == 'release' && contains(github.event.release.tag_name, 'bcgov/design-system-react-components') }}
110+
permissions:
111+
id-token: write # Required for OIDC
112+
runs-on: ubuntu-latest
80113
steps:
81114
- name: Checkout code
82115
uses: actions/checkout@v6
@@ -89,6 +122,7 @@ jobs:
89122
uses: actions/setup-node@v6
90123
with:
91124
node-version: ${{ env.GITHUB_NVMRC_VERSION }}
125+
registry-url: "https://registry.npmjs.org/"
92126

93127
- name: Install dependencies
94128
run: npm install
@@ -98,9 +132,39 @@ jobs:
98132
run: npm run build
99133
working-directory: ./packages/react-components
100134

101-
- name: Set up .npmrc configuration
102-
run: echo "//registry.npmjs.org/" > ~/.npmrc
103-
104135
- name: Publish to npm @latest
105-
run: npm publish --tag latest
136+
run: npm publish --tag latest --provenance
137+
working-directory: ./packages/react-components
138+
139+
publish-github-packages-latest:
140+
# Publish a `latest` version to GitHub Packages when a release is published that targets `@bcgov/design-system-react-components`
141+
if: ${{ github.event_name == 'release' && contains(github.event.release.tag_name, 'bcgov/design-system-react-components') }}
142+
permissions:
143+
packages: write
144+
runs-on: ubuntu-latest
145+
steps:
146+
- name: Checkout code
147+
uses: actions/checkout@v6
148+
149+
- name: Read .nvmrc
150+
run: echo "GITHUB_NVMRC_VERSION=$(cat .nvmrc)" >> $GITHUB_ENV
151+
working-directory: ./packages/react-components
152+
153+
- name: Set up Node.js for GitHub Packages
154+
uses: actions/setup-node@v6
155+
with:
156+
node-version: ${{ env.GITHUB_NVMRC_VERSION }}
157+
158+
- name: Install dependencies
159+
run: npm install
160+
working-directory: ./packages/react-components
161+
162+
- name: Build components for publishing
163+
run: npm run build
164+
working-directory: ./packages/react-components
165+
166+
- name: Publish to GitHub Packages @latest
167+
run: npm publish --tag latest --registry=https://npm.pkg.github.com/
106168
working-directory: ./packages/react-components
169+
env:
170+
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)