Skip to content

Commit 4ff4989

Browse files
authored
Merge pull request #83 from netfoundry/publish-theme-action
update publish action
2 parents fbd40e6 + 0f2e4d6 commit 4ff4989

4 files changed

Lines changed: 210 additions & 10 deletions

File tree

.github/workflows/pubshared.yml

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,72 @@ name: Publish the shared library to npmjs
22

33
on:
44
workflow_dispatch:
5+
inputs:
6+
dry-run:
7+
description: "Run npm publish in --dry-run mode (no upload)"
8+
type: boolean
9+
default: false
510

611
permissions:
7-
id-token: write # Required for OIDC
12+
id-token: write # required for npm OIDC trusted publisher
813
contents: read
914

1015
jobs:
1116
publish:
12-
defaults:
13-
run:
14-
working-directory: lib/packages/docusaurus-shared
1517
runs-on: ubuntu-latest
1618
steps:
19+
- name: Refuse to run from non-main branches
20+
if: github.ref != 'refs/heads/main'
21+
run: |
22+
echo "::error::This workflow may only be dispatched from main (got ${{ github.ref }})."
23+
exit 1
24+
1725
- uses: actions/checkout@v4
1826

1927
- uses: actions/setup-node@v4
2028
with:
2129
node-version: '24'
2230
registry-url: 'https://registry.npmjs.org'
2331

24-
- run: corepack enable
25-
- run: yarn install --immutable
26-
- run: yarn build --if-present
27-
- run: yarn test
28-
- run: npm publish
32+
# Pre-flight: read version from package.json, fail loudly *before*
33+
# spending time on install/build if the version is already on npm.
34+
- name: Verify version is not already published
35+
id: precheck
36+
run: |
37+
set -euo pipefail
38+
name="$(node -p "require('./packages/docusaurus-theme/package.json').name")"
39+
version="$(node -p "require('./packages/docusaurus-theme/package.json').version")"
40+
echo "name=$name" >> "$GITHUB_OUTPUT"
41+
echo "version=$version" >> "$GITHUB_OUTPUT"
42+
echo "Checking $name@$version on npm..."
43+
existing="$(npm view "${name}@${version}" version 2>/dev/null || true)"
44+
if [ -n "$existing" ]; then
45+
echo "::error title=Version already published::${name}@${version} is already on npm. Bump packages/docusaurus-theme/package.json before re-running."
46+
exit 1
47+
fi
48+
echo "OK: ${name}@${version} is not on npm yet."
49+
50+
- name: Install, build, test, publish
51+
run: ./scripts/publish-theme.sh ${{ inputs.dry-run && '--dry-run' || '' }}
52+
53+
- name: Explain npm publish failure
54+
if: failure() && steps.precheck.outcome == 'success'
55+
run: |
56+
cat <<'EOF'
57+
::error title=Publish failed::npm publish failed after the version pre-check passed.
58+
59+
Most common cause: npm Trusted Publisher (OIDC) is not yet
60+
configured for this package + workflow. To fix:
61+
62+
1. Log in to npmjs.com as a maintainer of @netfoundry/docusaurus-theme
63+
2. Go to: package > Settings > Trusted Publishers > Add
64+
3. Provider: GitHub Actions
65+
Organization: netfoundry
66+
Repository: docusaurus-shared
67+
Workflow file: .github/workflows/pubshared.yml
68+
Environment: (leave blank)
69+
4. Re-run this workflow.
70+
71+
If trusted publisher is already configured, check the publish step
72+
logs above for the actual npm error.
73+
EOF

packages/docusaurus-theme/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@netfoundry/docusaurus-theme",
3-
"version": "0.12.1",
3+
"version": "0.12.2",
44
"description": "NetFoundry Docusaurus theme with shared layout, footer, and styling",
55
"main": "dist/src/index.js",
66
"types": "dist/src/index.d.ts",

