简体   繁体   English

条件 after_script in.gitlab-ci.yml

[英]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_jobemail_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.

相关问题 .gitlab-ci.yml after_script 部分:如何判断任务是成功还是失败? - .gitlab-ci.yml after_script section: how can I tell whether the task succeeded or failed? gitlab-ci.yml:'脚本:-pytest 找不到任何要检查的测试' - gitlab-ci.yml: 'script: -pytest cannot find any tests to check' 是否可以在.gitlab-ci.yml 中使用动态变量? - Is it possible to use dynamic variables in .gitlab-ci.yml? 使用 PWD 在.gitlab-ci.yml 中定义变量时出错 - Error using the PWD to define variables in .gitlab-ci.yml 使用 gitlab-ci.yml 中的 awk 命令杀死 docker 容器 - Kill a docker container using awk command in gitlab-ci.yml 用于编译 Go 二进制文件的 Simple.gitlab-ci.yml 文件 - Simple .gitlab-ci.yml file for compiling a Go binary 如何制作一个简单的 GitLab Ci/CD gitlab-ci.yml 文件来构建 Angular 项目? - How to make a simple GitLab Ci/CD gitlab-ci.yml file to build an Angular project? 如何在 windows 上使用 gitlab-ci-multi-runner 访问 gitlab-ci.yml 中的变量 - How to access variables in gitlab-ci.yml using gitlab-ci-multi-runner on windows 如何在没有 GitLab Runner 的远程服务器上使用 gitlab-ci.yml 自动部署? - How can i auto deploy with gitlab-ci.yml on a remote server without GitLab Runner? 如何根据.gitlab-ci.yml 中所做的更改跳过工作? - How can I skip a job based on the changes made in .gitlab-ci.yml?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM