簡體   English   中英

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 Actions 上的工作流之間建立依賴關系。

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

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

更新:現在可以使用workflow_run創建對工作流的依賴項。 看到這個答案

有沒有辦法在工作流之間創建依賴關系?

我認為目前這是不可能的。 也許這是他們將來會添加的功能。 就個人而言,我認為更有可能添加像 CircleCI 的球體這樣的功能來共享工作流的公共部分。

對於替代解決方案,將其全部放在與以下相同的工作流程中對您有用嗎? 只有在推送以v開頭的標簽時,才會執行deploy-packages作業。

name: my workflow
on: [push, pull_request]
jobs:
  test-packages:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - name: test packages
        run: echo "Running tests"
  deploy-packages:
    if: startsWith(github.ref, 'refs/tags/v')
    runs-on: ubuntu-latest
    needs: test-packages
    steps:
      - uses: actions/checkout@v1
      - name: deploy packages
        run: echo "Deploying packages"

正如它在自述文件中聲明的那樣, Wait n Check 操作似乎是當前缺少此功能的最佳解決方法:

它允許解決非相互依賴工作流的 GitHub 操作限制(我們只能依賴單個工作流中的作業)。

更新:另請參閱我的其他答案,了解使用workflow_run的部分解決方案。

您可以(也)通過組合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' }}
    (...)

....不幸的是,無法以這種方式檢查第三個條件,因為deploy工作流是在默認分支的 HEAD 的上下文中觸發的,而不知道可能指向那里的標簽。

所以做類似的事情:

    if: ${{ github.event.workflow_run.conclusion == 'success' }} && startsWith(github.ref, 'refs/tags/') }}

...不管用。


當我找到此問題的解決方法時,我將更新此答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM