Github 操作中的手动工作流触发器

Manual workflow triggers in Github Actions

我正在为项目存储库设置 Github 操作。

工作流程包括以下步骤:


但是,我有两种不同的 Kubernetes 部署:一种用于开发,一种用于生产。因此,我还有两个 Github 操作工作流程。

每次推送提交时都会触发 Github 开发操作工作流:

on:
  push:
    branches:
    - master

但我不希望我的制作工作流程出现这种情况。我需要一个手动触发器,例如 Send to production 按钮。我在文档中没有看到任何接近的内容。


有没有办法在 Github 操作中手动触发工作流?

如何在 Github Actions、Docker 或 Kubernetes 上拆分我的开发和生产工作流程以实现我想要的?

更新:有关斜杠命令样式 "ChatOps" 解决方案,请参阅 slash-command-dispatch 操作。这可以让您使用问题和拉取请求评论中的斜杠命令(例如 /deploy)触发工作流。

这里是 deploy 斜杠命令的基本示例。 REPO_ACCESS_TOKEN 是一个 repo 范围的 Personal Access Token

name: Slash Command Dispatch
on:
  issue_comment:
    types: [created]
jobs:
  slashCommandDispatch:
    runs-on: ubuntu-latest
    steps:
      - name: Slash Command Dispatch
        uses: peter-evans/slash-command-dispatch@v1
        with:
          token: ${{ secrets.REPO_ACCESS_TOKEN }}
          commands: deploy

可以在此工作流中处理该命令。

name: Deploy Command
on:
  repository_dispatch:
    types: [deploy-command]

还有更多选项和不同的设置。有关完整的使用说明,请参阅 slash-command-dispatch

原答案: repository_dispatch 工作流程可以通过调用 GitHub API 手动触发,如下所示。

on:
  repository_dispatch:
    types: [production-deploy]
  • [username] 是 GitHub 用户名
  • [token] 是一个 repo 范围 Personal Access Token
  • [repository] 是工作流所在的存储库的名称。
curl -XPOST -u "[username]:[token]" \
  -H "Accept: application/vnd.github.everest-preview+json" \
  -H "Content-Type: application/json" \
  https://api.github.com/repos/[username]/[repository]/dispatches \
  --data '{"event_type": "production-deploy"}'

已编辑

很棒的推文,解释了工作流分派的使用: https://twitter.com/github/status/1321859709075394563?s=19


Is there a way to trigger a workflow manually in Github Actions?

我有一些小窍门...

使用 watch 事件,您可以手动触发一个操作,通过加星或取消加星 repo。您工作流程中事件的代码是:

on:
  watch
    types: [started]

我知道这很奇怪,但它确实有效!然而,如果它是一个有潜力明星的 public 回购协议,这不是最好的方法。


How can I split my development and my production workflows to achieve what I want, either on Github Actions, Docker or Kubernetes?

在 Github 操作中,我的意思是,您可以执行多个工作流/作业并按目标分支或事件进行过滤。您可以组合多个事件,例如在午夜触发 push 的工作流和 cron

虽然 是对原始问题最接近和最简单的答案,但它有点老套,所以我们最终创建了一个 dev 分支来使用以下触发器:

  • 开发工作流程:在 dev 分支上进行推送时触发:

    on:
      push:
        branches:    
          - dev
    
  • 生产工作流程:当拉取请求/合并从 devmaster 时触发:

    on:
      pull_request:
        branches:    
          - master
    

已编辑更多 detail/explanation。

您可以做的一件事是调用 repository_dispatch。您可以查看 GitHub 文档以了解如何使用 repository_dispatch here.

例如,如果您的 GitHub 操作工作流程如下所示:

on:
  repository_dispatch:
    types: [run_tests]
name: Run tests
jobs:
  test:
    name: Run your tests
    runs-on: ubuntu-latest
    steps:
      - run: |
          echo "I just ran all your tests!"

您可以按照 GitHub v3 API Documentation.

中说明的步骤创建存储库分派事件

首先,create a personal access token (PAT) 在 GitHub 上进行身份验证。

然后,您可以 运行 curl 像这样:

curl \
  -H "Authorization: token $YOUR_PAT" \
  --request POST \
  --data '{"event_type": "run_tests"}' \
  https://api.github.com/repos/$USER/$REPOSITORY/dispatches

与此同时,我还想分享一个我一直在与解决这个确切问题的伙伴合作的小项目。

https://www.actionspanel.app/

ActionsPanel 使用相同的 repository_dispatch API,但使用 GitHub 应用令牌,因此您无需担心管理自己的 PAT。这也使得在多人团队中触发您的操作变得更加容易。

根据用户的请求和反馈,我们内置了一些功能来指定将 repository_dispatch 发送到哪个分支,我们甚至还内置了一种在您想要执行时注入参数的方法动作。

您使用保留在存储库中的声明性 yaml 文件配置您的按钮,ActionsPanel 将读取该文件并动态创建您的 UI 以供您触发操作。

使用当前 Github 操作产品解决此问题的另一种方法是在需要部署时从 master 创建一个 production 分支并在 production 分支上触发部署操作。 production 分支本质上是 master.

的镜像
on:
  push:
    branches:    
      - master

Dev builds/push 可以在提交给 master 时发生。

on:
  push:
    branches:    
      - production

在发布计划的某个时刻,您可以将 PR 提升到 production 分支。这将处理产品 build/deploy.

Is there a way to trigger a workflow manually in Github Actions?

你可以考虑,from July2020:

GitHub Actions: Manual triggers with workflow_dispatch

(注:或多个工作流,通过 new Composite Run Steps,2020 年 8 月)

You can now create workflows that are manually triggered with the new workflow_dispatch event.
You will then see a 'Run workflow' button on the Actions tab, enabling you to easily trigger a run.

You can choose which branch the workflow is run on.

philippe adds :

One thing that's not mentioned in the documentation: the workflow must exist on the default branch for the "Run workflow" button to appear.
Once you add it there, you can continue developing the action on its own branch and the changes will take effect when run using the button

文档继续:

In addition, you can optionally specify inputs, which GitHub will present as form elements in the UI. Workflow dispatch inputs are specified with the same format as action inputs.

For example:

on: 
  workflow_dispatch:
    inputs:
      logLevel:
        description: 'Log level'     
        required: true
        default: 'warning'
      tags:
        description: 'Test scenario tags'  

The triggered workflow receives the inputs in the github.event context.

For example:

jobs:
  printInputs:
    runs-on: ubuntu-latest
    steps:
    - run: |
        echo "Log level: ${{ github.event.inputs.logLevel }}"
        echo "Tags: ${{ github.event.inputs.tags }}" 

shim adds in :

You can add workflow_dispatch to a workflow that also has other triggers (like on push and / or schedule)

For instance:

on:
 workflow_dispatch:
 push:
   branches:
     - master
 pull_request:
   types: [opened, synchronize, reopened]

GitHub 神秘文档未能阐明的是,您可以在 .github/workflows 下拥有多个工作流文件,每个文件都有自己的触发器。例如,我有一个针对每个推送和拉取请求构建和运行测试的工作流,以及另一个手动触发以发布工件的工作流。

(ci.yml)

name: CI Pipeline
on: [push, pull_request]

---
(publish.yml)

name: Publish
on:
  workflow_dispatch: