问题描述
我正在使用 Gitlab CI ,因此一直在努力一个相当复杂的 .gitlab-ci.yml
文件.该文件具有after_script
节,该节在主要任务完成或主要任务因某种原因失败时运行.问题:我需要根据主要任务是成功还是失败进行不同的清理,但是我找不到任何 Gitlab CI变量,它指示主要任务的结果.
I'm using Gitlab CI, and so have been working on a fairly complex .gitlab-ci.yml
file. The file has an after_script
section which runs when the main task is complete, or the main task has failed somehow. Problem: I need to do different cleanup based on whether the main task succeeded or failed, but I can't find any Gitlab CI variable that indicates the result of the main task.
如何在after_script
部分中判断主要任务是成功还是失败?
How can I tell, inside the after_script
section, whether the main task has succeeded or failed?
推荐答案
我建议您定义另一个阶段,并使用何时语法,您可以在其中使用when: on_failure
或when: on_success
.
Instead of determining whether or not the task succeeded or failed in the after_script
, I would suggest defining another stage, and using the when syntax, where you can use when: on_failure
or when: on_success
.
文档中的示例:
stages:
- build
- cleanup_build
- test
- deploy
- cleanup
build_job:
stage: build
script:
- make build
cleanup_build_job:
stage: cleanup_build
script:
- cleanup build when failed
when: on_failure
test_job:
stage: test
script:
- make test
deploy_job:
stage: deploy
script:
- make deploy
when: manual
cleanup_job:
stage: cleanup
script:
- cleanup after jobs
when: always
这篇关于.gitlab-ci.yml after_script部分:如何判断任务成功还是失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!