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

Terwer 技术分享评论209字数 1866阅读6分13秒阅读模式

release please​​ 是一个来自于 Google​​ 的自动发版工具,基于 Github Actions​ 可实现全自动发版。文章源自浅海拾贝-https://blog.terwergreen.com/githubactions-uses-reaseplease-to-implement-automatic-version-z1yj1lb.html

官网:https://github.com/googleapis/release-please文章源自浅海拾贝-https://blog.terwergreen.com/githubactions-uses-reaseplease-to-implement-automatic-version-z1yj1lb.html

上手

在项目根目录的 .github​ 的 workflows​ 里面新建一个 release-please.yml​ 文件,下面是一个标准的 node​ 项目的标准配置:文章源自浅海拾贝-https://blog.terwergreen.com/githubactions-uses-reaseplease-to-implement-automatic-version-z1yj1lb.html

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​​ 仓库。文章源自浅海拾贝-https://blog.terwergreen.com/githubactions-uses-reaseplease-to-implement-automatic-version-z1yj1lb.html

注意 1:任务运行完毕后并不是直接就发版了,而是会新建一个 pr,可以检查 pr 内容,需要发版,就合并。如果暂时不发版,可以直接关闭这个 pr。文章源自浅海拾贝-https://blog.terwergreen.com/githubactions-uses-reaseplease-to-implement-automatic-version-z1yj1lb.html

Github-Actions使用release-please实现自动发版文章源自浅海拾贝-https://blog.terwergreen.com/githubactions-uses-reaseplease-to-implement-automatic-version-z1yj1lb.html

注意 2:pr 会自动递增版本号,所以不要提前手动更改版本号。node 项目的版本号是 package.json 里面的 version 字段,格式是:x.x.x 。文章源自浅海拾贝-https://blog.terwergreen.com/githubactions-uses-reaseplease-to-implement-automatic-version-z1yj1lb.html

版本号规则是:文章源自浅海拾贝-https://blog.terwergreen.com/githubactions-uses-reaseplease-to-implement-automatic-version-z1yj1lb.html

feat:->大版本,例如:1.0.0->1.1.0文章源自浅海拾贝-https://blog.terwergreen.com/githubactions-uses-reaseplease-to-implement-automatic-version-z1yj1lb.html

fix:->小版本,例如:1.0.0->1.0.1文章源自浅海拾贝-https://blog.terwergreen.com/githubactions-uses-reaseplease-to-implement-automatic-version-z1yj1lb.html

feat!:, fix!:, refactor!:->主要版本,例如:1.0.0->2.0.0文章源自浅海拾贝-https://blog.terwergreen.com/githubactions-uses-reaseplease-to-implement-automatic-version-z1yj1lb.html

如果从未发过版本,那么初始是 1.0.0​ 。文章源自浅海拾贝-https://blog.terwergreen.com/githubactions-uses-reaseplease-to-implement-automatic-version-z1yj1lb.html

Github-Actions使用release-please实现自动发版文章源自浅海拾贝-https://blog.terwergreen.com/githubactions-uses-reaseplease-to-implement-automatic-version-z1yj1lb.html

文章源自浅海拾贝-https://blog.terwergreen.com/githubactions-uses-reaseplease-to-implement-automatic-version-z1yj1lb.html

另外,如果不需要发布到 npm​ ,可以使用下面的配置:文章源自浅海拾贝-https://blog.terwergreen.com/githubactions-uses-reaseplease-to-implement-automatic-version-z1yj1lb.html

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

文章源自浅海拾贝-https://blog.terwergreen.com/githubactions-uses-reaseplease-to-implement-automatic-version-z1yj1lb.html

注意事项

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

    https://www.npmjs.com/settings/terwer/tokens 申请一个 token​ ,然后再项目里面设置:文章源自浅海拾贝-https://blog.terwergreen.com/githubactions-uses-reaseplease-to-implement-automatic-version-z1yj1lb.html

    Settings -> Secrets and variables -> Actions -> New repository secret​ ,新建一个即可。文章源自浅海拾贝-https://blog.terwergreen.com/githubactions-uses-reaseplease-to-implement-automatic-version-z1yj1lb.html

  • Github​ 仓库权限设置文章源自浅海拾贝-https://blog.terwergreen.com/githubactions-uses-reaseplease-to-implement-automatic-version-z1yj1lb.html

    注意:默认的 Github 仓库不允许拉取,需要开启权限才行。方法如下:文章源自浅海拾贝-https://blog.terwergreen.com/githubactions-uses-reaseplease-to-implement-automatic-version-z1yj1lb.html

    转到 https://github.com/OWNER/REPO/settings/actions​ 页面向下划到 Workflow Permissions 然后切换到 Read and Write permissions 。文章源自浅海拾贝-https://blog.terwergreen.com/githubactions-uses-reaseplease-to-implement-automatic-version-z1yj1lb.html

    Github-Actions使用release-please实现自动发版文章源自浅海拾贝-https://blog.terwergreen.com/githubactions-uses-reaseplease-to-implement-automatic-version-z1yj1lb.html

文章更新历史
2023-03-06 feat:初稿 文章源自浅海拾贝-https://blog.terwergreen.com/githubactions-uses-reaseplease-to-implement-automatic-version-z1yj1lb.html

文章源自浅海拾贝-https://blog.terwergreen.com/githubactions-uses-reaseplease-to-implement-automatic-version-z1yj1lb.html

相关文章
  • 扫码加我微信
  • 验证消息请输入:来自你的博客
  • weinxin
  • 我的微信公众号
  • 微信扫一扫与我交流吧
  • weinxin
Terwer
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: