【github】 github actions 扫盲日记

284 阅读2分钟

Github Actions 是Github的持续集成服务,除此之外还有TravisCICircleCI 、GitLab CI/CD、Jenkins等,本文只记录github actions笔记

有一个官网市场 集合了别人提交的各种Actions

# .github/workflows/publish.yml 这是一个使用pnpm发布一个node项目的实例
name: CI
on:
  push:
    branches:
      - master

jobs:
  release:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [16.x]
    steps:
      - name: Checkout source code
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Install pnpm
        uses: pnpm/action-setup@v2
        with:
          version: 8
          run_install: false

      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
          registry-url: 'https://registry.npmjs.org/'
          cache: 'pnpm'

      - name: Install the dependencies
        run: pnpm i

      - name: git config
        run: |
          git config --global user.name 'xx'
          git config --global user.email 'xxx'

      - name: Run Release
        run: |
          pnpm release
          echo Source branch is ${{ github.head_ref }}
          echo ${{ github.ref }}

        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: End Message
        run: echo 'Success'

现在从一些基本的概念开始

workflow

在你的项目创建.github/workflows/publish.yml, workflow的字段配置非常多

name

workflow的名字,不写就是文件名, 比如上面的name就是CI

on

on:
  push:
    branches:
      - master
    paths-ignore:
      - 'examples/**'
      - 'docs/**'
      - '**/*.md'
  pull_request:
    types:
      - 'opened'
      - 'synchronize'

通常是指触发workflow的条件,比如触发的事件

1、 on

on:push

push的时候触发workflow,也可以是多事件

on: [push, pull_request]

push和pull_request的时候触发workflow

3、 on.<event_name>.types


#  当一个新的pull request被打开,或者pull request关联的分支有新的提交被推上去时触发
on:
  pull_request:
    types:
      - 'opened'
      - 'synchronize'

4、 on.<pull_request|pull_request_target>.<branches|branches-ignore>

# 在push的时候会忽略 examples/xx分支
on:
  push:
    branches:
      - master
    paths-ignore: 
      - 'examples/**'

5、 on.workflow_dispatch

使用workflow_dispatch,选择性地指定传递给工作流的输入

concurrency

用于控制并发运行的工作或工作流

concurrency:
  group: ${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

github.head_refgit_pull_request的分支名,github.run_id是运行的唯一id。上文代表着新的工作或工作流开始运行,并且它们和正在运行的工作或工作流在同一组,那么正在运行的工作或工作流将会被取消,以便新的工作或工作流能够运行

jobs

一个workflow 由多个job构成

1、jobs.<job_id>.name

jobs:
  release:  # job_id
    name: My first job  #job_name

2、jobs.<job_id>.permissions

为job设置GITHUB_TOKEN的使用范围

permissions:
  actions: read|write|none
  checks: read|write|none
  contents: read|write|none
  deployments: read|write|none
  id-token: read|write|none
  issues: read|write|none
  discussions: read|write|none
  packages: read|write|none
  pages: read|write|none
  pull-requests: read|write|none
  repository-projects: read|write|none
  security-events: read|write|none
  statuses: read|write|none

3、jobs.<job_id>.runs-on

定义运行job的类型

  • github hoster runner

image.png

runs-on: [self-hosted, linux]
 runs-on: 
      group: ubuntu-runners

4、jobs.<job_id>.strategy.matrix

jobs:
  example_matrix:
    strategy:
      matrix:
        os: [ubuntu-22.04, ubuntu-20.04]
        version: [10, 12, 14]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.version }}

5、 jobs.<job_id>.needs

创建jobs的依赖关系,比如job1依赖job2

jobs:
  job1:
  job2:
    needs: job1
  job3:
    if: ${{ always() }} # alyway()表达式,无论前面的job是否成功,这里永远执行
    needs: [job1, job2]

更多表达式

6、 jobs.<job_id>.steps

创建完成job的步骤

   steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Install pnpm
        if: ${{ matrix.node-version != '14.x' }}
        uses: pnpm/action-setup@v2.2.4

      - name: Install pnpm (node 14)
        if: ${{ matrix.node-version == '14.x' }}
        run: npm install -g @pnpm/exe@8.6.2

if条件表达式,很容易理解。以上代表着如果node版本是14.x, 用pnpm8.6.2的版本,否则用pnpm/action-setup@v2.2.4 actions

7、 jobs.<job_id>.steps[*].name

image.png

steps:
  - name: Install pnpm
    uses: pnpm/action-setup@v2

name 定义的会展示actions上

steps:
  - uses: actions/checkout@v3
    with:
        fetch-depth: 0

允许workflow在你代码库下执行(比如构建、测试等),with是入参, fetch-depth: 0代表获取所有的历史提交,它还有非常多的参数actions/checkout@v3

变量

1、 env

2、jobs.<job_id>.env

3、jobs.<job_id>.steps[*].env

name: Greeting on variable day

on:
  workflow_dispatch

env:
  DAY_OF_WEEK: Monday

jobs:
  greeting_job:
    runs-on: ubuntu-latest
    env:
      Greeting: Hello
    steps:
      - name: "Say Hello Mona it's Monday"
        run: echo "$Greeting $First_Name. Today is $DAY_OF_WEEK!"
        env:
          First_Name: Mona

4、 如何创建自定义变量NPM_TOKEN

image.png

github actions Context

github

  • github.head_ref

pull_request的源分支,在pull_request会出现

  • github.run_id

git运行的每个工作流的唯一id。如果重新运行工作流运行,id不会更改

  • github.workflow

工作流的名称,如果没有指定,则此属性的值是存git工作流文件的完整路径

  • github.event_name

workflow 运行时触发的事件,比如push

secrets

  • secrtets.GITHUB_TOKEN

runner

  • runner.os

执行job的操作系统,可能取值有  LinuxWindows, 或 macOS.

  • runner.temp

更多可以参考github actions Context

TODO:

假设你有一个开源项目,如何配置issue-open-checked