Gui*_*ent 59 continuous-integration github continuous-deployment 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
...
Run Code Online (Sandbox Code Playgroud)
.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 }}
...
Run Code Online (Sandbox Code Playgroud)
这不起作用,我无法在另一个工作流程中引用作业:
### 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.
Run Code Online (Sandbox Code Playgroud)
有没有办法在工作流之间创建依赖关系?
我想要的是test.yml
然后deploy.yml
在标签上运行,并且test.yml
只在推送和拉取请求上运行。我不想在工作流之间复制作业。
lha*_*son 111
workflow_call
这可以通过和的组合来实现needs
。添加workflow_call
到on
作业的值允许其他作业调用它。然后,您可以从其他工作流程调用该作业,并用于needs
强制未来的步骤取决于该作业的成功。您可以在此处阅读有关使工作流程可调用的信息:https ://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#onworkflow_call ,并要求在触发未来步骤之前先执行一个步骤: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
...
Run Code Online (Sandbox Code Playgroud)
Ahm*_*aid 65
现在可以使用workflow_run在 Github Actions 上的工作流之间建立依赖关系。
使用此配置,Release
工作流将在Run Tests
工作流完成后工作。
name: Release
on:
workflow_run:
workflows: ["Run Tests"]
branches: [main]
types:
- completed
Run Code Online (Sandbox Code Playgroud)
Gre*_*cki 47
您也可以通过组合workflow_run和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' }}
(...)
Run Code Online (Sandbox Code Playgroud)
....但不幸的是,无法以这种方式检查第三个条件,因为deploy
工作流是在默认分支的 HEAD 上下文中触发的,而不了解可能指向那里的标签。
所以做类似的事情:
if: ${{ github.event.workflow_run.conclusion == 'success' }} && startsWith(github.ref, 'refs/tags/') }}
Run Code Online (Sandbox Code Playgroud)
...不管用。
当我找到这个问题的解决方法时,我会更新这个答案。
pet*_*ans 25
更新:现在可以使用workflow_run
. 看到这个答案。
有没有办法在工作流之间创建依赖关系?
我认为目前这是不可能的。也许这是他们将来会添加的功能。就个人而言,我认为更有可能添加像 CircleCI 的球体这样的功能来共享工作流程的公共部分。
对于替代解决方案,是否将其全部放在与以下相同的工作流程中对您有用?deploy-packages
只有v
在推送以 开头的标签时,作业才会执行。
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"
Run Code Online (Sandbox Code Playgroud)
正如其自述文件中所声明的那样,“Wait n Check”操作目前似乎是此缺失功能的最佳解决方法:
它允许解决非相互依赖工作流程的 GitHub Actions 限制(我们只能依赖于单个工作流程中的作业)。
更新:另请参阅我的其他答案,了解使用的部分解决方案workflow_run
。
归档时间: |
|
查看次数: |
14674 次 |
最近记录: |