Skip to content

Commit b4720bb

Browse files
feat: support tokenless trusted publishers (#90)
* feat: support tokenless trusted publishers * docs: add trusted publisher usage * fix: prevent error in dry-run publication due to collision with an already published version * style: typo * fix: update npm only if needed * style: use "preferred/legacy" instead of "deprecated"
1 parent d011d4c commit b4720bb

File tree

3 files changed

+65
-19
lines changed

3 files changed

+65
-19
lines changed

.github/workflows/publish-npm.yml

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ on:
8181
type: boolean
8282
secrets:
8383
npm-token:
84-
description: "NPM auth token (required unless `dry-run: true`)"
84+
description: "NPM auth token (required unless `dry-run: true` or workflow is called by a trusted publisher)"
8585
required: false
8686

8787
jobs:
@@ -93,16 +93,6 @@ jobs:
9393
packages: write
9494
id-token: write
9595
steps:
96-
- name: Ensure npm-token
97-
if: ${{ !inputs.dry-run }}
98-
run: |
99-
if [ -n "${{ secrets.npm-token }}" ]; then
100-
echo "`npm-token` secret is set"
101-
else
102-
echo "Missing `npm-token` secret (required unless `dry-run: true`)"
103-
exit 1
104-
fi
105-
10696
- name: Download extra artifact
10797
if: ${{ inputs.artifact-name != '' }}
10898
uses: actions/download-artifact@v4
@@ -118,6 +108,18 @@ jobs:
118108
registry-url: ${{ inputs.registry }}
119109
scope: ${{ inputs.scope }}
120110

111+
- name: Ensure npm version
112+
if: ${{ !inputs.dry-run }}
113+
run: |
114+
if [ -n "${{ secrets.npm-token }}" ]; then
115+
echo "npm-token secret is set not using OIDC"
116+
elif [ $(npx semver -r ">=11.5.1" $(npm -v)) ]; then
117+
echo "OIDC trusted publishing supported by current npm version"
118+
else
119+
echo "OIDC trusted publishing requires npm >= 11.5.1, updating npm"
120+
npm install -g npm@11
121+
fi
122+
121123
- name: Install dependencies
122124
working-directory: ${{ inputs.working-directory }}
123125
run: ${{ inputs.install-command }}
@@ -161,7 +163,8 @@ jobs:
161163
162164
DRY_RUN_OPT=""
163165
if [ "${{ inputs.dry-run }}" = "true" ]; then
164-
DRY_RUN_OPT="--dry-run"
166+
npm pkg set version=$(npm pkg get version | sed 's/"//g')-dry-run
167+
DRY_RUN_OPT="--dry-run --tag dry-run"
165168
fi
166169
167170
PROVENANCE_OPT=""

publish-npm/README.md

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ for the package scope, Node.js version, registry URL, and other options. The wor
4343

4444
### Secrets 🔐
4545

46-
| **Secret** | **Description** | **Required** |
47-
| ------------- | -------------------------------------------------- | ------------ |
48-
| **npm-token** | NPM auth token (required unless `dry-run: true`)". | No |
46+
| **Secret** | **Description** | **Required** |
47+
| ------------- | ---------------------------------------------------------------------------------------------- | ------------ |
48+
| **npm-token** | NPM auth token (required unless `dry-run: true` or workflow is called by a trusted publisher). | No |
4949

5050
## Job and Steps ⚙️
5151

@@ -60,10 +60,52 @@ for the package scope, Node.js version, registry URL, and other options. The wor
6060

6161
## How to Use This Reusable Workflow 🔄
6262

63-
1. **Save the Workflow File**
64-
Place this YAML file (e.g., `publish-npm.yml`) in the `.github/workflows/` directory of your repository. 💾
63+
### With Trusted Publishers (preferred)
64+
65+
> ⚠️ this method uses npm >= 11.5.1 ensure your project supports a compatible version of npm if not please use the method with npm token.
66+
67+
1. **Call the Reusable Workflow**
68+
69+
In another workflow file (e.g., triggered by a release), invoke this reusable workflow like so:
70+
71+
```yaml
72+
name: Call Publish Package NPM Workflow
73+
on:
74+
release:
75+
types: [published]
76+
77+
permissions:
78+
id-token: write # Required for OIDC
79+
packages: write
80+
contents: read
81+
82+
jobs:
83+
publish:
84+
uses: iExecBlockchainComputing/github-actions-workflows/.github/workflows/publish-npm.yml@main
85+
with:
86+
node-version: "22"
87+
build-command: "npm run build:prod"
88+
run-tests: true
89+
test-command: "npm run test:ci"
90+
lint-command: "npm run lint"
91+
type-check-command: "npm run check-types"
92+
format-check-command: "npm run check-format"
93+
# Optional: Download an artifact before building
94+
# artifact-name: 'my-build-artifact'
95+
# artifact-path: './dist'
96+
```
97+
98+
2. **Configure Trusted Publisher on NPM**
99+
100+
On [npmjs.com](https://www.npmjs.com/), configure the root publish workflow of your GitHub repository as a trusted publisher for your package.
101+
![trusted publisher](trusted-publisher.png)
102+
103+
NB: You can have only one trusted publisher per package, if you need multiple publication triggers (workflow_dispatch, release, etc.), you need to merge them into a single workflow referenced as trusted publisher.
104+
105+
### With npm token (legacy)
106+
107+
1. **Call the Reusable Workflow**
65108

66-
2. **Call the Reusable Workflow**
67109
In another workflow file (e.g., triggered by a release), invoke this reusable workflow like so:
68110

69111
```yaml
@@ -90,7 +132,8 @@ for the package scope, Node.js version, registry URL, and other options. The wor
90132
npm-token: ${{ secrets.NPM_TOKEN }}
91133
```
92134
93-
3. **Configure Secrets**
135+
2. **Configure Secrets**
136+
94137
Ensure that the `NPM_TOKEN` secret is added to your repository’s settings. This token is required to authenticate
95138
with the NPM registry during publishing. 🔑
96139

publish-npm/trusted-publisher.png

54.4 KB
Loading

0 commit comments

Comments
 (0)