Skip to content

Instantly share code, notes, and snippets.

@radimih
Last active March 3, 2020 10:33
Show Gist options
  • Save radimih/52e15547ee9f133fc146b3bd59de0598 to your computer and use it in GitHub Desktop.
Save radimih/52e15547ee9f133fc146b3bd59de0598 to your computer and use it in GitHub Desktop.
GitLab CI/CD: Mattermost notification
variables:
project_stand: test
mattermost_webhook: "http://mattermost.example.ru/hooks/jfdlkds74jldkfjd34dd"
stages:
- deploy
deploy:
stage: deploy
when: manual
variables:
deploy_failed_path: "${CI_PROJECT_DIR}/.deploy_failed"
deploy_msg_begin: |
##### Начинаю деплой на стенд...
| Стенд | Pipeline / Job | Ветка / Коммит / Время коммита | Инициатор |
|:-----:|:--------------:|:------------------------------:|:----------|
| **${project_stand}** :arrows_counterclockwise: | [${CI_PIPELINE_ID}](${CI_PIPELINE_URL}) / [${CI_JOB_ID}](${CI_JOB_URL}) | `${CI_COMMIT_REF_NAME}` / [${CI_COMMIT_SHORT_SHA}](${CI_PROJECT_URL}/commit/${CI_COMMIT_SHA}) / DDD | ${GITLAB_USER_NAME} |
deploy_msg_success: |
##### Деплой на стенд успешно завершен
| Стенд | Pipeline / Job |
|:-----:|:--------------:|
| **${project_stand}** :white_check_mark: | [${CI_PIPELINE_ID}](${CI_PIPELINE_URL}) / [${CI_JOB_ID}](${CI_JOB_URL}) |
deploy_msg_fail: |
##### Деплой на стенд завершился с ошибкой!
| Стенд | Ошибка |
|:-----:|:------:|
| **${project_stand}** :sos: | [Подробности](${CI_JOB_URL}) |
before_script:
- 'touch ${deploy_failed_path}'
- 'commit_date="$(git show --no-patch --format=%cd --date=format:"%Y-%m-%d %H:%M" ${CI_COMMIT_SHA})"'
- 'deploy_msg_begin="$(echo "$deploy_msg_begin" | sed -e "s/DDD/$commit_date/g")"'
- 'curl --silent -X POST -H "Content-Type: application/json" --data "{\"text\": \"${deploy_msg_begin}\"}" ${mattermost_webhook}'
script:
- 'true'
- 'rm ${deploy_failed_path}'
after_script:
- '[ -f "${deploy_failed_path}" ] && deploy_msg_final="${deploy_msg_fail}" || deploy_msg_final="${deploy_msg_success}"'
- 'curl --silent -X POST -H "Content-Type: application/json" --data "{\"text\": \"${deploy_msg_final}\"}" ${mattermost_webhook}'
tags:
- shell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment