10 个你该了解的 GitHub Actions 进阶技巧

系统
在执行 workflow 时, 允许在 GitHub Actions 页面输入参数,控制执行逻辑。我们可以将人工处理的逻辑,在 GitHub Actions 参数化执行,适用于持续部署场景。

[[379607]]

 本文转载自微信公众号「问其」,作者陈少文。转载本文请联系问其公众号。

1. workflow 执行时,传入参数

在执行 workflow 时, 允许在 GitHub Actions 页面输入参数,控制执行逻辑。我们可以将人工处理的逻辑,在 GitHub Actions 参数化执行,适用于持续部署场景。

  1. on:  
  2.   workflow_dispatch: 
  3.     inputs: 
  4.       logLevel: 
  5.         description: 'Log level'      
  6.         required: true 
  7.         default'warning' 
  8.       tags: 
  9.         description: 'Test scenario tags'   
  10. jobs: 
  11.   printInputs: 
  12.     runs-on: ubuntu-latest 
  13.     steps: 
  14.     - run: | 
  15.         echo "Log level: ${{ github.event.inputs.logLevel }}" 
  16.         echo "Tags: ${{ github.event.inputs.tags }}"  

上面的 workflow 执行时,会弹出如下对话框。

2. Job 编排控制执行顺序

一个 workflow 由很多个 job 组成,借助于 needs 参数,我们可以管理这些 job 之间的依赖,控制其执行流程。

  1. on: push 
  2. jobs: 
  3.   job1: 
  4.     runs-on: ubuntu-latest 
  5.     steps: 
  6.       - run: echo "job1" 
  7.   job2: 
  8.     runs-on: ubuntu-latest 
  9.     steps: 
  10.       - run: sleep 5 
  11.     needs: job1 
  12.   job3: 
  13.     runs-on: ubuntu-latest 
  14.     steps: 
  15.       - run: sleep 10 
  16.     needs: job1 
  17.   job4: 
  18.     runs-on: ubuntu-latest 
  19.     steps: 
  20.       - run: echo "job4" 
  21.     needs: [job2, job3] 

上面的 workflows 执行时,job2 和 job3 会等 job1 执行成功时才执行,job4 会等 job2 和 job3 执行成功时才执行。

3. 用于项目管理

Kubernetes 基于 ChatOps 使用 Prow 协调社区有序协作。但并不是每个团队,都愿意搭建并维护一套 Prow 机器人系统。ChatOps 实现的核心是事件驱动,这在 GitHub 中使用 Actions 也能实现。

下面是几个项目管理相关的 action

  • 根据修改的目录添加标签
  1. - uses: actions/labeler@main 
  2.   with
  3.     repo-token: "${{ secrets.GITHUB_TOKEN }}" 

