commit 245ec2d25128d9adae22a5f0e3cd295c161e4193 Author: Evann Regnault Date: Sat May 4 16:49:10 2024 +0200 First commit diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..d92f03f --- /dev/null +++ b/.drone.yml @@ -0,0 +1,17 @@ +kind: pipeline +name: build +type: docker + +steps: + - name: build + image: plugins/docker + settings: + registry: r.regnault.dev + username: + from_secret: DOCKER_USERNAME + password: + from_secret: DOCKER_PASSWORD + repo: r.regnault.dev/drone-discord-webhook + dockerfile: docker/Dockerfile + context: src/ + force_tag: true \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..caa7347 --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +# Drone Discord Webhook + +This is a plugin for the [Drone CI/CD](https://www.drone.io/) tool which purpose is to post messages after each builds completed or failed. + +## How to use it? + +There are 3 environment variable that can be set. + +(Required) `WEBHOOK_URL`: The url of the webhook that will send the status message + +(Optionnal) `AVATAR`: Url of an image that'll be used for the webhook user. + +(Optionnal) `USERNAME`: Username of the avatar that'll be assigned to the webhook. + +```yaml +- name: Send discord notification + image: r.regnault.dev/drone-discord-webhook + environment: + AVATAR: "Optionnal webhook avatar url" + USERNAME: "Optionnal webhook username" + WEBHOOK_URL: + from_secret: WEBHOOK_URL + when: + status: [ success, failure, changed ] +``` + +## How does it look? + +![Screen showing the embed appearance of the webhook message](images/image.png) \ No newline at end of file diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..4faa9f8 --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,8 @@ +FROM alpine +RUN apk add jq curl bash + +COPY . /app +WORKDIR /app +RUN chmod +x launch.sh + +ENTRYPOINT [ "/bin/bash", "/app/launch.sh" ] \ No newline at end of file diff --git a/images/image.png b/images/image.png new file mode 100644 index 0000000..7956a49 Binary files /dev/null and b/images/image.png differ diff --git a/src/launch.sh b/src/launch.sh new file mode 100644 index 0000000..54c72db --- /dev/null +++ b/src/launch.sh @@ -0,0 +1,37 @@ +#/bin/sh + +# List +cd /app +DRONE_COMMIT_SHA=$(head -c 8 <<< $DRONE_COMMIT_SHA) + +USERNAME=${USERNAME:-"DRONE CI/CD"} +AVATAR=${AVATAR:-"https://github.com/drone/brand/blob/master/logos/png/with-background/drone-logo-white-with-background@2x.png?raw=true"} + +#Init +cp template.json build.json + +#Build json +if [ "$DRONE_BUILD_STATUS" = "success" ]; then + sed -i "s/\$COLOR/3795747/g" build.json + sed -i "s/\$BUILD_STATE/Success/g" build.json +elif [ "$DRONE_BUILD_STATUS" = "failure" ]; then + sed -i "s/\$COLOR/15409955/g" build.json + sed -i "s/\$BUILD_STATE/Failed/g" build.json +fi + +sed -i "s^\$COMMIT_MESSAGE^$(echo $DRONE_COMMIT_MESSAGE)^g" build.json +sed -i "s^\$WORKFLOW_URL^$DRONE_BUILD_LINK^g" build.json +sed -i "s^\$COMMIT_USER^$DRONE_COMMIT_AUTHOR^g" build.json +sed -i "s^\$COMMIT_HASH^$DRONE_COMMIT_SHA^g" build.json +sed -i "s^\$COMMIT_LINK^$DRONE_COMMIT_LINK^g" build.json +sed -i "s^\$EVENT^$DRONE_BUILD_EVENT^g" build.json +sed -i "s^\$BRANCH^$DRONE_REPO_BRANCH^g" build.json +sed -i "s^\$REPO_NAME^$DRONE_REPO^g" build.json +sed -i "s^\$REPO_LINK^$DRONE_REPO_LINK^g" build.json +sed -i "s^\$JOB_NAME^$DRONE_STAGE_NAME^g" build.json +sed -i "s^\$TIMESTAMP^$(date -Iseconds -d @$DRONE_BUILD_FINISHED)^g" build.json +sed -i "s^\$USERNAME^$USERNAME^g" build.json +sed -i "s^\$AVATAR^$AVATAR^g" build.json + +# Curl +curl -X POST -H "Content-Type: application/json" -d "$(cat build.json)" $WEBHOOK_URL \ No newline at end of file diff --git a/src/template.json b/src/template.json new file mode 100644 index 0000000..fe0e9ee --- /dev/null +++ b/src/template.json @@ -0,0 +1,44 @@ +{ + "content": "", + "tts": false, + "embeds": [ + { + "color": $COLOR, + "fields": [ + { + "name": "Repository", + "value": "[$REPO_NAME]($REPO_LINK)", + "inline": true + }, + { + "name": "Ref", + "value": "$BRANCH", + "inline": true + }, + { + "name": "Event - $EVENT", + "value": " [`$COMMIT_HASH`]($COMMIT_LINK) $COMMIT_MESSAGE", + "inline": false + }, + { + "name": "Triggered by", + "value": "$COMMIT_USER", + "inline": true + }, + { + "name": "Workflow", + "value": "[Drone link]($WORKFLOW_URL)", + "inline": true + } + ], + "author": { + "name": "$BUILD_STATE: $JOB_NAME" + }, + "timestamp": "$TIMESTAMP" + } + ], + "components": [], + "actions": {}, + "username": "$USERNAME", + "avatar_url": "$AVATAR" + } \ No newline at end of file