From 28011b3fe32ec61f76fc39b789fd839cfc4b066b Mon Sep 17 00:00:00 2001 From: parra Date: Thu, 9 Feb 2023 17:11:55 +0100 Subject: [PATCH] Create simple script to notify via Ntfy --- .drone.yml | 65 +++++++++++++++++++++++++++--------------------------- README.md | 56 ++++++++++++++++++++++++++++++++++++++++++++++ script.sh | 22 +++++++++++++++--- 3 files changed, 107 insertions(+), 36 deletions(-) create mode 100644 README.md diff --git a/.drone.yml b/.drone.yml index 1f95534..53cd1f2 100644 --- a/.drone.yml +++ b/.drone.yml @@ -12,6 +12,7 @@ steps: settings: repo: parrazam/drone-ntfy auto_tag: true + auto_tag_suffix: linux-amd64 mirror: https://registry.cuzo.dev username: from_secret: docker_hub_user @@ -42,12 +43,31 @@ steps: path: docker settings: target: parrazam/drone-ntfy + template: parrazam/drone-ntfy:OS-ARCH auto_tag: true ignore_missing: true username: from_secret: docker_hub_user password: from_secret: docker_hub_pass + platforms: + - linux/amd64 +- name: send ntfy notification + image: registry.cuzo.dev/parrazam/drone-ntfy + when: + status: [success, failure] + ref: + exclude: + - refs/tags/* + settings: + url: https://ntfy.parravidales.es + topic: pipelines + priority: low + tags: manifest + username: + from_secret: ntfy_user + password: + from_secret: ntfy_password depends_on: - build @@ -74,9 +94,19 @@ steps: api_key: from_secret: drone_api_key base_url: https://git.parravidales.es +- name: send ntfy notification + image: registry.cuzo.dev/parrazam/drone-ntfy when: - ref: - - refs/tags/* + status: [success, failure] + settings: + url: https://ntfy.parravidales.es + topic: pipelines + priority: low + tags: release + username: + from_secret: ntfy_user + password: + from_secret: ntfy_password depends_on: - manifest @@ -87,34 +117,3 @@ image_pull_secrets: trigger: event: - tag - ---- -kind: pipeline -type: docker -name: Notify result - -steps: -- name: send ntfy notification - image: registry.cuzo.dev/parrazam/drone-ntfy - settings: - url: https://ntfy.parravidales.es - topic: pipelines - priority: low - username: - from_secret: ntfy_user - password: - from_secret: ntfy_password - -depends_on: -- manifest -- release - -image_pull_secrets: -- custom_mirror_registry - -trigger: - event: - - push - - tag - exclude: - - pull_request \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..1684c2b --- /dev/null +++ b/README.md @@ -0,0 +1,56 @@ +# Drone Ntfy plugin + +[![Build Status](https://ci.cuzo.dev/api/badges/parra/drone-ntfy/status.svg)](https://ci.cuzo.dev/parra/drone-ntfy) + +The Ntfy.sh plugin posts build status messages to your selected Ntfy server. + +Example: +```yml +kind: pipeline +name: default + +steps: +- name: send ntfy notification + image: parrazam/drone-ntfy + when: + status: [success, failure] + settings: + url: https://ntfy.example.org + topic: events + priority: low + tags: + - pipeline-status + - dev + username: + from_secret: ntfy_user + password: + from_secret: ntfy_password +``` + +## Properties + +`url` *string* [optional] \ +Ntfy server. +> *Default: https://ntfy.sh* + +`topic` *string* [**REQUIRED**] \ +Topic to publish message. +> *Default: none* + +`priority` *string* [optional] \ +Priority of the notification. Values can be [min, low, default, high, max]. +> *Default: default* + +`tags` *string* [optional] \ +Custom tags to include. +> *Default: none* + +`username` *string* [optional] \ +Username with publish permissions. +> *Default: none* + +`password` *string* [optional] \ +[***SECRET RECOMMENDED***] \ +Password for username. + +> *Default: none* diff --git a/script.sh b/script.sh index ff4de84..abb3ec8 100644 --- a/script.sh +++ b/script.sh @@ -1,14 +1,22 @@ #!/bin/sh -DEFAULT_TAGS="drone,$CI_REPO_NAME/$CI_COMMIT_BRANCH" +DEFAULT_TAGS="drone" BASE_URL=${PLUGIN_URL:-"https://ntfy.sh"} TOPIC=$PLUGIN_TOPIC # mandatory TITLE="Build #$DRONE_BUILD_NUMBER $DRONE_BUILD_STATUS" -PRIORITY=${PLUGIN_PRIORITY:-""} +PRIORITY=${PLUGIN_PRIORITY:-"default"} TAGS=${PLUGIN_TAGS:-""} MESSAGE="[${CI_COMMIT_SHA:0:8}] $CI_COMMIT_MESSAGE" +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 + if [ -z "$TOPIC" ] then echo "Topic cannot be empty."; @@ -30,11 +38,19 @@ fi TAGS="$DEFAULT_TAGS,$TAGS" TAGS=${TAGS%,} +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 + HTTP_STATUS=$( curl \ -o /dev/null -s -w "%{http_code}\n" \ --retry 3 --retry-delay 5 \ - -u $PLUGIN_USERNAME:$PLUGIN_PASSWORD \ + $AUTH_HEADER \ -H title:"$TITLE" \ -H tags:$TAGS \ -H prio:$PRIORITY \