Github-Actions使用release-please实现自动发版

release please​​ 是一个来自于 Google​​ 的自动发版工具,基于 Github Actions​ 可实现全自动发版。

官网:https://github.com/googleapis/release-please

上手

在项目根目录的 .github​ 的 workflows​ 里面新建一个 release-please.yml​ 文件,下面是一个标准的 node​ 项目的标准配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
on:
push:
branches:
- main
name: release-please
jobs:
release-please:
runs-on: ubuntu-latest
steps:
- uses: google-github-actions/release-please-action@v3
id: release
with:
release-type: node
package-name: release-please-action
# Checkout
- uses: actions/checkout@v3
if: ${{ steps.release.outputs.release_created }}
# Setup node
- uses: actions/setup-node@v3
with:
node-version: 16
registry-url: 'https://registry.npmjs.org'
if: ${{ steps.release.outputs.release_created }}
# Setup pnpm
- uses: pnpm/action-setup@v2
if: ${{ steps.release.outputs.release_created }}
# Install dependencies
- run: pnpm install
if: ${{ steps.release.outputs.release_created }}
# Build output
- run: pnpm build
if: ${{ steps.release.outputs.release_created }}
# Publish to npm
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
if: ${{ steps.release.outputs.release_created }}

提交之后,正常情况就会在 main​​ 分支的 push​​ 事件触发之时,启动自动发版,包括发布到 npm​​ 仓库。

注意 1:任务运行完毕后并不是直接就发版了,而是会新建一个 pr,可以检查 pr 内容,需要发版,就合并。如果暂时不发版,可以直接关闭这个 pr。

注意 2:pr 会自动递增版本号,所以不要提前手动更改版本号。node 项目的版本号是 package.json 里面的 version 字段,格式是:x.x.x 。

版本号规则是:

feat:->大版本,例如:1.0.0->1.1.0

fix:->小版本,例如:1.0.0->1.0.1

feat!:, fix!:, refactor!:->主要版本,例如:1.0.0->2.0.0

如果从未发过版本,那么初始是 1.0.0​ 。

另外,如果不需要发布到 npm​ ,可以使用下面的配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
on:
push:
branches:
- main
name: release-please
jobs:
release-please:
runs-on: ubuntu-latest
steps:
- uses: google-github-actions/release-please-action@v3
id: release
with:
release-type: node
package-name: release-please-action

注意事项

  • 配置 npm token (可选,如果不需要发布到 npm 可忽略)

    https://www.npmjs.com/settings/terwer/tokens 申请一个 token​ ,然后再项目里面设置:

    Settings -> Secrets and variables -> Actions -> New repository secret​ ,新建一个即可。

  • Github​ 仓库权限设置

    注意:默认的 Github 仓库不允许拉取,需要开启权限才行。方法如下:

    转到 https://github.com/OWNER/REPO/settings/actions​ 页面向下划到 Workflow Permissions 然后切换到 Read and Write permissions 。

文章更新历史
2023-03-06 feat:初稿

作者

Terwer

发布于

2023-03-06

更新于

2023-03-06

许可协议

评论