如何远程触发 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

相关资源
如何利用云原生构建 AIGC 业务基石
AIGC即AI Generated Content,是指利用人工智能技术来生成内容,AIGC也被认为是继UGC、PGC之后的新型内容生产方式,AI绘画、AI写作等都属于AIGC的分支。而 AIGC 业务的部署也面临着异构资源管理、机器学习流程管理等问题,本次分享将和大家分享如何使用云原生技术构建 AIGC 业务。
相关产品
评论
未登录
看完啦,登录分享一下感受吧~
暂无评论