scripts/publish-theme.ps1

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<#
2+
.SYNOPSIS
3+
Publish @netfoundry/docusaurus-theme to npmjs.
4+
5+
.DESCRIPTION
6+
PowerShell mirror of publish-theme.sh. Keep the two in sync -- see
7+
scripts/CLAUDE.md.
8+
9+
Local usage:
10+
./scripts/publish-theme.ps1 # real publish
11+
./scripts/publish-theme.ps1 -DryRun # npm publish --dry-run
12+
./scripts/publish-theme.ps1 -CheckOnly # only verify version is not on npm
13+
14+
Real (non-dry-run) publishes expect either:
15+
- npm OIDC trusted-publisher auth (CI), or
16+
- `npm login` already done in the current shell (local).
17+
#>
18+
[CmdletBinding()]
19+
param(
20+
[switch]$DryRun,
21+
[switch]$CheckOnly
22+
)
23+
24+
$ErrorActionPreference = 'Stop'
25+
26+
$repoRoot = (Resolve-Path (Join-Path $PSScriptRoot '..')).Path
27+
$pkgDir = Join-Path $repoRoot 'packages/docusaurus-theme'
28+
$pkgJson = Join-Path $pkgDir 'package.json'
29+
30+
Set-Location $repoRoot
31+
32+
$pkg = Get-Content $pkgJson -Raw | ConvertFrom-Json
33+
$name = $pkg.name
34+
$version = $pkg.version
35+
36+
Write-Host "==> Publishing $name@$version"
37+
Write-Host " repo root: $repoRoot"
38+
Write-Host " dry run: $DryRun"
39+
Write-Host " check only: $CheckOnly"
40+
41+
Write-Host "==> Checking npm registry for existing $name@$version"
42+
# `npm view <pkg>@<ver> version` prints the version if it exists, empty if not.
43+
# It can fail for a not-yet-published name -- swallow that.
44+
$existing = & npm view "$name@$version" version 2>$null
45+
if ($LASTEXITCODE -ne 0) { $existing = '' }
46+
if ($existing) {
47+
Write-Error @"
48+
$name@$version is already published on npm.
49+
Bump the version in packages/docusaurus-theme/package.json before re-running.
50+
"@
51+
exit 1
52+
}
53+
Write-Host " not on registry -- ok to publish"
54+
55+
if ($CheckOnly) {
56+
Write-Host "==> --CheckOnly set; stopping before install/build/publish."
57+
exit 0
58+
}
59+
60+
Write-Host "==> yarn install"
61+
yarn install --frozen-lockfile
62+
if ($LASTEXITCODE -ne 0) { throw "yarn install failed" }
63+
64+
Write-Host "==> yarn theme:build"
65+
yarn theme:build
66+
if ($LASTEXITCODE -ne 0) { throw "yarn theme:build failed" }
67+
68+
Write-Host "==> yarn test"
69+
yarn test
70+
if ($LASTEXITCODE -ne 0) { throw "yarn test failed" }
71+
72+
Write-Host "==> npm publish"
73+
$publishArgs = @('publish','--access','public','--provenance')
74+
if ($DryRun) { $publishArgs += '--dry-run' }
75+
76+
Push-Location $pkgDir
77+
try {
78+
& npm @publishArgs
79+
if ($LASTEXITCODE -ne 0) {
80+
Write-Error @"
81+
npm publish failed (exit $LASTEXITCODE).
82+
If running in CI: confirm npm Trusted Publisher is configured for this
83+
package + workflow (npmjs.com > package > Settings > Trusted Publishers).
84+
If running locally: confirm 'npm whoami' returns the right account.
85+
"@
86+
exit 1
87+
}
88+
} finally {
89+
Pop-Location
90+
}
91+
92+
Write-Host "==> Done."

scripts/publish-theme.sh

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Publish @netfoundry/docusaurus-theme to npmjs.
4+
#
5+
# Local usage:
6+
# ./scripts/publish-theme.sh [--dry-run]
7+
#
8+
# In CI this is invoked by .github/workflows/pubshared.yml. For real
9+
# (non-dry-run) publishes the script expects OIDC trusted-publisher auth
10+
# to be configured on npm; locally you can either use --dry-run or have
11+
# `npm login` already done in your shell.
12+
#
13+
# Exits non-zero if the version in package.json is already published.
14+
15+
set -euo pipefail
16+
17+
DRY_RUN=0
18+
for arg in "$@"; do
19+
case "$arg" in
20+
--dry-run) DRY_RUN=1 ;;
21+
*) echo "unknown arg: $arg" >&2; exit 2 ;;
22+
esac
23+
done
24+
25+
repo_root="$(cd "$(dirname "$0")/.." && pwd)"
26+
pkg_dir="$repo_root/packages/docusaurus-theme"
27+
cd "$repo_root"
28+
29+
name="$(node -p "require('./packages/docusaurus-theme/package.json').name")"
30+
version="$(node -p "require('./packages/docusaurus-theme/package.json').version")"
31+
32+
echo "==> Publishing $name@$version"
33+
echo " repo root: $repo_root"
34+
echo " dry run: $DRY_RUN"
35+
36+
echo "==> Checking npm registry for existing $name@$version"
37+
# `npm view <pkg>@<ver> version` prints the version if it exists, empty if not.
38+
# It fails (non-zero) for a not-yet-published package name -- swallow that.
39+
existing="$(npm view "${name}@${version}" version 2>/dev/null || true)"
40+
if [ -n "$existing" ]; then
41+
echo "ERROR: $name@$version is already published on npm. Bump the version in" >&2
42+
echo " packages/docusaurus-theme/package.json before re-running." >&2
43+
exit 1
44+
fi
45+
echo " not on registry -- ok to publish"
46+
47+
echo "==> yarn install"
48+
yarn install --frozen-lockfile
49+
50+
echo "==> yarn theme:build"
51+
yarn theme:build
52+
53+
echo "==> yarn test"
54+
yarn test
55+
56+
echo "==> npm publish"
57+
publish_args=(--access public --provenance)
58+
if [ "$DRY_RUN" -eq 1 ]; then
59+
publish_args+=(--dry-run)
60+
fi
61+
( cd "$pkg_dir" && npm publish "${publish_args[@]}" )
62+
63+
echo "==> Done."

0 commit comments

Comments
 (0)