@lewisliu0717/npm-oidc-demo 0.0.0-stage → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +64 -2
- package/index.js +3 -0
- package/package.json +22 -4
package/README.md
CHANGED
|
@@ -1,3 +1,65 @@
|
|
|
1
|
-
#
|
|
1
|
+
# npm OIDC 发布案例
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
这个案例只演示 GitHub Actions 如何向 npm 证明身份并发布一个最小包,不依赖公司的代码或包。
|
|
4
|
+
|
|
5
|
+
测试包:`@lewisliu0717/npm-oidc-demo`。源码仓库为私有 GitHub 仓库;npm 测试包使用公开可见性,便于使用个人免费账号验证。
|
|
6
|
+
|
|
7
|
+
## 发布方式
|
|
8
|
+
|
|
9
|
+
手动运行工作流时,`dry_run` 默认勾选,只验证身份和打包安装,不发布到 npm。真正发布时才取消勾选。
|
|
10
|
+
|
|
11
|
+
## 一个工作流,两个任务
|
|
12
|
+
|
|
13
|
+
`.github/workflows/publish.yml` 对应 Actions 页面中的 `npm OIDC demo`:
|
|
14
|
+
|
|
15
|
+
- `check`:推送代码后自动运行,检查语法和将要打包的文件,不执行发布。
|
|
16
|
+
- 第二个任务只在 Actions 页面从 `main` 分支手动运行时执行;先通过 `check`,再根据 `dry_run` 选择预演或正式发布。
|
|
17
|
+
|
|
18
|
+
## 没有 npm 账号时可以先验证什么
|
|
19
|
+
|
|
20
|
+
打开 GitHub Actions → npm OIDC demo → Run workflow,保持 `dry_run` 勾选:
|
|
21
|
+
|
|
22
|
+
1. GitHub 实际签发一次 audience 为 `npm:registry.npmjs.org` 的 OIDC 身份凭证。
|
|
23
|
+
2. 脚本读取并核对凭证中仓库、分支和工作流字段,不在日志或制品中保留完整 token,也不自行校验 JWT 签名。
|
|
24
|
+
3. 构建 `.tgz` 安装包,并在全新的目录中安装和调用它。
|
|
25
|
+
4. 将安装包保存为 GitHub Actions 制品,保留 1 天。
|
|
26
|
+
5. 跳过 `npm publish`。
|
|
27
|
+
|
|
28
|
+
这能够验证 GitHub 一侧的身份凭证和打包安装流程,**不能证明 npm 已信任此工作流或 npm 实际发布已经成功**。后两项仍需要 npm 包管理员完成 Trusted Publisher 授权。
|
|
29
|
+
|
|
30
|
+
发布工作流没有配置 `NPM_TOKEN` 或 `NODE_AUTH_TOKEN` 密钥,发布权限来自 npm 中预先配置的 Trusted Publisher。
|
|
31
|
+
|
|
32
|
+
## 首次跑通的顺序
|
|
33
|
+
|
|
34
|
+
1. 使用个人 npm 账号 `lewisliu0717`,测试包名称为 `@lewisliu0717/npm-oidc-demo`,可见性为公开。
|
|
35
|
+
2. 创建 GitHub 仓库并推送代码,确认 `check` 成功。推送触发的任务会跳过 `publish`,这是正常情况。
|
|
36
|
+
3. 对全新的测试包,先由 npm 账号持有人登录并发布 `0.0.1`,创建 npm 包。
|
|
37
|
+
4. 在该 npm 包的 Settings → Trusted publishing 中添加 GitHub Actions:
|
|
38
|
+
- Organization or user:`LJHAAAAA`
|
|
39
|
+
- Repository:`npm-oidc-demo`
|
|
40
|
+
- Workflow filename:`publish.yml`
|
|
41
|
+
- Environment name:留空,本案例没有配置 GitHub Environment。
|
|
42
|
+
- Allowed actions:允许直接执行 `npm publish`。
|
|
43
|
+
5. 把版本号更新为 `0.0.2`,推送到 `main`。
|
|
44
|
+
6. 打开 GitHub Actions → npm OIDC demo → Run workflow,取消勾选 `dry_run`,执行真正的 npm 发布。
|
|
45
|
+
7. 确认 Actions 发布成功,并从 npm 安装 `0.0.2` 验证。
|
|
46
|
+
|
|
47
|
+
新建 Trusted Publisher 后,需在 2 天内完成首次成功发布以验证配置;验证成功后无须按 90 天定期更换发布 token。
|
|
48
|
+
|
|
49
|
+
本例没有私有依赖,因此 CI 安装依赖也不需要只读 token。公司的项目如果安装私有依赖,可以给安装步骤单独配置只读 token。
|
|
50
|
+
|
|
51
|
+
## 本地检查
|
|
52
|
+
|
|
53
|
+
```sh
|
|
54
|
+
npm ci --ignore-scripts
|
|
55
|
+
npm run check
|
|
56
|
+
npm pack --dry-run
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
本例发布的代码只有 `index.js` 中的 `greet` 函数:
|
|
60
|
+
|
|
61
|
+
```js
|
|
62
|
+
import { greet } from '@lewisliu0717/npm-oidc-demo';
|
|
63
|
+
|
|
64
|
+
console.log(greet('npm OIDC'));
|
|
65
|
+
```
|
package/index.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lewisliu0717/npm-oidc-demo",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "A minimal GitHub Actions and npm OIDC publishing demo",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": "./index.js",
|
|
7
|
+
"files": [
|
|
8
|
+
"index.js",
|
|
9
|
+
"README.md"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"check": "node --check index.js"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/LJHAAAAA/npm-oidc-demo.git"
|
|
17
|
+
},
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"registry": "https://registry.npmjs.org/",
|
|
21
|
+
"access": "public",
|
|
22
|
+
"provenance": false
|
|
23
|
+
}
|
|
24
|
+
}
|