How to send GitLab only schedule pipeline fail email?
Asked Answered
J

3

10

I wanted to send an email when schedule pipeline is failed. I have configure Project -> Settings -> Integrations -> Pipelines emails but these are sending emails for all the pipelines.

I want to apply Pipeline Emails only for schedule ones.

Could you please suggest any solution.

Jaclyn answered 15/3, 2019 at 8:59 Comment(0)
U
1

Looks like this isn't possible in GitLab. There is an issue about it here: https://gitlab.com/gitlab-org/gitlab/-/issues/220059

You probably have to turn off the automated GitLab emails entirely and then set up something manually in the schedule pipelines to email you, which is quite a drag but I guess should work.

Uptown answered 22/11, 2023 at 4:46 Comment(0)
N
1

It's impossible in GitLab. However, you can achieve this through the [unfortunately complicated] CI template. In my case, I needed to send a Mircosoft Teams notification when the pipeline failed (or succeeded).

Disclaimer: I'm not claiming the best answer.

  1. Create separate repo e.g. called ci-templates
  2. Create file ./teams/notifications.yml and add something like:
---

.notification_template:
  script: |
    # (preparing @payload.json is emitted)
    curl -H 'Content-Type: application/json' -d @payload.json $TEAMS_WEBHOOK_URL 

.pipeline_failed:
  extends:
    - .notification_template
  rules:
   - when: on_failure
     if: '$CI_COMMIT_TAG =~ /^[0-9]+\.[0-9]+\.[0-9]+$/'
     variables:
       STATUS: "failed"
       ICON: "❌"
   - when: never

.pipeline_succeed:
  extends:
    - .notification_template
  rules:
   - when: on_success
     if: '$CI_COMMIT_TAG =~ /^[0-9]+\.[0-9]+\.[0-9]+$/'
     variables:
       STATUS: "success"
       ICON: "✅"
   - when: never

.pipeline_failed_tpl:
  extends:
    - .pipeline_failed

.pipeline_succeed_tpl:
  extends:
    - .pipeline_succeed

In .notification_template add code that will send the notification you want manually. In my case, this is Teams webhook with curl triggering it. It works for tags only. In your case, it will be email stuff and your rules.

  1. In a .gitlab-ci.yml file of your repo, for each repo, sadly, you have to add as a last stage the following:
include:
 - project: 'dt/ci-templates'
   ref: stable
   file: 
    - 'teams/notifications.yaml'

# (your main CI code is emmited)

teams:notifications:failed:
  stage: teams:notifications # this should be a last stage
  extends:
    - .pipeline_failed_tpl

teams:notifications:success:
  stage: teams:notifications
  extends:
    - .pipeline_succeed_tpl

So when your pipeline fails, the on_failure job will work, sending curl (or any script you want, including email sending) that will send a custom notification.


You can guess the drawbacks of this approach, but it also has benefits. You can customize notifications as you want and send them whenever you want. For example, I send different notifications depending on the project. See my full notification template: https://pastebin.com/raw/cpQvi45V.

Nettles answered 3/12, 2023 at 10:16 Comment(0)
P
-2

I know this is an old post, but posting a solution in case someone else is looking for it.

Have you tried this option? This is available under CI/CD settings of the Gitlab project you've your pipeline scheduled.

enter image description here

Photoactinic answered 14/7, 2020 at 12:5 Comment(3)
Could you provide more details on what you mean by "this options"? It is not clear as to how different from what OP has already tried.Debauched
But this sends emails for all pipelines that failed... not pipelines that are scheduled. OP and I want to only send emails if scheduled jobs fail...Wrangle
apologies, I must have gone by the title. I was showing the option for notifying only when a pipeline failed - "Notify only on broken pipelines"Photoactinic

© 2022 - 2024 — McMap. All rights reserved.