2023-02-09 17:06:27 +01:00
|
|
|
#!/bin/sh
|
|
|
|
|
2023-02-09 17:11:55 +01:00
|
|
|
DEFAULT_TAGS="drone"
|
2023-02-09 17:06:27 +01:00
|
|
|
BASE_URL=${PLUGIN_URL:-"https://ntfy.sh"}
|
|
|
|
TOPIC=$PLUGIN_TOPIC # mandatory
|
|
|
|
TITLE="Build #$DRONE_BUILD_NUMBER $DRONE_BUILD_STATUS"
|
2023-02-09 17:11:55 +01:00
|
|
|
PRIORITY=${PLUGIN_PRIORITY:-"default"}
|
2023-02-09 17:06:27 +01:00
|
|
|
TAGS=${PLUGIN_TAGS:-""}
|
|
|
|
|
|
|
|
MESSAGE="[${CI_COMMIT_SHA:0:8}] $CI_COMMIT_MESSAGE"
|
|
|
|
|
2023-02-09 17:11:55 +01:00
|
|
|
if [[ $CI_COMMIT_REF == *"refs/tags/"* ]]
|
|
|
|
then
|
|
|
|
MESSAGE="Tag $DRONE_TAG created"
|
|
|
|
DEFAULT_TAGS="$DEFAULT_TAGS,$DRONE_TAG"
|
|
|
|
else
|
|
|
|
DEFAULT_TAGS="$DEFAULT_TAGS,$CI_REPO_NAME/$CI_COMMIT_BRANCH"
|
|
|
|
fi
|
|
|
|
|
2023-02-09 17:06:27 +01:00
|
|
|
if [ -z "$TOPIC" ]
|
|
|
|
then
|
|
|
|
echo "Topic cannot be empty.";
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
URL="$BASE_URL/$TOPIC"
|
|
|
|
|
|
|
|
if [ $DRONE_BUILD_STATUS = "success" ]
|
|
|
|
then
|
|
|
|
DEFAULT_TAGS="$DEFAULT_TAGS,white_check_mark"
|
|
|
|
elif [ $DRONE_BUILD_STATUS = "failure" ]
|
|
|
|
then
|
|
|
|
DEFAULT_TAGS="$DEFAULT_TAGS,x"
|
|
|
|
else
|
|
|
|
DEFAULT_TAGS="$DEFAULT_TAGS,grey_question"
|
|
|
|
fi
|
|
|
|
|
|
|
|
TAGS="$DEFAULT_TAGS,$TAGS"
|
|
|
|
TAGS=${TAGS%,}
|
|
|
|
|
2023-02-09 17:11:55 +01:00
|
|
|
AUTH_HEADER=""
|
|
|
|
if [ -z "$PLUGIN_USERNAME" ] || [ -z "$PLUGIN_PASSWORD" ]
|
|
|
|
then
|
|
|
|
echo "Trying to publish message in a public topic..."
|
|
|
|
else
|
|
|
|
AUTH_HEADER="-u $PLUGIN_USERNAME:$PLUGIN_PASSWORD"
|
|
|
|
fi
|
|
|
|
|
2023-02-09 17:06:27 +01:00
|
|
|
HTTP_STATUS=$(
|
|
|
|
curl \
|
|
|
|
-o /dev/null -s -w "%{http_code}\n" \
|
|
|
|
--retry 3 --retry-delay 5 \
|
2023-02-09 17:11:55 +01:00
|
|
|
$AUTH_HEADER \
|
2023-02-09 17:06:27 +01:00
|
|
|
-H title:"$TITLE" \
|
|
|
|
-H tags:$TAGS \
|
|
|
|
-H prio:$PRIORITY \
|
|
|
|
-H "Actions: view, Build, $DRONE_BUILD_LINK; view, Changes, $DRONE_COMMIT_LINK" \
|
|
|
|
-d "$MESSAGE" \
|
|
|
|
$URL
|
|
|
|
)
|
|
|
|
|
|
|
|
if [ $? -eq 0 ] && [ $HTTP_STATUS -eq 200 ]
|
|
|
|
then
|
|
|
|
echo "Message sent!"
|
|
|
|
else
|
|
|
|
echo "Error publishing notification."
|
|
|
|
exit 2
|
|
|
|
fi
|