2021 年更新
我知道可以只在特定分支上运行整个工作流程,但这意味着我将拥有一个“测试”工作流程和一个“部署”工作流程。
这听起来像是一个解决方案,但是它们会并行运行。在理想情况下,测试将首先运行,并且只有当它们成功时,才会开始部署作业。使用 2 个单独的工作流程时情况并非如此。
您现在可以使用该事件workflow_run
来实现该部分the tests would run first, and only if they succeed, then the deploy job would start
(继续阅读以了解如何):
文档页面workflow_run
https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows#workflow_run
此事件在请求或完成工作流运行时发生,并允许您根据另一个工作流的完成结果执行工作流。无论先前工作流的结果如何,都会触发工作流运行。
例如,如果您的 pull_request 工作流生成构建工件,您可以创建一个使用 workflow_run 来分析结果并向原始拉取请求添加注释的新工作流。
现在,考虑到 OP 的初始问题:
我希望测试在每个分支上运行,但部署应该只在有东西被推送到时才会发生master
现在可以这样解决:
以下设置正在运行,几分钟前我刚刚在我的一个存储库中实现了相同的逻辑
工作流程<your_repo>/.github/workflows/tests.yml
name: My tests workflow
on:
push:
branches:
- master
pull_request: {}
jobs:
test:
# ... your implementation to run your tests
工作流程<your_repo>/.github/workflows/deploy.yml
name: My deploy workflow
on:
workflow_run:
workflows: My tests workflow # Reuse the name of your tests workflow
branches: master
types: completed
jobs:
deploy:
# `if` required because a workflow run is triggered regardless of
# the result of the previous workflow (see the documentation page)
if: ${{ github.event.workflow_run.conclusion == 'success' }}
# ... your implementation to deploy your project