在配置文件 .github/workflows/labeler.yml 中添加规则,给对 docs 目录进行修改的 Pull Requests(以下简称 PR) 自动添加 docs_label 标签:

  1. docs_label: 
  2.   - ./docs/* 
  • 根据标签添加 Issues 到 Projects

使用 srggrs/assign-one-project-github-action , 我们可以将新增的 Issues 或者 PR 添加到指定的 Projects 中。

  1. name: Assign NEW issues and NEW pull requests to project 2 
  2.   uses: srggrs/assign-one-project-github-action@1.2.0 
  3.   if: github.event.action == 'opened' 
  4.   with
  5.     project: 'https://github.com/srggrs/assign-one-project-github-action/projects/2' 

也可以将包含指定标签的 Issues 或 PR 添加到指定 Project 的指定 Column 中。

  1. name: Assign issues and pull requests with `bug` label to project 3 
  2.   uses: srggrs/assign-one-project-github-action@1.2.0 
  3.   if: | 
  4.     contains(github.event.issue.labels.*.name'bug') || 
  5.     contains(github.event.pull_request.labels.*.name'bug'
  6.   with
  7.     project: 'https://github.com/srggrs/assign-one-project-github-action/projects/3' 
  8.     column_name: 'Labeled' 
  • 清理长时间无人跟进的 Issues

如果一个 Issue 长达 30 天没有更新,那么下面的 workflow 将会再等 5 天,然后将其关闭。

  1. name'Close stale issues and PRs' 
  2. on
  3.   schedule: 
  4.     - cron: '30 1 * * *' 
  5. jobs: 
  6.   stale: 
  7.     runs-on: ubuntu-latest 
  8.     steps: 
  9.       - uses: actions/stale@v3 
  10.         with
  11.           stale-issue-message: 'This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.' 
  12.           days-before-stale: 30 
  13.           days-before-close: 5 

GitHub 上的项目管理,主要是围绕 Issues、Projects、Labels、Pull Requests 展开,可以在 GitHub Actions 的 Marketplace 中搜索相关的 Action 使用。

4. 在线调试

在使用 GitHub Actions 的过程中,如果需要登录到 Runner 上调试命令,那么下面这个技巧你一定会感兴趣。

  1. - uses: shaowenchen/debugger-action@v2 
  2.   name: debugger 
  3.   timeout-minutes: 30 
  4.   continue-on-error: true 
  5.   with
  6.     ngrok_token: ${{ secrets.NGROK_TOKEN }} 

只需要去 Ngrok 官网申请一个 token,就可以通过 ssh 远程登录到 Runner。当然,也可以暴露 Runner 上的服务,提供外网访问的链接,最长可达 6 小时。

在执行日志中,我们可以找到 ssh 的登录链接,使用 root/root 即可登录 Runner。如果配置了 web 的端口映射,还可以查看到相关的服务链接。

5. 设置缓存

缓存能有效地加快构建速度,减少网络请求,复用中间码。这对于 Java、Nodejs、Python 等项目,非常有用。

  1. name: Get yarn cache directory path 
  2.   id: yarn-cache-dir-path 
  3.   run: echo "::set-output name=dir::$(yarn cache dir)" 
  4. - uses: actions/cache@v2 
  5.   id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`) 
  6.   with
  7.     path: ${{ steps.yarn-cache-dir-path.outputs.dir }} 
  8.     key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} 
  9.     restore-keys: | 
  10.       ${{ runner.os }}-yarn- 

6. 检测项目中的问题链接

项目维护时间长了之后,最令人头疼的就是文档。研发、测试跟进的是代码、功能,而文档却时常无人更新。缺少维护的文档,会让潜在参与者流失。下面这个 Action 能检测文档中的 Broken 链接。

  1. nameCheck Markdown links 
  2. on: push 
  3. jobs: 
  4.   markdown-link-check
  5.     runs-on: ubuntu-latest 
  6.     steps: 
  7.     - uses: actions/checkout@master 
  8.     - uses: gaurav-nelson/github-action-markdown-link-check@v1 
  9.       with
  10.         use-quiet-mode: 'yes' 
  11.         config-file: '.github/workflows/checklink_config.json' 
  12.         max-depth: 3 

gaurav-nelson/github-action-markdown-link-check 支持自定义配置,非常灵活易用,堪称必备 Action。

下面是一个 .github/workflows/checklink_config.json 的示例:

  1.   "replacementPatterns": [ 
  2.     { 
  3.       "pattern""^/"
  4.       "replacement""/github/workspace/" 
  5.     } 
  6.   ], 
  7.   "aliveStatusCodes": [ 
  8.     429, 
  9.     200 
  10.   ] 

最后在 GitHub Actions 日志页面,会输出这样的检测结果:

  1. =========================> MARKDOWN LINK CHECK <========================= 
  2. FILE: ./docs/governance.md 
  3. 4 links checked. 
  4. FILE: ./docs/configuration/cri.md 
  5. [✖] https://build.opensuse.org/project/show/devel:kubic:libcontainers:stable 
  6. 7 links checked. 
  7. ERROR: 1 dead links found! 
  8. [✖] https://build.opensuse.org/project/show/devel:kubic:libcontainers:stable → Status: 404 
  9. FILE: ./docs/configuration/kubeedge.md 
  10. 21 links checked. 
  11. ========================================================================= 

7. Job 批量执行,参数排列组合执行任务

数据驱动测试的场景下,可以通过输入的参数控制测试的流程。在 GitHub Actions 中,我们也可以通过参数化的方式,批量地执行或编排流程。

GitHub Actions 会将 matrix 中的每个参数排列组合,产生一个新的运行实例。

  1. on: push 
  2. jobs: 
  3.   node: 
  4.     runs-on: ${{ matrix.os }} 
  5.     strategy: 
  6.       matrix: 
  7.         os: [ubuntu-16.04, ubuntu-18.04] 
  8.         node: [6, 8, 10] 
  9.     steps: 
  10.       - uses: actions/setup-node@v1 
  11.         with
  12.           node-version: ${{ matrix.node }} 
  13.       - run: node --version 

上面的 workflow 执行时, 会执行 6 个 job。

无论是用来测试兼容性, 还是批量执行 Job, 都是非常好的。

8. 拷贝 Action 的 Badge 状态显示在文档中

通常,我们使用 GitHub Actions 对项目进行代码分析、执行测试、编译、打包、构建、推送镜像等。这些行为对于保证项目的稳定,至关重要。

但并不是每个人都会关注 Actions 的执行细节。我们可以在显眼的地方,给出这些过程的最终实时状态,以提醒用户和开发者。如果 main 分支构建失败了,能提醒用户谨慎使用,能提醒研发尽快修复问题。

在 GitHub Actions 页面中, 点击 Create status badge。

将弹框中的 URL 链接,增加在 Readme 文档中,即可实时快速地查看到 workflow 的执行结果。

9. 精准 hook GitHub 上的行为

workflow 通过 on 关键字定义触发条件。主要有三类触发事件:

  • 人工触发
  1. on: workflow_dispatch 
  • 定时触发

每隔 15 分钟触发一次 workflows。

  1. on
  2.   schedule: 
  3.     - cron:  '*/15 * * * *' 
  • Webhook 触发

