1
+ name : npm publish # 定义工作流名称
2
+
3
+ on :
4
+ workflow_dispatch : # 允许手动触发工作流,默认是不开启的
5
+ push : # 当有代码推送到仓库时触发
6
+ tags :
7
+ - ' v*.*.*' # 监听所有以 v 开头的标签,比如 v1.0.0
8
+
9
+ jobs :
10
+ build : # 工作流程中的一个作业
11
+ runs-on : ubuntu-latest # 指定运行作业的虚拟环境
12
+ steps :
13
+ - name : Checkout repository # 步骤名称
14
+ uses : actions/checkout@v4 # 使用 GitHub 官方提供的 Checkout 动作,拷贝最新的代码到虚拟机上
15
+ - name : Set up Node.js # 步骤名称
16
+ uses : actions/setup-node@v4 # 使用官方的 actions/setup-node@v4 来设置 Node.js 环境
17
+ with :
18
+ node-version : 20 # 使用 Node.js 20 版本
19
+ - name : enable pnpm # 步骤名称
20
+ run : corepack enable
21
+ - name : install Dependencies # 步骤名称
22
+ run : pnpm i
23
+ - name : Run Test
24
+ run : npm test
25
+ - name : Build Project
26
+ run : pnpm build
27
+ - name : Upload Build Artifacts
28
+ uses : actions/upload-artifact@v4
29
+ with :
30
+ name : build-artifacts # 构建产物名称
31
+ path : ./dist # 构建产物路径
32
+
33
+ publish-npm :
34
+ needs : build # 依赖 build 作业,确保在 build 成功后再执行
35
+ runs-on : ubuntu-latest
36
+ steps :
37
+ - name : Checkout repository
38
+ uses : actions/checkout@v4
39
+ - name : Set up Node.js
40
+ uses : actions/setup-node@v4
41
+ with :
42
+ node-version : 20
43
+ registry-url : https://registry.npmjs.org/
44
+ - name : Download Build Artifacts
45
+ uses : actions/download-artifact@v4
46
+ with :
47
+ name : build-artifacts # 构建产物名称,需与上传时的名称一致
48
+ path : ./dist # 下载后存放路径
49
+ - name : List Build Directory # 列出构建目录中的文件
50
+ run : ls -al ./dist
51
+ - name : List Publish Directory # 列出发布目录中的文件
52
+ run : ls -al ./
53
+ - name : Publish to NPM # 步骤名称
54
+ run : npm publish
55
+ env :
56
+ NODE_AUTH_TOKEN : ${{ secrets.NPM_TOKEN }} # 使用 GitHub Secrets 中配置的 NPM 认证 token
0 commit comments