【问题标题】:Dependencies Between Workflows on Github ActionsGithub 操作上的工作流之间的依赖关系
【发布时间】:2020-02-15 19:43:42
【问题描述】:

我有一个带有两个工作流程的 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。我不想在工作流之间重复作业。

【问题讨论】:

    标签: github continuous-integration continuous-deployment github-actions


    【解决方案1】:

    现在可以使用 workflow_run 在 Github Actions 上的工作流之间建立依赖关系。

    使用此配置,Release 工作流将在 Run Tests 工作流完成后工作。

    name: Release
    on:
      workflow_run:
        workflows: ["Run Tests"]
        branches: [main]
        types: 
          - completed
    

    【讨论】:

    • 重要的是要知道,只要Run Tests 工作流完成,无论是成功还是失败,您的示例中的Release 工作流都会运行。如果工作流结果很重要(通常很重要),您需要检查github.event.workflow_run.conclusion。更多细节在这里:github.community/t/…
    • 我们可以在工作流中给出两个工作流名称吗?我必须在完成另外两个工作流程后触发它。
    【解决方案2】:

    您可以(也)通过组合 workflow_runif 来做到这一点。


    使用以下配置,deploy 工作流将仅在所有这些条件都为真时启动:

    1. test 工作流程完成后,
    2. 如果test 工作流成功,
    3. 有一个标签被推送到默认分支,

    假设默认分支为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/') }}
    

    ...不会工作。


    当我找到此问题的解决方法时,我会更新此答案。

    【讨论】:

      【解决方案3】:

      Wait n Check action 似乎是目前缺少此功能的最佳解决方法,正如它在自述文件中声明的那样:

      ? 它允许解决非相互依赖工作流的 GitHub Actions 限制(我们只能依赖单个工作流中的作业)。

      更新:另请参阅my other answer,了解使用workflow_run 的部分解决方案。

      【讨论】:

      • 该操作似乎只是像超时一样等待,而不是在让简洁的作业开始之前真正等待工作流完成。
      【解决方案4】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-28
        • 1970-01-01
        • 1970-01-01
        • 2012-09-16
        • 2018-10-20
        • 2019-02-17
        • 2015-04-22
        • 1970-01-01
        相关资源
        最近更新 更多