Skip to content

Commit 3697854

Browse files
committed
fix(repo,versioner): publish with npm via OIDC
Switch @dot/versioner to pack with pnpm and publish via an OIDC-capable npm CLI. Update the release workflow and .npmrc to drop token-based auth.
1 parent a094d9d commit 3697854

File tree

3 files changed

+56
-18
lines changed

3 files changed

+56
-18
lines changed

.github/workflows/release.yml

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ on:
55
branches:
66
- master
77

8+
permissions:
9+
contents: read
10+
811
jobs:
912
publish:
1013
if: |
@@ -15,29 +18,32 @@ jobs:
1518

1619
name: release
1720

21+
permissions:
22+
contents: write
23+
id-token: write
24+
1825
steps:
19-
- name: Checkout Commit
20-
uses: actions/checkout@v1
26+
- name: Checkout
27+
uses: actions/checkout@v4
28+
with:
29+
fetch-depth: 100
30+
fetch-tags: true
31+
ref: master
2132

2233
- name: Setup Node
23-
uses: actions/setup-node@v1
34+
uses: actions/setup-node@v4
2435
with:
25-
node-version: 18
36+
node-version: 20
2637

27-
- name: Checkout Master
28-
run: |
29-
git branch -f master origin/master
30-
git checkout master
38+
- name: Install PNPM
39+
uses: pnpm/action-setup@v4
3140

3241
- name: Sanity Check
3342
run: |
3443
echo branch `git branch --show-current`;
3544
echo node `node -v`;
3645
echo pnpm `pnpm -v`
3746
38-
- name: Install PNPM
39-
uses: pnpm/action-setup@v4
40-
4147
- name: Set Git Config
4248
run: |
4349
git config pull.rebase false
@@ -65,8 +71,16 @@ jobs:
6571
# Note: this satisfies aws sdk for @dot/config tests
6672
AWS_REGION: 'us-east-1'
6773

74+
- name: OIDC Preflight
75+
shell: bash
76+
run: |
77+
if [ -z "${ACTIONS_ID_TOKEN_REQUEST_URL:-}" ] || [ -z "${ACTIONS_ID_TOKEN_REQUEST_TOKEN:-}" ]; then
78+
echo "Missing GitHub Actions OIDC env vars (ACTIONS_ID_TOKEN_REQUEST_URL/TOKEN)." >&2
79+
echo "Ensure the job requests permissions: id-token: write." >&2
80+
exit 1
81+
fi
82+
83+
echo "OIDC env vars detected."
84+
6885
- name: Release and Publish Changed Packages
6986
run: pnpm --filter [HEAD^] --workspace-concurrency=1 release
70-
env:
71-
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
72-
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

.npmrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
2-
31
# npm options
42
auth-type=legacy
53

4+
# Publishing in CI uses GitHub OIDC (npm Trusted Publisher). For local publishing, authenticate via ~/.npmrc.
5+
66
# pnpm options
77
always-auth = true
88
enable-pre-post-scripts = true

packages/versioner/src/versioner.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import 'source-map-support';
22

33
import { dirname, join, resolve } from 'path';
4-
import { existsSync, readFileSync, writeFileSync } from 'fs';
4+
import { existsSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'fs';
5+
import { tmpdir } from 'os';
56

67
import { getLog } from '@dot/log';
78
import parser from 'conventional-commits-parser';
@@ -25,6 +26,7 @@ const parserOptions = {
2526
noteKeywords: ['BREAKING CHANGE', 'Breaking Change']
2627
};
2728
const reBreaking = new RegExp(`(${parserOptions.noteKeywords.join(')|(')})`);
29+
const NPM_CLI_SPEC = 'npm@11.5.1';
2830

2931
type Commit = parser.Commit<string | number | symbol>;
3032

@@ -153,7 +155,29 @@ const publish = async (cwd: string) => {
153155

154156
log.info(chalk`\n{cyan Publishing to NPM}`);
155157

156-
await execa('pnpm', ['publish', '--no-git-checks'], { cwd, stdio: 'inherit' });
158+
const packDir = mkdtempSync(join(tmpdir(), 'versioner-pack-'));
159+
try {
160+
await execa('pnpm', ['pack', '--pack-destination', packDir], { cwd, stdio: 'inherit' });
161+
162+
const tarballs = readdirSync(packDir).filter((file) => file.endsWith('.tgz'));
163+
const [tarball] = tarballs;
164+
if (!tarball) throw new Error(`Could not find packed tarball in: ${packDir}`);
165+
166+
const tarballPath = join(packDir, tarball);
167+
const hasOidcEnv =
168+
!!process.env.ACTIONS_ID_TOKEN_REQUEST_URL && !!process.env.ACTIONS_ID_TOKEN_REQUEST_TOKEN;
169+
const provenanceArgs = hasOidcEnv ? ['--provenance'] : [];
170+
171+
log.info(chalk`{grey Using npm CLI:} ${NPM_CLI_SPEC}`);
172+
173+
await execa(
174+
'pnpm',
175+
['dlx', NPM_CLI_SPEC, 'publish', '--no-git-checks', ...provenanceArgs, tarballPath],
176+
{ cwd, stdio: 'inherit' }
177+
);
178+
} finally {
179+
rmSync(packDir, { force: true, recursive: true });
180+
}
157181
};
158182

159183
const pull = async () => {

0 commit comments

Comments
 (0)