From 2900a3d40848b94d4621ec8d96d4a848f5a24155 Mon Sep 17 00:00:00 2001 From: parra Date: Thu, 23 Feb 2023 16:31:35 +0100 Subject: [PATCH] Migrate script to Go --- .drone.yml | 36 ++++++++++++++++++++++++++++-- .gitignore | 2 ++ cmd/drone-ntfy/main.go | 50 ++++++++++++++++++++++++++++++++++++++++++ go.mod | 8 +++++++ go.sum | 4 ++++ 5 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 cmd/drone-ntfy/main.go create mode 100644 go.mod create mode 100644 go.sum diff --git a/.drone.yml b/.drone.yml index 53cd1f2..3706bf5 100644 --- a/.drone.yml +++ b/.drone.yml @@ -1,11 +1,39 @@ kind: pipeline type: docker -name: build +name: build-go + +steps: +- name: Build service + image: registry.cuzo.dev/library/golang + volumes: + - name: deps + path: /go + commands: + - go build -o bin/ cmd/drone-ntfy/main.go + - go run cmd/drone-ntfy/main.go + +image_pull_secrets: +- custom_mirror_registry + +trigger: + event: + - push + - tag + exclude: + - pull_request + +--- +kind: pipeline +type: docker +name: build-image steps: - name: Build docker image image: registry.cuzo.dev/plugins/docker privileged: true + when: + branch: + - main volumes: - name: manifest path: docker @@ -38,6 +66,9 @@ steps: - name: Upload manifest image: registry.cuzo.dev/plugins/manifest privileged: true + when: + branch: + - main volumes: - name: manifest path: docker @@ -70,7 +101,8 @@ steps: from_secret: ntfy_password depends_on: -- build +- build-image +- build-go image_pull_secrets: - custom_mirror_registry diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..662f6d5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode/ +bin/ diff --git a/cmd/drone-ntfy/main.go b/cmd/drone-ntfy/main.go new file mode 100644 index 0000000..141e51a --- /dev/null +++ b/cmd/drone-ntfy/main.go @@ -0,0 +1,50 @@ +package main + +import ( + "fmt" + "log" + "os" + + "github.com/caarlos0/env/v7" +) + +type Config struct { + BaseUrl string `env:"PLUGIN_BASE_URL" envDefault:"https://ntfy.sh"` + Topic string `env:"PLUGIN_TOPIC,required"` + Title string `env:"PLUGIN_TITLE" envDefault:"Drone notification"` + Priority string `env:"PLUGIN_PRIORITY" envDefault:"default"` + Tags []string `env:"PLUGIN_TAGS" envSeparator:","` +} + +type DroneConfig struct { + BuildNumber string `envPrefix:"DRONE_"` + StageName string `envPrefix:"DRONE_"` + StageStatus string `envPrefix:"DRONE_"` +} + +type CiConfig struct { + RepoName string `envPrefix:"CI_"` + CommitMessage string `envPrefix:"CI_"` +} + +const DEFAULT_PRIORITY = "default" + +const BASE_URL = "https://ntfy.sh" +const TOPIC = "" +const TITLE = "" +const PRIORITY = "" +const TAGS = "" + +const MESSAGE = "Build $DRONE_BUILD_NUMBER of $CI_REPO_NAME at stage $DRONE_STAGE_NAME $DRONE_STAGE_STATUS.\n\nCommit: $CI_COMMIT_MESSAGE" + +func main() { + + fmt.Printf("Topic: %s\n", os.Environ()) + cfg := Config{} + err := env.Parse(&cfg) + if err != nil { + log.Fatalf("unable to parse environment variables: %e", err) + } + + fmt.Printf("Config: %v\n", cfg) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..0f35684 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module git.parravidales.es/parra/drone-ntfy + +go 1.19 + +require ( + github.com/caarlos0/env/v7 v7.0.0 // indirect + github.com/joho/godotenv v1.5.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..daef734 --- /dev/null +++ b/go.sum @@ -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=