我们确实遇到了 GitHub Actions 作业的问题,尽管所有“需要的”作业都已成功运行,但该作业始终会被跳过。这就是工作:
deploy-api:
needs: [build-test-api, terraform-apply, set-deployment-env]
uses: ./.github/workflows/workflow-api-deploy.yml
为了验证所有需求均已通过,我添加了另一个作业进行调试并打印了所需作业的结果。
debug-deploy-api:
runs-on: ubuntu-latest
needs: [build-test-api, terraform-apply, set-deployment-env]
if: always() # Had to add this, otherwise it would be skipped just as "deploy-api".
steps:
- run: |
echo "Result of build-test-api: ${{ needs.build-test-api.result }}"
echo "Result of terraform-apply: ${{ needs.terraform-apply.result }}"
echo "Result of set-deployment-env: ${{ needs.set-deployment-env.result }}"
输出是
Result of build-test-api: success
Result of terraform-apply: success
Result of set-deployment-env: success
我不明白为什么跳过
deploy-api
。
将依赖项添加到
build-test-api
后开始行为:
使用此版本的
build-test-api
,部署作业确实运行得很好:
build-test-api:
uses: # reusable WF from internal repo
needs: set-deployment-env
改成后
build-test-api:
uses: # reusable WF from internal repo
needs: [set-deployment-env, auto-versioning]
if: |
always() &&
(needs.set-deployment-env.result == 'success') &&
(needs.auto-versioning.result == 'success' || needs.auto-versioning.result == 'skipped')
deploy-api
一直被跳过。但是,尽管发生了这种变化,build-test-api
仍然运行良好,甚至将创建的工件附加到工作流程运行中。
激活运行器和步骤调试日志记录并没有揭示任何有关作业仍被跳过的原因的见解。有什么想法吗?
同时我联系了 GitHub 高级支持,他们提供了解决方案:
deploy-api:
if: success('build-test-api') # This line is required, if any of the previous job did not end with status 'success'.
needs: build-test-api
uses: ./.github/workflows/48-reusable-workflow-2.yml
我想我也知道为什么:文档说:
您可以使用以下状态检查函数作为
条件语句中的表达式。除非您包含这些函数之一,否则将应用默认状态检查if
。success()
success()
的定义如下:
当前面的步骤均未失败或取消时,返回
。true
我认为唯一的问题应该是:
当前面的步骤均未失败、取消或跳过时,返回
。true
作为对那些从另一个 ci 文件中引用多个工作的人的单独答案:
我有一个ci.yaml
,有 3 份工作:
php-cs
、
phpstan
和
phpunit
。我也有
build-image.yaml
。我用它来构建图像,但只想在
ci.yaml
中的所有内容都成功时运行它。文档对此不是很清楚,只描述了“需求”,但只有一个需要匹配。我希望在构建之前一切都成功,这就是我的情况:
jobs:
code-quality: #random name
uses: ./.github/workflows/ci.yml # the file we want the jobs from
build-and-push-image:
needs: code-quality #same as 'random name'
if: success('code-quality') # <-- magic line to change ANY match to ALL match