Github Actions 的使用方法,构建属于你的 CI

发布于 2021-03-11 17:07:02

什么是 CI (CONTINUOUS INTEGRATION)

在持续集成环境中,开发人员将会频繁的提交代码到主干。这些新提交在最终合并到主线之前,都需要通过编译和自动化测试流进行验证。这样做是基于之前

持续集成过程中很重视自动化测试验证结果,以保障所有的提交在合并主线之后的质量问题,对可能出现的一些问题进行预警

GitHub Actions

GitHub Actions makes it easy to automate all your software workflows, now with world-class CI/CD. Build, test, and deploy your code right from GitHub. Make code reviews, branch management, and issue triaging work the way you want.

使用方法

创建工作流程

  • 从 GitHub 上的仓库,在 .github/workflow 目录中创建一个名为 action.yml 的新文件。 更多信息请参阅“创建新文件”。
  • 将以下 YAML 内容复制到 action.yml 文件中。 注: 如果默认分支不是 main,请更新 DEFAULT_BRANCH 的值以匹配仓库的默认分支名称。目前 Github 新建的仓库主分支默认为 main。
  name: action-test
  
  # Run this workflow every time a new commit pushed to your repository
  on: push
  
  jobs:
    # Set the job key. The key is displayed as the job name
    # when a job name is not provided
    action-test:
      # Name the Job
      name: action test
      # Set the type of machine to run on
      runs-on: ubuntu-latest
  
      steps:
        # Checks out a copy of your repository on the ubuntu-latest machine
        - name: Checkout code
          uses: actions/checkout@v2
        # Runs the test
        - name: Run test 
          shell: bash
          run: echo "hello, world"
  • 在仓库中提交工作流程文件会触发 push 事件并运行工作流程。

查看工作流程

  • 在 GitHub 上,导航到仓库的主页面。
  • 在仓库名称下,单击 Actions(操作)

image.png

  • 在左侧边栏中,单击您想要查看的工作流程。

image.png

  • 从工作流程运行列表中,单击要查看的运行的名称。

image.png

  • 在左侧边栏中,单击 action-test作业。

image.png

  • 查看 CI 构建流程以及日志。

image.png

高阶用法

定时

可以使用 POSIX cron 语法安排工作流程在特定的 UTC 时间运行。

# Runs at 16:00 UTC on the 1st of every month
schedule:
  - cron:  '0 16 1 * *'

自动触发 CI 构建:

image.png

env

设置环境变量

env:
  SERVER: production
  • 在工作流程中使用 env, 此时设置的环境变量只能作用于当前作业步骤:
  steps:
    - name: Hello world
      run: echo Hello world $FIRST_NAME $middle_name $Last_Name!
      env:
        FIRST_NAME: Mona
        middle_name: The
        Last_Name: Octocat
  • 也可以使用 GITHUB_ENV, 此时设置的环境变量可用于此步骤和以下的所有作业的步骤:
  steps:
    - name: Hello world1
      run: echo  "TEST_ENV=true" >> $GITHUB_ENV
    - name: Hello world2
      run: echo $TEST_ENV

构建矩阵

使用多个 Python

strategy:
  matrix:
    python: [3.5, 3.6, 3.7, 3.8]
steps:
  - uses: actions/setup-node@v1
    with:
      python-version: ${{ matrix.python }}

使用多个操作系统

runs-on: ${{ matrix.os }}
strategy:
  matrix:
    os: [ubuntu-16.04, ubuntu-18.04]
    python: [3.5, 3.6, 3.7, 3.8]
steps:
  - uses: actions/setup-node@v1
    with:
      python-version: ${{ matrix.python }}

在矩阵中使用环境变量

在 RT-Thread 的 CI 中,就是用了这种方法,来编译仓库中的 bsp:

image.png

