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.
- Create separate repo e.g. called
ci-templates
- 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.
- 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.