Github 操作的工作流之间的依赖关系
Dependencies Between Workflows on Github Actions
我有一个包含两个工作流程的 monorepo:
.github/workflows/test.yml
name: test
on: [push, pull_request]
jobs:
test-packages:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: test packages
run: |
yarn install
yarn test
...
.github/workflows/deploy.yml
name: deploy
on:
push:
tags:
- "*"
jobs:
deploy-packages:
runs-on: ubuntu-latest
needs: test-packages
steps:
- uses: actions/checkout@v1
- name: deploy packages
run: |
yarn deploy
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
...
这不起作用,我无法在另一个工作流程中引用作业:
### ERRORED 19:13:07Z
- Your workflow file was invalid: The pipeline is not valid. The pipeline must contain at least one job with no dependencies.
有没有办法在工作流之间创建依赖关系?
我想要的是 运行 test.yml
然后 deploy.yml
在标签上, test.yml
仅在推送和拉取请求上。我不想在工作流程之间重复作业。
现在可以使用 workflow_run.
在 Github 操作的工作流程之间建立依赖关系
使用此配置,Release
工作流程将在 Run Tests
工作流程完成后运行。
name: Release
on:
workflow_run:
workflows: ["Run Tests"]
branches: [main]
types:
- completed
似乎 Wait n Check action 目前是这个缺失功能的最佳解决方法,正如它在其自述文件中声明的那样:
It allows to work around a GitHub Actions limitation of non-interdependent workflows (we can only depend on jobs inside a single workflow).
更新:另见使用workflow_run
.
的部分解决方案
您(也)可以通过组合 workflow_run and if.
来完成
使用以下配置,deploy
工作流程仅在满足所有这些条件时才会启动:
test
工作流程完成后,
- 如果
test
工作流程成功,
有一个标签被推送到默认分支,
假设默认分支为main
:
name: deploy
on:
# the 1st condition
workflow_run:
workflows: ["tests"]
branches: [main]
types:
- completed
jobs:
deploy-packages:
# the 2nd condition
if: ${{ github.event.workflow_run.conclusion == 'success' }}
(...)
...BUT 不幸的是,第三个条件无法通过这种方式检查,因为 deploy
工作流是在默认分支的 HEAD 上下文中触发的,不知道可能指向那里的标签。
所以做这样的事情:
if: ${{ github.event.workflow_run.conclusion == 'success' }} && startsWith(github.ref, 'refs/tags/') }}
...将不起作用。
当我找到解决此问题的方法时,我会更新此答案。
您可以创建可重复使用的工作流:https://docs.github.com/en/actions/using-workflows/reusing-workflows。
这可以通过 workflow_call
和 needs
的组合来实现。将 workflow_call
添加到作业的 on
值允许它被其他作业调用。然后您可以从其他工作流程调用该作业,并使用 needs
强制后续步骤取决于该作业的成功。您可以在此处阅读有关使工作流可调用的信息:https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#onworkflow_call and requiring a step to succeed before triggering future steps here: https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idneeds
因此,对于您的情况,以下应该有效:
# cat .github/workflows/tests.yml
name: tests
on: [workflow_call] # allow this workflow to be called from other workflows
jobs:
...
# cat .github/workflows/deploy.yml
name: deploy
on:
push:
tags: # trigger the deploy job on tag creation
- *
jobs:
tests:
uses: ./.github/workflows/tests.yml # use the callable tests job to run tests
deploy:
name: deploy
needs: [tests] # require tests to pass before deploy runs
...
我有一个包含两个工作流程的 monorepo:
.github/workflows/test.yml
name: test
on: [push, pull_request]
jobs:
test-packages:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: test packages
run: |
yarn install
yarn test
...
.github/workflows/deploy.yml
name: deploy
on:
push:
tags:
- "*"
jobs:
deploy-packages:
runs-on: ubuntu-latest
needs: test-packages
steps:
- uses: actions/checkout@v1
- name: deploy packages
run: |
yarn deploy
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
...
这不起作用,我无法在另一个工作流程中引用作业:
### ERRORED 19:13:07Z
- Your workflow file was invalid: The pipeline is not valid. The pipeline must contain at least one job with no dependencies.
有没有办法在工作流之间创建依赖关系?
我想要的是 运行 test.yml
然后 deploy.yml
在标签上, test.yml
仅在推送和拉取请求上。我不想在工作流程之间重复作业。
现在可以使用 workflow_run.
在 Github 操作的工作流程之间建立依赖关系使用此配置,Release
工作流程将在 Run Tests
工作流程完成后运行。
name: Release
on:
workflow_run:
workflows: ["Run Tests"]
branches: [main]
types:
- completed
似乎 Wait n Check action 目前是这个缺失功能的最佳解决方法,正如它在其自述文件中声明的那样:
It allows to work around a GitHub Actions limitation of non-interdependent workflows (we can only depend on jobs inside a single workflow).
更新:另见workflow_run
.
您(也)可以通过组合 workflow_run and if.
来完成使用以下配置,deploy
工作流程仅在满足所有这些条件时才会启动:
test
工作流程完成后,- 如果
test
工作流程成功, 有一个标签被推送到默认分支,
假设默认分支为main
:
name: deploy
on:
# the 1st condition
workflow_run:
workflows: ["tests"]
branches: [main]
types:
- completed
jobs:
deploy-packages:
# the 2nd condition
if: ${{ github.event.workflow_run.conclusion == 'success' }}
(...)
...BUT 不幸的是,第三个条件无法通过这种方式检查,因为 deploy
工作流是在默认分支的 HEAD 上下文中触发的,不知道可能指向那里的标签。
所以做这样的事情:
if: ${{ github.event.workflow_run.conclusion == 'success' }} && startsWith(github.ref, 'refs/tags/') }}
...将不起作用。
当我找到解决此问题的方法时,我会更新此答案。
您可以创建可重复使用的工作流:https://docs.github.com/en/actions/using-workflows/reusing-workflows。
这可以通过 workflow_call
和 needs
的组合来实现。将 workflow_call
添加到作业的 on
值允许它被其他作业调用。然后您可以从其他工作流程调用该作业,并使用 needs
强制后续步骤取决于该作业的成功。您可以在此处阅读有关使工作流可调用的信息:https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#onworkflow_call and requiring a step to succeed before triggering future steps here: https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idneeds
因此,对于您的情况,以下应该有效:
# cat .github/workflows/tests.yml
name: tests
on: [workflow_call] # allow this workflow to be called from other workflows
jobs:
...
# cat .github/workflows/deploy.yml
name: deploy
on:
push:
tags: # trigger the deploy job on tag creation
- *
jobs:
tests:
uses: ./.github/workflows/tests.yml # use the callable tests job to run tests
deploy:
name: deploy
needs: [tests] # require tests to pass before deploy runs
...