Elffuss BraidHub

kikocis/EDC public

main · 4.0 KiB · executableRawPermalink
1#!/bin/bash
2# Copyright (c) 2022 Microsoft Corporation
3#
4# This program and the accompanying materials are made available under the
5# terms of the Apache License, Version 2.0 which is available at
6# https://www.apache.org/licenses/LICENSE-2.0
7#
8# SPDX-License-Identifier: Apache-2.0
9#
10# Contributors:
11# Microsoft Corporation - initial implementation
12
13# This file is intended to be used as Post-Build action on CI pipelines to post a message to the EDC's #jenkins-ci Discord
14# channel.
15# It is loosely based on https://github.com/symboxtra/universal-ci-discord-webhook/blob/master/send.sh, but with many simplifications.
16
17# todo: make configurable
18BRANCH_NAME="main"
19
20WEBHOOK_URL="$1"
21STATUS="$2"
22JENKINS_JOB="$3"
23BUILD_NUMBER="$4"
24REPO_URL="$5"
25CONTENT="$6"
26
27# do not run script if required parameters are not supplied
28if [ "$#" -lt 5 ]; then
29 echo "usage: discord_webhook.sh WEBHOOK_URL STATUS JOB_NAME BUILD_NUMBER REPO_URL CONTENT"
30 echo " WEBHOOK_URL = URL of the webhook to invoke, e.g. for discord"
31 echo " STATUS = \"success\" or \"failure\". Will use \"Unknown\" when anything else is passed"
32 echo " JOB_NAME = name of the job EXACTLY as configured in Jenkins. Use quotes if the job name contains blanks"
33 echo " BUILD_NUMBER = jenkins build number, must be an integer"
34 echo " REPO_URL = URL to the (Github) repository"
35 echo " CONTENT = [OPTIONAL] a string containing message content to be posted to. Defaults to \"I finished a job\""
36 exit 1
37fi
38
39if [ -z "$6" ]; then
40 echo "No content supplied, using default."
41 CONTENT="I finished a job"
42fi
43
44echo "'"WEBHOOK_URL: "${WEBHOOK_URL}""'"
45echo "'"STATUS: "${STATUS}""'"
46echo "'"JENKINS_JOB: "${JENKINS_JOB}""'"
47echo "'"BUILD_NUMBER: "${BUILD_NUMBER}""'"
48echo "'"REPO_URL: "${REPO_URL}""'"
49echo "'"CONTENT: "${CONTENT}""'"
50
51
52CI_PROVIDER="Jenkins"
53DISCORD_AVATAR="https://wiki.jenkins.io/download/attachments/2916393/headshot.png?version=1&modificationDate=1302753947000&api=v2"
54SUCCESS_AVATAR="https://jenkins.io/images/logos/cute/cute.png"
55FAILURE_AVATAR="https://jenkins.io/images/logos/fire/fire.png"
56UNKNOWN_AVATAR="https://www.jenkins.io/images/logos/mono/mono.png"
57
58JOB_URL="https://ci.eclipse.org/edc/job/${JENKINS_JOB}"
59BUILD_URL="${JOB_URL}/${BUILD_NUMBER}"
60BUILD_URL="${BUILD_URL}/console"
61
62echo
63echo -e "[Webhook]: ${CI_PROVIDER} CI detected."
64echo -e "[Webhook]: Sending webhook to Discord..."
65echo
66
67case ${STATUS} in
68"success")
69 EMBED_COLOR=3066993
70 STATUS_MESSAGE="Passed"
71 AVATAR="${SUCCESS_AVATAR}"
72 ;;
73"failure")
74 EMBED_COLOR=15158332
75 STATUS_MESSAGE="Failed"
76 AVATAR="${FAILURE_AVATAR}"
77 ;;
78*)
79 EMBED_COLOR=8421504
80 STATUS_MESSAGE="Status Unknown"
81 echo "status \"${STATUS}\" --> ${STATUS_MESSAGE}"
82 AVATAR="${UNKNOWN_AVATAR}"
83 ;;
84esac
85
86TIMESTAMP=$(date -u +%FT%TZ)
87WEBHOOK_DATA='{
88 "username": "Jenkins CI",
89 "content": "'"${CONTENT}"'",
90 "avatar_url": "'"${DISCORD_AVATAR}"'",
91 "embeds": [ {
92 "color": '${EMBED_COLOR}',
93 "author": {
94 "name": "'"${CI_PROVIDER}"' '"${JENKINS_JOB} #${BUILD_NUMBER}"' - '"${STATUS_MESSAGE}"'",
95 "url": "'"${BUILD_URL}"'",
96 "icon_url": "'"${AVATAR}"'"
97 },
98 "title": "'"${JENKINS_JOB} - ${STATUS_MESSAGE}"'",
99 "url": "'"${JOB_URL}"'",
100 "fields": [
101 {
102 "name": "Job Name",
103 "value": "'"[${JENKINS_JOB%}](${JOB_URL})"'",
104 "inline": true
105 },
106 {
107 "name": "Build Number",
108 "value": "'"[${BUILD_NUMBER%.*}](${BUILD_URL})"'",
109 "inline": true
110 },
111 {
112 "name": "Branch/Tag",
113 "value": "'"[\`${BRANCH_NAME}\`](${REPO_URL}/tree/${BRANCH_NAME})"'",
114 "inline": true
115 }
116 ],
117 "timestamp": "'"${TIMESTAMP}"'"
118 } ]
119}'
120
121curl --fail --progress-bar -A "${CI_PROVIDER}-Webhook" -H "Content-Type:application/json" -d "${WEBHOOK_DATA}" "${WEBHOOK_URL}"
122
123if [ $? -ne 0 ]; then
124 echo -e "Webhook data:\\n${WEBHOOK_DATA}"
125 echo -e "\\n[Webhook]: Unable to send webhook."
126
127 # Exit with an error signal
128 exit 1
129else
130 echo -e "\\n[Webhook]: Successfully sent the webhook."
131fi