惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
A
About on SuperTechFans
The Cloudflare Blog
G
Google Developers Blog
博客园_首页
Martin Fowler
Martin Fowler
Apple Machine Learning Research
Apple Machine Learning Research
L
LangChain Blog
D
Docker
C
Check Point Blog
T
Tailwind CSS Blog
博客园 - 司徒正美
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
Microsoft Security Blog
Microsoft Security Blog
V
V2EX
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss
酷 壳 – CoolShell
酷 壳 – CoolShell
IT之家
IT之家
M
MIT News - Artificial intelligence
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 【当耐特】
GbyAI
GbyAI

陈少文的网站

巨变与机遇的未来十年 Kubernetes 平台管理软件压力测试方案 使用镜像部署 Hexo 静态页面 终于等到你 - GitHub 镜像仓库服务(ghcr.io) 一起来学 Go --(6)Interface 一起来学 Go --(5)Goroutine 和 Channel 什么是函数式编程 如何在 Kubernetes 集群集成 Kata 柯里化与偏函数 使用 PyGithub 自动创建 Label 软件产品是团队能力的输出 Helm 2 、Helm 3 比较 IoT 变现 Kubernetes 中的 DNS 服务 国内的 Helm 镜像源 Harbor 使用自签证书支持 Https 访问 DevOps 工具链之 Prow 如何使用 kfctl 安装 Kubeflow VS Code 无法下载 Go 插件的工具包 工程师更应具有服务精神 你不知道的 Docker 使用技巧 使用 Docker 运行 Tensorflow 论中国 什么是左移 如何清空 Git 仓库全部历史记录 一禅小和尚 有风吹过厨房 时间的玫瑰 如何在 CentOS 安装 GPU 驱动 开发 Tips(19)
如何远程触发 GitHub Action
微信公众号 · 2021-08-25 · via 陈少文的网站

通常,我们需要在 GitHub 上进行一些操作,才能触发 GitHub Action。本篇将介绍一种通过 API 远程调用触发 GitHub Action 的方法。

1. 常见的几种触发 GitHub Action 的方式

下面是一个 GitHub Action 的示例:

1
2
3
4
5
6
7
name: GitHub Actions Demo
on: [push, pull_request]
jobs:
  Explore-GitHub-Actions:
    runs-on: ubuntu-latest
    steps:
      - run: echo "Hello World!"

在 on 关键字下,定义的就是触发 Workflow 执行的事件。下面常用的几种 GitHub Action 事件:

  • workflow_dispatch, 手动触发

在 inputs 中可以添加交互参数(可选)。

1
2
3
4
5
6
7
on:
  workflow_dispatch:
    inputs:
      name:
        description: "Person to greet"
        required: true
        default: "Mona the Octocat"
  • push, 推送代码
  • issues, issues 生命周期
1
2
3
on:
  issues:
    types: [opened, edited, milestoned]
  • issue_comment, issue 评论
1
2
3
on:
  issue_comment:
    types: [created, deleted]
  • project, 项目管理生命周期
1
2
3
on:
  project:
    types: [created, deleted]
  • pull_request, pr 生命周期
1
2
3
on:
  pull_request:
    types: [assigned, opened, synchronize, reopened]

利用这些事件 hook,可以自动化很多流程。

2. 使用 API 远程触发 GitHub Action

2.1 创建一个 Token

访问链接页面 https://github.com/settings/tokens/new 申请一个 Token。

需要勾选 repo 权限。

2.2 添加

在仓库 https://github.com/shaowenchen/wait-webhook-to-run 下,新建一个文件 .github/workflows/worker.yml。内容如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
on:
  repository_dispatch:
    types:
      - webhook-1
      - webhook-2

jobs:
  run:
    runs-on: ubuntu-latest

    steps:
    - name: Hello World
      run: |
        echo Hello World!

repository_dispatchtypes 中,可以自定义事件类型。

2.3 远程触发 Github Action

下面是 API 调用格式:

1
2
3
4
curl -X POST https://api.github.com/repos/:owner/:repo/dispatches \
    -H "Accept: application/vnd.github.everest-preview+json" \
    -H "Authorization: token TRIGGER_TOKEN" \
    --data '{"event_type": "TRIGGER_EVENT"}'

其中,owner 是用户名,repo 是仓库名, TRIGGER_TOKEN 是上面申请的 Token 凭证,TRIGGER_EVENT 是自定义的事件名。

  • 触发 webhook-1 事件
1
2
3
4
curl -X POST https://api.github.com/repos/shaowenchen/wait-webhook-to-run/dispatches \
    -H "Accept: application/vnd.github.everest-preview+json" \
    -H "Authorization: token ghp_xxxxxxxxxxxxxxxxxxxxxxxxxx" \
    --data '{"event_type": "webhook-1"}'
  • 触发 webhook-2 事件
1
2
3
4
curl -X POST https://api.github.com/repos/shaowenchen/wait-webhook-to-run/dispatches \
    -H "Accept: application/vnd.github.everest-preview+json" \
    -H "Authorization: token ghp_xxxxxxxxxxxxxxxxxxxxxxxxxx" \
    --data '{"event_type": "webhook-2"}'

查看 GitHub Action 执行:

3. 参考