若是你已经在使用 GitHub Actions ,那么阅读本文你将得到更多有趣而有用的打开方式。阅读完,我又给仓库新增了几个 workflow 。html
在执行 workflow 时, 容许在 GitHub Actions 页面输入参数,控制执行逻辑。咱们能够将人工处理的逻辑,在 GitHub Actions 参数化执行,适用于持续部署场景。node
on:
workflow_dispatch:
inputs:
logLevel:
description: 'Log level'
required: true
default: 'warning'
tags:
description: 'Test scenario tags'
jobs:
printInputs:
runs-on: ubuntu-latest
steps:
- run: |
echo "Log level: ${{ github.event.inputs.logLevel }}"
echo "Tags: ${{ github.event.inputs.tags }}"
复制代码
上面的 workflow 执行时,会弹出以下对话框。linux
一个 workflow 由不少个 job 组成,借助于 needs 参数,咱们能够管理这些 job 之间的依赖,控制其执行流程。git
on: push
jobs:
job1:
runs-on: ubuntu-latest
steps:
- run: echo "job1"
job2:
runs-on: ubuntu-latest
steps:
- run: sleep 5
needs: job1
job3:
runs-on: ubuntu-latest
steps:
- run: sleep 10
needs: job1
job4:
runs-on: ubuntu-latest
steps:
- run: echo "job4"
needs: [job2, job3]
复制代码
上面的 workflows 执行时,job2 和 job3 会等 job1 执行成功时才执行,job4 会等 job2 和 job3 执行成功时才执行。github
Kubernetes 基于 ChatOps 使用 Prow 协调社区有序协做。但并非每一个团队,都愿意搭建并维护一套 Prow 机器人系统。ChatOps 实现的核心是事件驱动,这在 GitHub 中使用 Actions 也能实现。web
下面是几个项目管理相关的 actiondocker
- uses: actions/labeler@main
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
复制代码
在配置文件 .github/workflows/labeler.yml
中添加规则,给对 docs 目录进行修改的 Pull Requests(如下简称 PR) 自动添加 docs_label
标签:typescript
docs_label:
- ./docs/*
复制代码
使用 srggrs/assign-one-project-github-action
, 咱们能够将新增的 Issues 或者 PR 添加到指定的 Projects 中。shell
- name: Assign NEW issues and NEW pull requests to project 2
uses: srggrs/assign-one-project-github-action@1.2.0
if: github.event.action == 'opened'
with:
project: 'https://github.com/srggrs/assign-one-project-github-action/projects/2'
复制代码
也能够将包含指定标签的 Issues 或 PR 添加到指定 Project 的指定 Column 中。json
- name: Assign issues and pull requests with `bug` label to project 3
uses: srggrs/assign-one-project-github-action@1.2.0
if: | contains(github.event.issue.labels.*.name, 'bug') || contains(github.event.pull_request.labels.*.name, 'bug') with:
project: 'https://github.com/srggrs/assign-one-project-github-action/projects/3'
column_name: 'Labeled'
复制代码
若是一个 Issue 长达 30 天没有更新,那么下面的 workflow 将会再等 5 天,而后将其关闭。
name: 'Close stale issues and PRs'
on:
schedule:
- cron: '30 1 * * *'
jobs:
stale:
runs-on: ubuntu-latest
steps:
- uses: actions/stale@v3
with:
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.'
days-before-stale: 30
days-before-close: 5
复制代码
GitHub 上的项目管理,主要是围绕 Issues、Projects、Labels、Pull Requests 展开,能够在 GitHub Actions 的 Marketplace 中搜索相关的 Action 使用。
在使用 GitHub Actions 的过程当中,若是须要登陆到 Runner 上调试命令,那么下面这个技巧你必定会感兴趣。
- uses: shaowenchen/debugger-action@v2
name: debugger
timeout-minutes: 30
continue-on-error: true
with:
ngrok_token: ${{ secrets.NGROK_TOKEN }}
复制代码
只须要去 Ngrok 官网申请一个 token,就能够经过 ssh 远程登陆到 Runner。固然,也能够暴露 Runner 上的服务,提供外网访问的连接,最长可达 6 小时。
在执行日志中,咱们能够找到 ssh 的登陆连接,使用 root/root 便可登陆 Runner。若是配置了 web 的端口映射,还能够查看到相关的服务连接。
缓存能有效地加快构建速度,减小网络请求,复用中间码。这对于 Java、Nodejs、Python 等项目,很是有用。
- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
- uses: actions/cache@v2
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: | ${{ runner.os }}-yarn- 复制代码
项目维护时间长了以后,最使人头疼的就是文档。研发、测试跟进的是代码、功能,而文档却时常无人更新。缺乏维护的文档,会让潜在参与者流失。下面这个 Action 能检测文档中的 Broken 连接。
name: Check Markdown links
on: push
jobs:
markdown-link-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: gaurav-nelson/github-action-markdown-link-check@v1
with:
use-quiet-mode: 'yes'
config-file: '.github/workflows/checklink_config.json'
max-depth: 3
复制代码
gaurav-nelson/github-action-markdown-link-check
支持自定义配置,很是灵活易用,堪称必备 Action。
下面是一个 .github/workflows/checklink_config.json
的示例:
{
"replacementPatterns": [
{
"pattern": "^/",
"replacement": "/github/workspace/"
}
],
"aliveStatusCodes": [
429,
200
]
}
复制代码
最后在 GitHub Actions 日志页面,会输出这样的检测结果:
=========================> MARKDOWN LINK CHECK <=========================
FILE: ./docs/governance.md
4 links checked.
FILE: ./docs/configuration/cri.md
[✖] https://build.opensuse.org/project/show/devel:kubic:libcontainers:stable
7 links checked.
ERROR: 1 dead links found!
[✖] https://build.opensuse.org/project/show/devel:kubic:libcontainers:stable → Status: 404
FILE: ./docs/configuration/kubeedge.md
21 links checked.
=========================================================================
复制代码
数据驱动测试的场景下,能够经过输入的参数控制测试的流程。在 GitHub Actions 中,咱们也能够经过参数化的方式,批量地执行或编排流程。
GitHub Actions 会将 matrix 中的每一个参数排列组合,产生一个新的运行实例。
on: push
jobs:
node:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-16.04, ubuntu-18.04]
node: [6, 8, 10]
steps:
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node }}
- run: node --version
复制代码
上面的 workflow 执行时, 会执行 6 个 job。
不管是用来测试兼容性, 仍是批量执行 Job, 都是很是好的。
一般,咱们使用 GitHub Actions 对项目进行代码分析、执行测试、编译、打包、构建、推送镜像等。这些行为对于保证项目的稳定,相当重要。
但并非每一个人都会关注 Actions 的执行细节。咱们能够在显眼的地方,给出这些过程的最终实时状态,以提醒用户和开发者。若是 main 分支构建失败了,能提醒用户谨慎使用,能提醒研发尽快修复问题。
在 GitHub Actions 页面中, 点击 Create status badge
。
将弹框中的 URL 连接,增长在 Readme 文档中,便可实时快速地查看到 workflow 的执行结果。
workflow 经过 on 关键字定义触发条件。 主要有三类触发事件:
on: workflow_dispatch
复制代码
每隔 15 分钟触发一次 workflows。
on:
schedule:
- cron: '*/15 * * * *'
复制代码
咱们在 GitHub 上的操做,好比建立 Issues、新增 Deployment 等,都可以经过 API 获取到相关的事件。经过这些事件,咱们能够精准地定制 workflow 的行为。一般咱们都是基于 push 或者 pull requests 触发,下面列举几个不常见的示例:
当有人 fork 仓库时触发
on:
fork
复制代码
当有人 star 仓库时触发
on:
watch:
types: [started]
复制代码
当有新建的 Issue 时触发
on:
issues:
types: [opened]
复制代码
若是在 Marketplace 找不到合适的 Action,那么本身开发 Action 也是一个不错的选择。
其实,开发一个 Action 没有想象中那么难。一个 Action 就是一个处理逻辑,接收输入参数,执行必定的逻辑,而后输出参数。有三种类型的 Action:
经过 Docker 容器,提供 Action 的执行逻辑处理。好比下面这个例子:
Dockerfile
FROM appleboy/drone-scp:1.6.2-linux-amd64
ADD entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
复制代码
entrypoint.sh
#!/bin/sh
set -eu
[ -n "$INPUT_STRIP_COMPONENTS" ] && export INPUT_STRIP_COMPONENTS=$((INPUT_STRIP_COMPONENTS + 0))
sh -c "/bin/drone-scp $*"
复制代码
经过 dron-scp
镜像,快速开发了一个提供 scp 文件拷贝的 Action。
经过执行 JavaScript 处理 Action 逻辑。官方提供了 JavaScript 和 TypeScript 的 Action 模板。在建立项目时,使用模板建立,而后编写处理逻辑,发布本身的 Action 便可。
GitHub Actions 提供了工具包,以支持这种方式的扩展,例如执行命令、操做 GitHub 等,均可以经过引用包,直接调用相关函数实现。下面是其中几个工具包:
@actions/exec, 执行命令
@actions/core, 输入、输出、日志、秘钥相关
@actions/io, 操做文件
复制代码
这种类型,容许将一连串的 Shell 操做做为一个 Action 使用。
name: 'Hello World'
description: 'Greet someone'
inputs:
who-to-greet: # id of input
description: 'Who to greet'
required: true
default: 'World'
outputs:
random-number:
description: "Random number"
value: ${{ steps.random-number-generator.outputs.random-id }}
runs:
using: "composite"
steps:
- run: echo Hello ${{ inputs.who-to-greet }}.
shell: bash
- id: random-number-generator
run: echo "::set-output name=random-id::$(echo $RANDOM)"
shell: bash
- run: ${{ github.action_path }}/goodbye.sh
shell: bash
复制代码
原文连接 www.chenshaowen.com/blog/10-tip…
更多精彩内容请关注公众号。