Compare commits

...

5 Commits
0.1.0 ... main

Author SHA1 Message Date
parra 7dc1ee294d [Fix] Split repo name from build in title (#4)
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details
Reviewed-on: #4
Co-authored-by: parra <victor+git@parravidales.es>
Co-committed-by: parra <victor+git@parravidales.es>
2023-10-28 18:14:53 +02:00
parra 04a45d0c58 Add Repo Name in notification title (#3)
continuous-integration/drone/push Build is passing Details
Reviewed-on: #3
Co-authored-by: parra <victor+git@parravidales.es>
Co-committed-by: parra <victor+git@parravidales.es>
2023-10-28 18:12:13 +02:00
parra 9f86b72ce9 Naming refactor
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details
2023-03-15 20:38:50 +01:00
parra a628f978e6 Log when .env file is loaded
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details
2023-02-23 21:39:10 +01:00
parra 7f102790fa Migrate project to Go
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details
Reviewed-on: #1
2023-02-23 21:30:10 +01:00
9 changed files with 241 additions and 126 deletions

View File

@ -3,8 +3,24 @@ type: docker
name: build
steps:
- name: Build Go app
image: registry.cuzo.dev/library/golang
volumes:
- name: deps
path: /go
commands:
- go mod tidy
- GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o bin/ cmd/drone-ntfy/main.go
- name: Build docker image
image: registry.cuzo.dev/plugins/docker
depends_on:
- Build Go app
when:
ref:
include:
- refs/heads/main
- refs/tags/**
privileged: true
volumes:
- name: manifest
@ -19,25 +35,16 @@ steps:
password:
from_secret: docker_hub_pass
image_pull_secrets:
- custom_mirror_registry
trigger:
event:
- push
- tag
exclude:
- pull_request
---
kind: pipeline
type: docker
name: manifest
steps:
- name: Upload manifest
image: registry.cuzo.dev/plugins/manifest
privileged: true
depends_on:
- Build docker image
when:
ref:
include:
- refs/heads/main
- refs/tags/**
volumes:
- name: manifest
path: docker
@ -52,25 +59,6 @@ steps:
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
image_pull_secrets:
- custom_mirror_registry
@ -82,6 +70,7 @@ trigger:
exclude:
- pull_request
---
kind: pipeline
type: docker
@ -94,22 +83,9 @@ 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:
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
- build
image_pull_secrets:
- custom_mirror_registry
@ -117,3 +93,26 @@ image_pull_secrets:
trigger:
event:
- tag
---
kind: pipeline
type: docker
name: notify
steps:
- name: send ntfy notification
image: registry.cuzo.dev/parrazam/drone-ntfy
settings:
url: https://ntfy.parravidales.es
topic: pipelines
priority: low
token:
from_secret: ntfy_token
image_pull_secrets:
- custom_mirror_registry
depends_on:
- build
- release

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.vscode/
bin/
.env

View File

@ -1,5 +1,4 @@
FROM alpine
ADD script.sh /bin/
RUN chmod +x /bin/script.sh
RUN apk -Uuv add curl ca-certificates
ENTRYPOINT /bin/script.sh
ADD bin/main /bin/main
RUN apk -Uuv add ca-certificates
ENTRYPOINT /bin/main

View File

@ -18,13 +18,11 @@ steps:
url: https://ntfy.example.org
topic: events
priority: low
tags:
tags:
- pipeline-status
- dev
username:
from_secret: ntfy_user
password:
from_secret: ntfy_password
token:
from_secret: ntfy_token
```
## Properties
@ -54,3 +52,34 @@ Username with publish permissions.
Password for username.
> *Default: none*
`token` *string* [optional] \
[***SECRET RECOMMENDED***] \
Token to use, instead username and password.
> *Default: none*
## Development
If you want to test the project locally, first you need to create an `.env` file with:
```env
PLUGIN_TOPIC=
PLUGIN_URL=
PLUGIN_USERNAME=
PLUGIN_PASSWORD=
PLUGIN_TOKEN=
CI_COMMIT_SHA=
CI_COMMIT_MESSAGE=
CI_REPO_NAME=
CI_COMMIT_BRANCH=
CI_COMMIT_REF=
DRONE_BUILD_NUMBER=
DRONE_BUILD_STATUS=
DRONE_STAGE_NUMBER=
DRONE_STAGE_STATUS=
DRONE_TAG=
DRONE_BUILD_LINK=
DRONE_COMMIT_LINK=
DRONE_REPO_NAME=
```

111
cmd/drone-ntfy/main.go Normal file
View File

@ -0,0 +1,111 @@
package main
import (
"fmt"
"log"
"net/http"
"strings"
"git.parravidales.es/parra/drone-ntfy/pkg/model"
"github.com/caarlos0/env/v7"
"github.com/joho/godotenv"
)
type Data struct {
App model.App
Drone model.Drone
Ci model.Ci
}
func main() {
data := loadData()
buildAppData(&data)
addResultToTags(&data)
sendNotification(&data)
}
func loadData() Data {
envErr := godotenv.Load()
if envErr == nil {
fmt.Printf("loaded .env file.")
}
data := Data{
App: model.App{},
Drone: model.Drone{},
Ci: model.Ci{},
}
loadDataFromEnv(&data.App)
loadDataFromEnv(&data.Drone)
loadDataFromEnv(&data.Ci)
data.App.Tags = append(data.App.DefaultTags, data.App.Tags...)
return data
}
func loadDataFromEnv(data any) {
err := env.Parse(data)
if err != nil {
log.Fatalf("unable to parse environment variables: %e", err)
}
}
func buildAppData(data *Data) {
data.App.Title = data.Drone.RepoName + " - Build #" + data.Drone.BuildNumber + " " + data.Drone.BuildStatus
if strings.Contains(data.Ci.CommitRef, "refs/tags/") {
data.App.Tags = append(data.App.Tags, data.Drone.Tag)
data.App.Message = "Tag " + data.Drone.Tag + " created"
} else {
data.App.Tags = append(data.App.Tags, data.Drone.RepoName+"/"+data.Ci.CommitBranch)
data.App.Message = "[" + data.Ci.CommitSha[0:8] + "] " + data.Ci.CommitMessage
}
}
func addResultToTags(data *Data) {
if data.Drone.BuildStatus == "success" {
data.App.Tags = append(data.App.Tags, "white_check_mark")
} else if data.Drone.BuildStatus == "failure" {
data.App.Tags = append(data.App.Tags, "x")
} else {
data.App.Tags = append(data.App.Tags, "grey_question")
}
}
func sendNotification(data *Data) {
req, _ := http.NewRequest("POST",
data.App.BaseUrl+"/"+data.App.Topic,
strings.NewReader(data.App.Message))
if data.App.Token != "" {
req.Header.Add("Authorization", "Bearer "+data.App.Token)
} else {
req.SetBasicAuth(data.App.Username, data.App.Password)
}
req.Header.Set("Title", data.App.Title)
req.Header.Set("Priority", data.App.Priority)
req.Header.Set("Tags", strings.Join(data.App.Tags, ","))
req.Header.Set("Actions", getActions(data))
res, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatalf("error trying to notify the result. Error: %+v", err)
}
if res.StatusCode != 200 {
log.Fatalf("error from server. HTTP status: %d. Error: %e", res.StatusCode, err)
}
}
func getActions(data *Data) string {
var buildLink = "view, Build, " + data.Drone.BuildLink
if strings.Contains(data.Ci.CommitRef, "refs/tags/") {
return buildLink
}
return buildLink + "; view, Changes, " + data.Drone.CommitLink
}

8
go.mod Normal file
View File

@ -0,0 +1,8 @@
module git.parravidales.es/parra/drone-ntfy
go 1.19
require (
github.com/caarlos0/env/v7 v7.0.0
github.com/joho/godotenv v1.5.1
)

4
go.sum Normal file
View File

@ -0,0 +1,4 @@
github.com/caarlos0/env/v7 v7.0.0 h1:cyczlTd/zREwSr9ch/mwaDl7Hse7kJuUY8hvHfXu5WI=
github.com/caarlos0/env/v7 v7.0.0/go.mod h1:LPPWniDUq4JaO6Q41vtlyikhMknqymCLBw0eX4dcH1E=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=

30
pkg/model/model.go Normal file
View File

@ -0,0 +1,30 @@
package model
type App struct {
BaseUrl string `env:"PLUGIN_URL" envDefault:"https://ntfy.sh"`
Topic string `env:"PLUGIN_TOPIC,required"`
Username string `env:"PLUGIN_USERNAME"`
Password string `env:"PLUGIN_PASSWORD"`
Token string `env:"PLUGIN_TOKEN"`
Title string `env:"PLUGIN_TITLE"`
Priority string `env:"PLUGIN_PRIORITY" envDefault:"default"`
Tags []string `env:"PLUGIN_TAGS" envSeparator:","`
DefaultTags []string `env:"PLUGIN_DEFAULT_TAGS" envDefault:"drone"`
Message string `env:"PLUGIN_MESSAGE"`
}
type Drone struct {
BuildNumber string `env:"DRONE_BUILD_NUMBER"`
BuildStatus string `env:"DRONE_BUILD_STATUS"`
Tag string `env:"DRONE_TAG"`
BuildLink string `env:"DRONE_BUILD_LINK"`
CommitLink string `env:"DRONE_COMMIT_LINK"`
RepoName string `env:"DRONE_REPO_NAME"`
}
type Ci struct {
CommitMessage string `env:"CI_COMMIT_MESSAGE"`
CommitSha string `env:"CI_COMMIT_SHA"`
CommitBranch string `env:"CI_COMMIT_BRANCH"`
CommitRef string `env:"CI_COMMIT_REF"`
}

View File

@ -1,68 +0,0 @@
#!/bin/sh
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:-"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.";
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%,}
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 \
$AUTH_HEADER \
-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