[英]Trigger action/job if pipeline job/stage fails
I have a GitLab pipeline one of whose jobs writes some content into a file, a simple .txt
file, and pushes a Git tag.我有一个 GitLab 管道,其中一个工作将一些内容写入一个文件,一个简单的.txt
文件,并推送一个 Git 标签。 After this job, other jobs are executed.在此作业之后,将执行其他作业。
I want to trigger an automatic action/job or something that undoes the .txt
file writing and deletes the Git tag in case any of the jobs of the pipeline fails.我想触发自动操作/作业或撤消.txt
文件写入并删除 Git 标记的东西,以防管道的任何作业失败。
Is it possible to do such a thing?有可能做这样的事情吗? Is there some kind of GitLab pipeline job that triggers only in case another fails?是否有某种 GitLab 管道作业仅在另一个失败时触发?
You're looking for the when: on_failure
option.您正在寻找when: on_failure
选项。 The docs for it are here: https://docs.gitlab.com/ee/ci/yaml/README.html#when它的文档在这里: https://docs.gitlab.com/ee/ci/yaml/README.html#when
If you put this on a job, that job will only run if another job in your pipeline from an earlier stage fails.如果你把它放在一个作业上,那么只有当你的管道中的另一个作业从早期阶段失败时,该作业才会运行。 If you only need one at the end of your pipeline, this will work fine, but it also lets you use multiple after each stage that needs it.如果您只在管道末端需要一个,这将很好,但它也允许您在需要它的每个阶段之后使用多个。 Here's an example from the docs:这是文档中的一个示例:
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
The job in the build
stage runs whatever's necessary to build the project, but if anything fails, the job in the cleanup_build
stage will run and remove any build artifacts, or whatever else is needed. build
阶段的作业运行构建项目所需的一切,但如果有任何失败, cleanup_build
阶段的作业将运行并删除任何构建工件,或任何其他需要的东西。
If the build job passes, the test stage job runs the tests.如果构建作业通过,测试阶段作业将运行测试。 If these pass, we don't have to do any cleanup so the pipeline would just fail immediately.如果这些都通过了,我们就不需要做任何清理工作,这样管道就会立即失败。 Otherwise, we continue to the deploy stage.否则,我们继续部署阶段。
Our deploy stage job is marked with when: manual
which is another useful option for your pipelines.我们的部署阶段作业标记为when: manual
,这是您的管道的另一个有用选项。 It means that this job will not run until either a Gitlab user or API hits a button to start the job.这意味着该作业将不会运行,直到 Gitlab 用户或 API 点击按钮开始作业。
Our last job in the cleanup
stage has when: always
.我们在cleanup
阶段的最后一项工作是when: always
。 This means that no matter what happens in any previous stage, it will always run.这意味着无论之前的任何阶段发生什么,它都会一直运行。 This is great for a final cleanup, and if a deployment fails we could perform a rollback or anything else we might need.这对于最终清理非常有用,如果部署失败,我们可以执行回滚或我们可能需要的任何其他操作。
The docs for the when
keyword and all its options are here: https://docs.gitlab.com/ee/ci/yaml/README.html#when when
关键字及其所有选项的文档在这里: https://docs.gitlab.com/ee/ci/yaml/README.html#when
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.