Publish npm packages automatically on version bumps (#1431)

* publish npm packages automatically when their version changes

* run the publish job in a main-only npm environment

* drop release tags so the publish job only needs read access

* split the package check and publish into separate workflows
This commit is contained in:
Gregor Vostrak
2026-12-02 13:19:58 +01:00
committed by GitHub
parent df71051feb
commit fcc7e0fed1
5 changed files with 104 additions and 64 deletions

View File

@@ -0,0 +1,37 @@
name: NPM Packages Check
# Type-checks and builds the shared packages on every PR that touches them.
on:
pull_request:
paths:
- 'resources/js/packages/**'
- 'package.json'
- 'package-lock.json'
- '.github/workflows/npm-packages-check.yml'
permissions:
contents: read
jobs:
check:
runs-on: ubuntu-latest
timeout-minutes: 15
env:
# Dependency order: ui and composables build against api
PACKAGES: api ui composables
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: '24.x'
- run: npm ci
- name: Type-check and build packages
run: |
for package in $PACKAGES; do
dir="resources/js/packages/$package"
[ -d "$dir" ] || continue
echo "::group::$package"
npm run type-check --if-present --prefix "$dir"
npm run build --prefix "$dir"
echo "::endgroup::"
done

66
.github/workflows/npm-packages.yml vendored Normal file
View File

@@ -0,0 +1,66 @@
name: NPM Packages
# Publishes each shared package whose version isn't on npm yet. npm's trusted
# publisher config points at this file, so keep unrelated changes out of it.
on:
push:
branches: [main]
paths:
- 'resources/js/packages/**'
- '.github/workflows/npm-packages.yml'
workflow_dispatch:
permissions:
contents: read
jobs:
publish:
runs-on: ubuntu-latest
timeout-minutes: 15
# Restricted to main in the repo settings, which a branch can't override
# by editing this file. npm's trusted publisher config requires it too.
environment: npm
concurrency:
group: npm-publish
cancel-in-progress: false
permissions:
contents: read
id-token: write # npm trusted publishing and provenance
env:
# Dependency order: ui and composables build against api
PACKAGES: api ui composables
steps:
- uses: actions/checkout@v7
with:
persist-credentials: false
- uses: actions/setup-node@v7
with:
# npm >= 11.5.1 is required for trusted publishing
node-version: '24.x'
registry-url: 'https://registry.npmjs.org'
- run: npm ci
- name: Publish new package versions
env:
# Fallback until trusted publishing is configured for every package
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
for package in $PACKAGES; do
dir="resources/js/packages/$package"
[ -d "$dir" ] || continue
name=$(node -p "require('./$dir/package.json').name")
version=$(node -p "require('./$dir/package.json').version")
# Build every package so later ones can build against it
npm run type-check --if-present --prefix "$dir"
npm run build --prefix "$dir"
if existing=$(npm view "$name@$version" version 2>&1); then
echo "$name@$version is already published"
continue
elif ! grep -q E404 <<< "$existing"; then
echo "$existing"
exit 1
fi
(cd "$dir" && npm publish --provenance --access public)
done

View File

@@ -1,32 +0,0 @@
name: Publish API package to NPM
on:
workflow_dispatch
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
steps:
- name: "Checkout code"
uses: actions/checkout@v7
# Setup .npmrc file to publish to npm
- name: Install root project dependencies
run: npm ci
- uses: actions/setup-node@v7
with:
node-version: '20.x'
registry-url: 'https://registry.npmjs.org'
- name: Install dependencies
run: npm ci
working-directory: ./resources/js/packages/api
- name: Build package
run: npm run build
working-directory: ./resources/js/packages/api
- name: Publish Package
run: npm publish --provenance --access public
working-directory: ./resources/js/packages/api
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

View File

@@ -1,32 +0,0 @@
name: Publish UI package to NPM
on:
workflow_dispatch
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
steps:
- name: "Checkout code"
uses: actions/checkout@v7
# Setup .npmrc file to publish to npm
- uses: actions/setup-node@v7
with:
node-version: '20.x'
registry-url: 'https://registry.npmjs.org'
- name: Install root project dependencies
run: npm ci
- name: Install package dependencies
run: npm ci
working-directory: ./resources/js/packages/ui
- name: Build package
run: npm run build
working-directory: ./resources/js/packages/ui
- name: Publish Package
run: npm publish --provenance --access public
working-directory: ./resources/js/packages/ui
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

View File

@@ -12,6 +12,7 @@
},
"scripts": {
"build": "vite build",
"type-check": "tsc --noEmit",
"watch": "vite build --watch",
"test": "echo \"Error: no test specified\" && exit 1"
},