Example: RT-Thread action

    strategy:
    # 设置为 true 时,如果任何 matrix 作业失败,GitHub 将取消所有进行中的作业。 默认值:true
      fail-fast: false
      matrix:
       legs:
         - {RTT_BSP: "CME_M7", RTT_TOOL_CHAIN: "sourcery-arm"}
         - {RTT_BSP: "apollo2", RTT_TOOL_CHAIN: "sourcery-arm"}
         - {RTT_BSP: "asm9260t", RTT_TOOL_CHAIN: "sourcery-arm"}
         - {RTT_BSP: "at91sam9260", RTT_TOOL_CHAIN: "sourcery-arm"} 
         - {RTT_BSP: "allwinner_tina", RTT_TOOL_CHAIN: "sourcery-arm"} 
         - {RTT_BSP: "efm32", RTT_TOOL_CHAIN: "sourcery-arm"} 
         - {RTT_BSP: "gd32e230k-start", RTT_TOOL_CHAIN: "sourcery-arm"} 
         - {RTT_BSP: "gd32303e-eval", RTT_TOOL_CHAIN: "sourcery-arm"}         
         - {RTT_BSP: "gd32450z-eval", RTT_TOOL_CHAIN: "sourcery-arm"}
         - {RTT_BSP: "gkipc", RTT_TOOL_CHAIN: "sourcery-arm"}
         - {RTT_BSP: "imx6sx/cortex-a9", RTT_TOOL_CHAIN: "sourcery-arm"} 
         - {RTT_BSP: "imxrt/imxrt1052-atk-commander", RTT_TOOL_CHAIN: "sourcery-arm"}  
         - {RTT_BSP: "imxrt/imxrt1052-fire-pro", RTT_TOOL_CHAIN: "sourcery-arm"}
         - {RTT_BSP: "imxrt/imxrt1052-nxp-evk", RTT_TOOL_CHAIN: "sourcery-arm"}
         - {RTT_BSP: "lm3s8962", RTT_TOOL_CHAIN: "sourcery-arm"}
         - {RTT_BSP: "lm3s9b9x", RTT_TOOL_CHAIN: "sourcery-arm"}
    steps:
      - uses: actions/checkout@v2
      - name: Set up Python
        uses: actions/setup-python@master
        with:
          python-version: 3.8       

IF 语法

Example: RT-ThreadStudio action

可以使用 if 条件阻止作业在条件得到满足之前运行。 可以使用任何支持上下文和表达式来创建条件

在 RT-Thread 仓库 CI 文件中,根据矩阵中设置的环境变量,来判断当前 BSP 的架构,拉取对应的工具链:

image.png

主分支触发

检查触发工作流程的分支本仓库并且上一个步骤成功时才会执行,否则跳过该步骤:

- name: Generate-Import-Compile
  shell: bash
  if: ${{ github.ref != 'refs/heads/master' && success() }}
  run: |
    cd ${{ github.workspace }}

合并时触发

当本仓库触发 merge_request 动作时, CI 就会被激活:

if: github.event.pull_request.merged

Marketplace

你可以在 Github 的 Marketplace 发现很多 Actions, 将这些已经写好的 action 添加到你的 CI 文件中,会大大减少写 CI 的工作量。

Checkout V2

拉取当前仓库代码。

链接

This action checks-out your repository under $GITHUB_WORKSPACE, so your workflow can access it.
- uses: actions/checkout@v2

setup-python V2

设置 Python 环境。

链接

This action sets up a Python environment for
- name: Set up Python
  uses: actions/setup-python@master
  with:
    python-version: 3.8

Upload-Artifact v2

上传产物。

链接

This uploads artifact from your workflow allowing you to share data between jobs and store data once a workflow is complete.
- name: Generate txt
  run: echo "hellowold" > test.txt
- name: Upload Results
  if: ${{ github.ref != 'refs/heads/master' && success() }}
  uses: actions/upload-artifact@v2
  continue-on-error: True
  with:
    name: check-report
    path: ${{ github.workspace }}/test.txt

file-existence-action

判断文件是否存在。

链接

This is a GitHub Action to check for the existence of files. It can be used for conditionally running workflow steps based on file(s) existence.
name: "File existence check"

on: [push, pull_request]

jobs:
  file_existence:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v1

      - name: Check file existence
        id: check_files
        uses: andstor/file-existence-action@v1
        with:
          files: "package.json, LICENSE, README.md"

      - name: File exists
        if: steps.check_files.outputs.files_exists == 'true'
        # Only runs if all of the files exists
        run: echo All files exists!

Close Stale Issues and PRs

自动关闭陈旧的 Issues 和 PR。

链接

Warns and then closes issues and PRs that have had no activity for a specified amount of time.
name: "Close stale issues"
on:
  schedule:
  - cron: "30 1 * * *"

jobs:
  stale:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/stale@v3
      with:
        repo-token: ${{ secrets.GITHUB_TOKEN }}
        stale-issue-message: 'Message to comment on stale issues. If none provided, will not mark issues stale'
        stale-pr-message: 'Message to comment on stale PRs. If none provided, will not mark PRs stale'
1 条评论

发布
问题