如何远程触发 GitHub Action

边缘云数据库管理服务ClickHouse
  1. 常见的几种触发 GitHub Action 的方式

下面是一个 GitHub Action 的示例:


        
          
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 中可以添加交互参数(可选)。


        
          
on:  
  workflow\_dispatch:  
    inputs:  
      name:  
        description: 'Person to greet'  
        required: true  
        default: 'Mona the Octocat'  

      
  • push, 推送代码

        
          
on:  
  push  

      
  • issues, issues 生命周期

        
          
on:  
  issues:  
    types: [opened, edited, milestoned]  

      
  • issue_comment, issue 评论

        
          
on:  
  issue\_comment:  
    types: [created, deleted]  

      
  • project, 项目管理生命周期

        
          
on:  
  project:  
    types: [created, deleted]  

      
  • pull_request, pr 生命周期

        
          
on:  
  pull\_request:  
    types: [assigned, opened, synchronize, reopened]  

      

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

  1. 使用 API 远程触发 GitHub Action

2.1 创建一个 Token

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

picture.image

需要勾选 repo 权限。

2.2 添加

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


        
          
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 调用格式:


        
          
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 事件

        
          
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 事件

        
          
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 执行:

picture.image

  1. 参考

0
0
0
0
关于作者
关于作者

文章

0

获赞

0

收藏

0

相关资源
vivo 容器化平台架构与核心能力建设实践
为了实现规模化降本提效的目标,vivo 确定了基于云原生理念构建容器化生态的目标。在容器化生态发展过程中,平台架构不断演进,并针对业务的痛点和诉求,持续完善容器化能力矩阵。本次演讲将会介绍 vivo 容器化平台及主要子系统的架构设计,并分享重点建设的容器化核心能力。
相关产品
评论
未登录
看完啦,登录分享一下感受吧~
暂无评论