forked from gadiener/ci-templates
-
Notifications
You must be signed in to change notification settings - Fork 5
/
notify.yml
76 lines (66 loc) · 2.23 KB
/
notify.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
publish:calendar:
image: europe-west1-docker.pkg.dev/jobtome-platform/public/calendar-publisher:v0.0.7
stage: notify
script:
- python3 /app/entrypoint.py
only:
- /^v.+$/i
except:
- branches
publish:slack:
image: europe-west1-docker.pkg.dev/jobtome-platform/public/ci-image-gcp:v1.6.0
stage: notify
script:
- |
if [ -z "${SLACK_WEBHOOK_URL}" ]; then
echo
echo "-> [WARNING] Missing 'SLACK_WEBHOOK_URL' variable!"
echo
exit 1
fi
if [ -z "${SLACK_ICON}" ]; then
PICTURE="\"icon_url\": \"https://img.icons8.com/color/2x/gitlab.png\""
else
PICTURE="\"icon_emoji\": \":shipit:\""
fi
if [ -z "${SLACK_USERNAME}" ]; then
SLACK_USERNAME="CI notifier"
fi
git tag -l "${CI_COMMIT_TAG}" --format='%(subject)' | tr -d '\r' > /tmp/msg.txt
cat <<EOF > /tmp/attachment.txt
{
"color": "#37FDFC",
"author_name": "${GITLAB_USER_LOGIN}",
"title": "${CI_PROJECT_NAME} ${CI_COMMIT_TAG}",
"title_link": "${CI_PIPELINE_URL}",
"text": "$(cat /tmp/msg.txt | sed 's/"/\\"/g')",
"fields": [
{
"title": "Completed at:",
"value": "$(date +"%d %b %H:%M %Z")",
"short": "false"
}
],
"footer": "${SLACK_NOTIFICATION_FOOTER:My-company_pipeline}",
"footer_icon": "${SLACK_NOTIFICATION_ICON:https://example.com/images/favicon.png}",
"fallback": "Deployment of ${CI_PROJECT_NAME} ${CI_COMMIT_TAG} completed\n \nMore info: <${CI_PIPELINE_URL}|${CI_PIPELINE_URL}>"
}
EOF
ATTACHMENT=" \"attachments\": [$(cat /tmp/attachment.txt)]"
PAYLOAD="payload={${ATTACHMENT}, ${PICTURE}, \"username\": \"${SLACK_USERNAME}\" }"
STATUS_CODE=$(curl -sS -o /tmp/respose.txt --write-out "%{http_code}" -X POST --data-urlencode "${PAYLOAD}" "${SLACK_WEBHOOK_URL}")
if [[ "${STATUS_CODE}" -ne 200 ]] ; then
echo "Slack webhook return '${STATUS_CODE}' error!"
echo
echo "Response:"
cat /tmp/respose.txt
echo
echo "Payload: ${PAYLOAD}"
exit 1
fi
echo "Message sent to Slack!"
when: on_success
only:
- /^v.+$/i
except:
- branches