我们在 GitHub 上的操作,比如创建 Issues、新增 Deployment 等,都能够通过 API 获取到相关的事件。通过这些事件,我们可以精准地定制 workflow 的行为。通常我们都是基于 push 或者 pull requests 触发,下面列举几个不常见的示例:

当有人 fork 仓库时触发

  1. on
  2.   fork 

当有人 star 仓库时触发

  1. on
  2.   watch: 
  3.     types: [started] 

当有新建的 Issue 时触发

  1. on
  2.   issues: 
  3.     types: [opened] 

10. 开发一个 Action 很简单

如果在 Marketplace 找不到合适的 Action,那么自己开发 Action 也是一个不错的选择。

其实,开发一个 Action 没有想象中那么难。一个 Action 就是一个处理逻辑,接收输入参数,执行一定的逻辑,然后输出参数。有三种类型的 Action:

  • Docker container, 适用 Linux 系统

通过 Docker 容器,提供 Action 的执行逻辑处理。比如下面这个例子:

Dockerfile

  1. FROM appleboy/drone-scp:1.6.2-linux-amd64 
  2. ADD entrypoint.sh /entrypoint.sh 
  3. RUN chmod +x /entrypoint.sh 
  4. ENTRYPOINT ["/entrypoint.sh"

entrypoint.sh

  1. #!/bin/sh 
  2. set -eu 
  3. [ -n "$INPUT_STRIP_COMPONENTS" ] && export INPUT_STRIP_COMPONENTS=$((INPUT_STRIP_COMPONENTS + 0)) 
  4. sh -c "/bin/drone-scp $*" 

通过 dron-scp 镜像,快速开发了一个提供 scp 文件拷贝的 Action。

  • JavaScript, 适用 Linux、macOS、Windows 系统

通过执行 JavaScript 处理 Action 逻辑。官方提供了 JavaScript 和 TypeScript 的 Action 模板。在创建项目时,使用模板创建,然后编写处理逻辑,发布自己的 Action 即可。

GitHub Actions 提供了工具包,以支持这种方式的扩展,例如执行命令、操作 GitHub 等,都可以通过引用包,直接调用相关函数实现。下面是其中几个工具包:

  1. @actions/exec, 执行命令 
  2. @actions/core, 输入、输出、日志、秘钥相关 
  3. @actions/io, 操作文件 
  • Composite run steps, 适用 Linux, macOS, Windows 系统

这种类型,允许将一连串的 Shell 操作作为一个 Action 使用。

  1. name'Hello World' 
  2. description: 'Greet someone' 
  3. inputs: 
  4.   who-to-greet:  # id of input 
  5.     description: 'Who to greet' 
  6.     required: true 
  7.     default'World' 
  8. outputs: 
  9.   random-number: 
  10.     description: "Random number" 
  11.     value: ${{ steps.random-number-generator.outputs.random-id }} 
  12. runs: 
  13.   using: "composite" 
  14.   steps: 
  15.     - run: echo Hello ${{ inputs.who-to-greet }}. 
  16.       shell: bash 
  17.     - id: random-number-generator 
  18.       run: echo "::set-output name=random-id::$(echo $RANDOM)" 
  19.       shell: bash 
  20.     - run: ${{ github.action_path }}/goodbye.sh 
  21.       shell: bash 

11. 参考

?https://github.com/actions/typescript-action

?https://github.com/shaowenchen/debugger-action

 

责任编辑:武晓燕 来源: 问其
相关推荐

2021-01-18 18:30:49

服务器开发工具

2021-01-19 05:26:22

Github ActiJenkinsDevOps

2020-06-04 15:55:54

GitHub代码开发者

2021-01-05 05:15:02

Github 前端仓库

2022-05-27 08:55:15

工具自动化软件

2017-01-16 15:12:36

Linuxwatch命令命令

2023-09-05 08:00:00

开源GreptimeDB

2022-12-21 08:20:01

2015-10-20 10:10:51

隐藏功能Windows 10微软

2014-03-04 09:35:45

JavaScript调试

2020-05-26 08:38:57

JavaScript语言

2020-10-29 10:26:28

DevOps软件自动化

2020-11-29 17:32:01

EmacsLinux

2015-03-19 11:15:16

云备份云存储

2017-01-09 16:40:07

React NatiAndroid 开发

2017-05-18 09:16:54

前端CSS技巧

2020-04-08 17:10:03

GitHub代码开源

2009-04-14 21:38:05

LinuxUbuntu技巧

2021-01-04 08:37:53

动态规划DP

2021-05-13 21:21:50

React应用GitHub
点赞
收藏

51CTO技术栈公众号