Skip to content

Commit ebfea75

Browse files
committed
refactor design tokens publish workflow to support publishing to GitHub Packages
1 parent 1653b99 commit ebfea75

1 file changed

Lines changed: 110 additions & 8 deletions

File tree

.github/workflows/publish_design_tokens.yaml

Lines changed: 110 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Publish @bcgov/design-tokens to npm
1+
name: Publish @bcgov/design-tokens to npm and GitHub Packages
22

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

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

1615
jobs:
17-
publish-design-tokens-next:
18-
name: Publish @bcgov/design-tokens@next
19-
# Publish a `next` version on PR merge to main branch.
16+
publish-design-tokens-npm-next:
17+
name: Publish @bcgov/design-tokens@next to npm
18+
# Publish a `next` version to npm on PR merge to main branch.
2019
if: ${{ github.event.pull_request.merged == true && github.repository == 'bcgov/design-system' }}
20+
permissions:
21+
id-token: write # Required for OIDC
2122
runs-on: ubuntu-latest
2223

2324
steps:
@@ -73,12 +74,74 @@ jobs:
7374
run: npm publish --tag next
7475
working-directory: ./packages/design-tokens/dist
7576

76-
publish-design-tokens-latest:
77-
name: Publish @bcgov/design-tokens@latest
78-
# Publish a `latest` version only when a release is published that targets
77+
publish-design-tokens-github-packages-next:
78+
name: Publish @bcgov/design-tokens@next to GitHub Packages
79+
# Publish a `next` version to GitHub Packages on PR merge to main branch.
80+
if: ${{ github.event.pull_request.merged == true && github.repository == 'bcgov/design-system' }}
81+
permissions:
82+
packages: write
83+
runs-on: ubuntu-latest
84+
85+
steps:
86+
- name: Checkout code
87+
uses: actions/checkout@v6
88+
89+
- name: Read .nvmrc
90+
run: echo "GITHUB_NVMRC_VERSION=$(cat .nvmrc)" >> $GITHUB_ENV
91+
working-directory: ./packages/design-tokens
92+
93+
- name: Set up Node.js
94+
uses: actions/setup-node@v6
95+
with:
96+
node-version: ${{ env.GITHUB_NVMRC_VERSION }}
97+
98+
- name: Install dependencies
99+
run: npm install
100+
working-directory: ./packages/design-tokens
101+
102+
- name: Install jq
103+
run: sudo apt-get install -y jq
104+
105+
- name: Run build script
106+
run: npm run build
107+
working-directory: ./packages/design-tokens
108+
109+
- name: Prepare npm package for publishing
110+
run: npm run prepare-npm-package
111+
working-directory: ./packages/design-tokens
112+
113+
# Use `jq` to change the package.json version to look like:
114+
#
115+
# <version in package.json>-pr<PR # that caused the workflow run>
116+
#
117+
# So package.json version v1.2.3 with a run caused by merging PR #456 to
118+
# `main` causes the version of the package on npm to look like:
119+
#
120+
# 1.2.3-pr456
121+
#
122+
# This is to ensure that it's easy to map an npm version to a particular PR.
123+
#
124+
- name: Update version in dist/package.json
125+
run: |
126+
PR_NUMBER=${{ github.event.pull_request.number }}
127+
CURRENT_VERSION=$(jq -r '.version' package.json)
128+
NEW_VERSION="${CURRENT_VERSION}-pr${PR_NUMBER}"
129+
jq --arg new_version "$NEW_VERSION" '.version = $new_version' package.json > tmp.json && mv tmp.json package.json
130+
shell: bash
131+
working-directory: ./packages/design-tokens/dist
132+
133+
- name: Publish to npm @next
134+
run: npm publish --tag next --registry=https://npm.pkg.github.com/
135+
working-directory: ./packages/design-tokens/dist
136+
137+
publish-design-tokens-npm-latest:
138+
name: Publish @bcgov/design-tokens@latest to npm
139+
# Publish a `latest` version to npm only when a release is published that targets
79140
# the design tokens package via a tag like:
80141
# @bcgov/design-tokens@v1.2.3
81142
if: ${{ github.event_name == 'release' && contains(github.event.release.tag_name, 'bcgov/design-tokens') }}
143+
permissions:
144+
id-token: write # Required for OIDC
82145
runs-on: ubuntu-latest
83146

84147
steps:
@@ -110,3 +173,42 @@ jobs:
110173
- name: Publish to npm @latest
111174
run: npm publish --tag latest
112175
working-directory: ./packages/design-tokens/dist
176+
177+
publish-design-tokens-github-packages-latest:
178+
name: Publish @bcgov/design-tokens@latest to GitHub Packages
179+
# Publish a `latest` version to GitHub Packages only when a release is published that targets
180+
# the design tokens package via a tag like:
181+
# @bcgov/design-tokens@v1.2.3
182+
if: ${{ github.event_name == 'release' && contains(github.event.release.tag_name, 'bcgov/design-tokens') }}
183+
permissions:
184+
packages: write
185+
runs-on: ubuntu-latest
186+
187+
steps:
188+
- name: Checkout code
189+
uses: actions/checkout@v6
190+
191+
- name: Read .nvmrc
192+
run: echo "GITHUB_NVMRC_VERSION=$(cat .nvmrc)" >> $GITHUB_ENV
193+
working-directory: ./packages/design-tokens
194+
195+
- name: Set up Node.js
196+
uses: actions/setup-node@v6
197+
with:
198+
node-version: ${{ env.GITHUB_NVMRC_VERSION }}
199+
200+
- name: Install dependencies
201+
run: npm install
202+
working-directory: ./packages/design-tokens
203+
204+
- name: Run build script
205+
run: npm run build
206+
working-directory: ./packages/design-tokens
207+
208+
- name: Prepare npm package for publishing
209+
run: npm run prepare-npm-package
210+
working-directory: ./packages/design-tokens
211+
212+
- name: Publish to GitHub Packages @latest
213+
run: npm publish --tag latest --registry=https://npm.pkg.github.com/
214+
working-directory: ./packages/design-tokens/dist

0 commit comments

Comments
 (0)