Github Actions 自动发布

172 阅读2分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第1天,点击查看活动详情

1. GitHub Actions

1.1 简介

GitHub Actions is a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your build, test, and deployment pipeline.

一个 CI/CD 工具,webhooks + server(server like: Jenkins)

1.2 能力

当Github接收到指定事件(如,代码推送),执行设置的任务。

事件包括:手动触发、代码推送、新建tag、PR合并、release创建、仓库被fork、被提issue,甚至设置定时任务

执行的任务包括:在服务器上checkout代码,执行打包、测试、发布npm、运行Github CLI

image-20220419114304460.png

  • Workflows: 一个YAML文件对应一个工作流,一个工作流包含多个job
  • Events: 一个触发工作流运行的事件
  • Actions: 包含多个任务的集合
  • Jobs: 执行在一个runner里一些steps
  • Runners: 一个运行job的虚拟服务器,每个job一个runner,相互隔离,多个jobs数据共享使用artifact

1.3 创建工作流

image-20220328114617477.png

image-20220328114651165.png 发布到npm

name: publish to npm
on:
  release:
    # created:创建一个release、prerelease
    # published:发布一个release、pre-release
    # released: 发布一个release
    types: [released]
  push:
  #  tags:
  #    - v*
  # pull_request:
  #   branches:
  #     - master
  # push:
  #   branches:
  #     - master
  # Allows you to run this workflow manually from the Actions tab (可以手动点)
  workflow_dispatch:

jobs:
  build: # job名称
    runs-on: ubuntu-latest # 指定运行环境
    steps:
      - uses: actions/checkout@v2 # 检出指定分支 根据触发条件
      - uses: actions/setup-node@v2 # 设置node环境
        with:
          node-version: 16 # 指定node版本
          registry-url: https://registry.npmjs.org/
      # 为啥用ci不用i  参看 https://juejin.cn/post/7148390657109786655
      - run: npm ci
      - run: npm run test
      - run: npm run build
      - run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{secrets.npm_token}}

自动打Tag

# This is a basic workflow to help you get started with Actions
name: auto tag

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the main branch
  # push:
  #   branches: [ main ]
  pull_request:
    branches: [ main ]
    types:
      - closed
  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "if_merged"
  if_merged:
    # 只有合并才进入 且 仓库符合判断
    if: github.event.pull_request.merged == true && github.repository == 'Sunxinqiang/npm-test1'
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      # Only a single commit is fetched by default, for the ref/SHA that triggered the workflow.
      - uses: actions/checkout@v3
      - run: git config --global user.email "autotag@xxx.com"
      - run: git config --global user.name "autotag"
      - run: npm version patch

2. 发布NPM

2.1 token设置

image-20220328115142360.png

image-20220328115233418.png 应该选择 Automation !! 因为可以绕过 two-factor authentication 双重认证

image-20220328115322874.png

image-20220328114854667.png

3. GitHub CLI

3.1 简介

GitHub的命令行工具

3.2 能力

完成一些GitHub操作,如创建PR、创建Release、创建Issue、运行工作流等

3.3 使用方法

  • token 创建:scopes至少勾选下图部分,不然登录会报权限不够

image-20220427153944349.png

image-20220427153829068.png

  • 登录:gh auth login --with-token < ../github-token.txt

  • 提pr:gh pr create --base master --title auto --body body

image-20220427154258803.png

参考