[英]Manual workflow triggers in Github Actions
I am setting up Github Actions for a project repository.我正在为项目存储库设置 Github 操作。
The workflow consists of the following steps:工作流程包括以下步骤:
However, I have two different Kubernetes deployments: one for development, and one for production.但是,我有两种不同的 Kubernetes 部署:一种用于开发,一种用于生产。 Hence, I have also two Github Action workflows.
因此,我还有两个 Github 操作工作流。
The Github Action workflow for development is triggered everytime that a commit is pushed:每次推送提交时都会触发 Github Action 开发工作流:
on:
push:
branches:
- master
But I don't want that for my production workflow.但我不希望将其用于我的生产工作流程。 I would need a manual trigger, like a Send to production button.
我需要一个手动触发器,比如发送到生产按钮。 I didn't see anything close to that in the docs.
我在文档中没有看到任何与此相似的内容。
Is there a way to trigger a workflow manually in Github Actions?有没有办法在 Github 操作中手动触发工作流?
How can I split my development and my production workflows to achieve what I want, either on Github Actions, Docker or Kubernetes?如何在 Github Actions、Docker 或 Kubernetes 上拆分我的开发和生产工作流程以实现我想要的?
Is there a way to trigger a workflow manually in Github Actions?
有没有办法在 Github 操作中手动触发工作流?
You might consider, from July2020 :您可能会考虑, 从 2020 年 7 月开始:
GitHub Actions: Manual triggers with workflow_dispatch
GitHub 操作:使用 workflow_dispatch 手动触发
(Note: or multiple workflows, through the new Composite Run Steps , August 2020) (注:或多个工作流,通过新的 Composite Run Steps ,2020 年 8 月)
You can now create workflows that are manually triggered with the new
workflow_dispatch
event.您现在可以创建使用新的
workflow_dispatch
事件手动触发的工作流。
You will then see a 'Run workflow
' button on the Actions tab, enabling you to easily trigger a run.然后,您将在“操作”选项卡上看到一个“
Run workflow
”按钮,使您能够轻松触发运行。You can choose which branch the workflow is run on.
您可以选择在哪个分支上运行工作流。
philippe adds in the comments :菲利普在评论中补充道:
One thing that's not mentioned in the documentation: the workflow must exist on the default branch for the "
Run workflow
" button to appear.文档中没有提到的一件事:工作流必须存在于默认分支上,“
Run workflow
”按钮才会出现。
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将其添加到那里后,您可以继续在其自己的分支上开发操作,并且更改将在使用按钮运行时生效
The documentation goes on:文档继续:
In addition, you can optionally specify inputs, which GitHub will present as form elements in the UI.
此外,您可以选择指定输入,GitHub 将在 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.触发的工作流接收
github.event
上下文中的输入。For example:
例如:
jobs:
printInputs:
runs-on: ubuntu-latest
steps:
- run: |
echo "Log level: ${{ github.event.inputs.logLevel }}"
echo "Tags: ${{ github.event.inputs.tags }}"
EDITED :编辑:
Great tweet explaining the use of workflow dispatch: https://twitter.com/github/status/1321859709075394563?s=19很棒的推文解释了工作流调度的使用: https://twitter.com/github/status/1321859709075394563?s=19
Is there a way to trigger a workflow manually in Github Actions?
有没有办法在 Github 操作中手动触发工作流?
I've got a little hack to do so...我有一个小技巧可以做到这一点......
With the watch event, you can manually trigger an action by star or unstar the repo.使用 watch 事件,你可以手动触发一个动作,通过 star 或 unstar repo。 The code for the event in your workflow is:
您的工作流程中的事件代码是:
on:
watch
types: [started]
I know it's weird but it works, Nevertheless.我知道这很奇怪,但它确实有效,尽管如此。 it's not the best way if it's a public repo with potential stars.
如果它是具有潜在明星的公共回购,这不是最好的方式。
How can I split my development and my production workflows to achieve what I want, either on Github Actions, Docker or Kubernetes?
如何在 Github Actions、Docker 或 Kubernetes 上拆分我的开发和生产工作流程以实现我想要的?
In Github Actions I mean, you can do multiple workflows / jobs and filter by targeted branches or events.在 Github 操作中,我的意思是,您可以执行多个工作流/作业并按目标分支或事件进行过滤。 You can combine multiple events for example trigger a workflow for push and with a cron on midnight.
您可以组合多个事件,例如触发推送工作流和午夜的 cron 。
Update : For a slash command style "ChatOps" solution see slash-command-dispatch action.更新:对于斜杠命令样式“ChatOps”解决方案,请参阅斜杠命令调度操作。 This can allow you to trigger workflows with slash commands (eg
/deploy
) from issue and pull request comments.这可以让您从问题和拉取请求评论中使用斜杠命令(例如
/deploy
)触发工作流。
Here is a basic example for a deploy
slash command.这是
deploy
斜杠命令的基本示例。 REPO_ACCESS_TOKEN
is a repo
scoped Personal Access Token REPO_ACCESS_TOKEN
是一个repo
范围的个人访问令牌
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
The command can be processed in this workflow.可以在此工作流程中处理命令。
name: Deploy Command
on:
repository_dispatch:
types: [deploy-command]
There are many more options and different setups.还有更多选项和不同的设置。 See slash-command-dispatch for full usage instructions.
有关完整的使用说明,请参阅slash-command-dispatch 。
Original Answer : A repository_dispatch
workflow can be manually triggered by a call to the GitHub API as follows.原始答案:可以通过调用 GitHub API 手动触发
repository_dispatch
工作流程,如下所示。
on:
repository_dispatch:
types: [production-deploy]
[username]
is a GitHub username [username]
是 GitHub 用户名[token]
is a repo
scoped Personal Access Token [token]
是一个repo
范围的个人访问令牌[repository]
is the name of the repository the workflow resides in. [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"}'
Another way to resolve this with the current Github Action offering is to create a production
branch from master when a deploy is needed & trigger deploy action on the production
branch.使用当前 Github Action 产品解决此问题的另一种方法是在需要部署时从 master 创建一个
production
分支并在production
分支上触发部署操作。 The production
branch is essentially a mirror of the master
. production
分支本质上是master
的镜像。
on:
push:
branches:
- master
Dev builds/push can happen whenever there is a commit to the master.只要有对主服务器的提交,就可能发生开发构建/推送。
on:
push:
branches:
- production
At some point in the release schedule, you can raise the PR to the production
branch.在发布计划的某个时间点,您可以将 PR 提升到
production
分支。 This will take care of the prod build/deploy.这将负责产品构建/部署。
Although Sarah's post was the closest and simplest answer to the original question, it is somewhat hacky so we eventually ended up by creating a dev
branch to use the following triggers:尽管Sarah 的帖子是对原始问题的最接近和最简单的答案,但它有点 hacky,所以我们最终创建了一个
dev
分支来使用以下触发器:
Development workflow: triggered when a push is made on the dev
branch:开发工作流程:在
dev
分支上进行推送时触发:
on: push: branches: - dev
Production workflow: triggered when a pull request / merge is made from dev
to master
:生产工作流程:当从
dev
到master
发出拉取请求/合并时触发:
on: pull_request: branches: - master
Edited for more detail/explanation.编辑以获取更多详细信息/解释。
One thing that you can do is call to repository_dispatch
.您可以做的一件事是调用
repository_dispatch
。 You can view the GitHub documentation for using a repository_dispatch
here .您可以在此处查看有关使用
repository_dispatch
的 GitHub 文档。
For example, if you have a GitHub Actions workflow that looks like this:例如,如果您有一个如下所示的 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!"
You can create a repository dispatch event by following the steps that are explained on the GitHub v3 API Documentation .您可以按照GitHub v3 API 文档中说明的步骤创建存储库调度事件。
First, create a personal access token (PAT) on GitHub for authentication.首先,在 GitHub 上创建个人访问令牌 (PAT)以进行身份验证。
Then, you can run curl
like so:然后,您可以像这样运行
curl
:
curl \
-H "Authorization: token $YOUR_PAT" \
--request POST \
--data '{"event_type": "run_tests"}' \
https://api.github.com/repos/$USER/$REPOSITORY/dispatches
At the same time, I also wanted to share a small project that I've been working on with a buddy that solves this exact problem.同时,我也想分享一个小项目,我一直在与一个解决这个确切问题的朋友一起工作。
https://www.actionspanel.app/ https://www.actionspanel.app/
ActionsPanel uses this same repository_dispatch
API but does so with a GitHub App token so that you don't need to worry about managing your own PAT. ActionsPanel 使用相同的
repository_dispatch
API 但使用 GitHub App 令牌这样做,因此您无需担心管理自己的 PAT。 This also makes it much easier to trigger your actions across teams with multiple people.这也使得跨多人团队触发您的操作变得更加容易。
Based on user requests and feedback, we've built in features to specify which branch to send the repository_dispatch
to, and we've even built in a way to inject parameters when you want to execute the action.根据用户请求和反馈,我们内置了功能来指定将
repository_dispatch
发送到哪个分支,我们甚至内置了一种在您想要执行操作时注入参数的方法。
You configure your buttons with a declarative yaml file that you leave in the repo, and ActionsPanel will read that file and dynamically create your UI for you to trigger your actions.您使用保留在 repo 中的声明性 yaml 文件配置您的按钮,ActionsPanel 将读取该文件并动态创建您的 UI 以触发您的操作。
What GitHub cryptic documentation fails to clarify is that you can have multiple workflow files under .github/workflows
, each with its own trigger. GitHub 神秘文档未能阐明的是,您可以在
.github/workflows
下拥有多个工作流文件,每个文件都有自己的触发器。 For instance, I've a workflow that builds and runs tests on every push and pull request, and another that is triggered manually to publish the artifact.例如,我有一个工作流在每个推送和拉取请求上构建和运行测试,另一个是手动触发以发布工件。
(ci.yml)
name: CI Pipeline
on: [push, pull_request]
---
(publish.yml)
name: Publish
on:
workflow_dispatch:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.