如何使用GitHub Actions `workflow_run` 事件?
How to use the GitHub Actions `workflow_run` event?
Another frequently-requested feature for Actions is a way to trigger one workflow based on the completion of another workflow. For example, you may want to take the results of a CI workflow and run some further analysis.
The new workflow_run event enables you to trigger a new workflow when one or more workflows are requested or completed. Runs triggered by the workflow_run event always use the default branch for the repository, and have access to a read/write token as well as secrets. As an example, as a maintainer you could set up a workflow that takes the artifacts generated by the pull request workflow, do some analysis, and post comments back to the pull request. This event is also available as a webhook and works all repos.
本文引用自Github's blog。
谁能告诉我如何使用新事件 workflow_run
实施建议的 示例 ?文档只提供了一个非常简单的例子:
on:
workflow_run:
workflows: ["Run Tests"]
branches: [main]
types:
- completed
- requested
如果有人能教我如何实现这个例子,我会很高兴。
要使示例正常工作(即让一个工作流程等待另一个工作流程完成),您需要两个文件。这两个文件都位于存储库的 .github/workflows
文件夹中。
第一个文件将照常设置。此文件将由 on
部分中设置的任何事件触发:
---
name: Preflight
on:
- pull_request
- push
jobs:
preflight-job:
name: Preflight Step
runs-on: ubuntu-latest
steps:
- run: env
第二个文件声明它应该只触发 on
任何 workflows
名称为 Preflight
:
的 workflow_run
事件
---
name: Test
on:
workflow_run:
workflows:
- Preflight
types:
- completed
jobs:
test-job:
name: Test Step
runs-on: ubuntu-latest
steps:
- run: env
这与 the example from the GitHub Actions manual 大致相同。
如您所见the actions page
my example repo,预检工作流程将首先 运行。完成后,将触发测试工作流程:
您可以也看到,分支不会出现在“测试”工作流中。
这是因为,(引自手册):
This event will only trigger a workflow run if the workflow file is on the default branch.
这意味着“测试”工作流程将 运行 on/with 来自默认分支的代码(通常是 main
或 master
)。
有一个解决方法...
每个动作都是运行一组contexts。 github
上下文包含有关触发工作流的事件的信息。这包括最初触发事件的分支 from/for: github.event.workflow_run.head_branch
.
这可用于检查操作中的起源分支,使用 GitHub 提供的 actions/checkout
操作。
为此,Yaml 将是:
---
name: Test
on:
workflow_run:
workflows:
- Preflight
types:
- completed
jobs:
test-job:
name: Test Step
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.event.workflow_run.head_branch }}
- run: git branch
- run: env
Another frequently-requested feature for Actions is a way to trigger one workflow based on the completion of another workflow. For example, you may want to take the results of a CI workflow and run some further analysis.
The new workflow_run event enables you to trigger a new workflow when one or more workflows are requested or completed. Runs triggered by the workflow_run event always use the default branch for the repository, and have access to a read/write token as well as secrets. As an example, as a maintainer you could set up a workflow that takes the artifacts generated by the pull request workflow, do some analysis, and post comments back to the pull request. This event is also available as a webhook and works all repos.
本文引用自Github's blog。
谁能告诉我如何使用新事件 workflow_run
实施建议的 示例 ?文档只提供了一个非常简单的例子:
on:
workflow_run:
workflows: ["Run Tests"]
branches: [main]
types:
- completed
- requested
如果有人能教我如何实现这个例子,我会很高兴。
要使示例正常工作(即让一个工作流程等待另一个工作流程完成),您需要两个文件。这两个文件都位于存储库的 .github/workflows
文件夹中。
第一个文件将照常设置。此文件将由 on
部分中设置的任何事件触发:
---
name: Preflight
on:
- pull_request
- push
jobs:
preflight-job:
name: Preflight Step
runs-on: ubuntu-latest
steps:
- run: env
第二个文件声明它应该只触发 on
任何 workflows
名称为 Preflight
:
workflow_run
事件
---
name: Test
on:
workflow_run:
workflows:
- Preflight
types:
- completed
jobs:
test-job:
name: Test Step
runs-on: ubuntu-latest
steps:
- run: env
这与 the example from the GitHub Actions manual 大致相同。
如您所见the actions page my example repo,预检工作流程将首先 运行。完成后,将触发测试工作流程:
您可以也看到,分支不会出现在“测试”工作流中。
这是因为,(引自手册):
This event will only trigger a workflow run if the workflow file is on the default branch.
这意味着“测试”工作流程将 运行 on/with 来自默认分支的代码(通常是 main
或 master
)。
有一个解决方法...
每个动作都是运行一组contexts。 github
上下文包含有关触发工作流的事件的信息。这包括最初触发事件的分支 from/for: github.event.workflow_run.head_branch
.
这可用于检查操作中的起源分支,使用 GitHub 提供的 actions/checkout
操作。
为此,Yaml 将是:
---
name: Test
on:
workflow_run:
workflows:
- Preflight
types:
- completed
jobs:
test-job:
name: Test Step
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.event.workflow_run.head_branch }}
- run: git branch
- run: env