[英]conditional after_script in .gitlab-ci.yml
I have written few jobs in .gitlab-ci.yml
and my question is similar to this one SO Question .我在.gitlab-ci.yml
中写过一些工作,我的问题类似于这个SO Question 。 However, the answers provided and accepted doesn't work for my scenario.但是,提供和接受的答案不适用于我的场景。
The job has an after_script section which executes when the main task completes or fails.该作业有一个 after_script 部分,当主要任务完成或失败时执行。
Problem: I send an email alert based on whether the main task succeeded or failed, but I can't find any Gitlab CI variable that indicates the result of the job to clarify in the alert email.问题:我根据主要任务是成功还是失败发送了 email 警报,但我找不到任何 Gitlab CI 变量,该变量指示作业结果,以在警报 Z0C83F57C786A0B4A39EFAB23731C 中澄清。
How can I tell, inside the after_script section, whether the main task has succeeded or failed?"在 after_script 部分中,我如何判断主要任务是成功还是失败?”
If I use when: on_failure
, then my question is when can I define my when: on_success
job, since these jobs will depend on the job right before the one - so I can only execute one of these.如果我使用when: on_failure
,那么我的问题是我什么时候可以定义我的when: on_success
作业,因为这些作业将取决于之前的作业 - 所以我只能执行其中一个。 I've been trying to find variables in Gitlab Variables for this, but couldn't find.我一直试图在Gitlab 变量中找到变量,但找不到。
Also, in my after script - I can write if condition, but I am checking if someone can provide better alternate soltion另外,在我的脚本中 - 我可以写 if 条件,但我正在检查是否有人可以提供更好的替代解决方案
Problem: I send an email alert based on whether the main task succeeded or failed, but I can't find any Gitlab CI variable that indicates the result of the job to clarify in the alert email.问题:我根据主要任务是成功还是失败发送电子邮件警报,但我找不到任何 Gitlab CI 变量来指示要在警报电子邮件中澄清的作业结果。
Also, in my after script - I can write if condition, but I am checking if someone can provide better alternate solution另外,在我的后脚本中 - 我可以写 if 条件,但我正在检查是否有人可以提供更好的替代解决方案
May be you will be interested in new stage
to provide email notification job.可能您会对提供电子邮件通知工作的新stage
感兴趣。
Example of full .gitlab-ci.yml
:完整的.gitlab-ci.yml
:
stages:
- build
- notify
build_job:
stage: build
script:
- echo "Your awesome script"
allow_failure: false # it's here just clarify the workflow, it's false by default
email_alarm:
stage: notify
when: on_failure
script:
- echo "Your alarm notification to email"
email_success:
stage: notify
when: on_success
script:
- echo "Your success notification to email"
So, here it is: email_alarm
will be executed only if build_job
has been failed.所以,这里是: email_alarm
只有在build_job
失败时才会执行。 And email_success
will be executed only if build_job
succeed finished.而email_success
将仅被执行build_job
得手完成。
If you need to provide some artifacts from build_job
to email - use gitlab artifacts
in build_job
and email_alarm/email_success
jobs.如果您需要从build_job
向电子邮件提供一些工件 - 在build_job
和email_alarm/email_success
作业中使用gitlab artifacts
。
Or you can set your custom conditions such as:或者您可以设置自定义条件,例如:
scripts:
- ./script_that_fails.sh > /dev/null 2>&1 || FAILED=true
- if [ $FAILED ]
then ./do_something.sh
fi
Note the answer is another question - since v13.5, there's a CI_JOB_STATUS
variable available in after_script
.请注意,答案是另一个问题- 从 v13.5 开始,在after_script
中有一个CI_JOB_STATUS
变量可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.