diff --git a/.drone.yml b/.drone.yml index 3706bf5..f01266f 100644 --- a/.drone.yml +++ b/.drone.yml @@ -1,39 +1,21 @@ kind: pipeline type: docker -name: build-go +name: build steps: -- name: Build service +- name: Build Go app 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: + - 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 - privileged: true when: branch: - main + privileged: true volumes: - name: manifest path: docker @@ -46,26 +28,11 @@ steps: from_secret: docker_hub_user 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: branch: - main @@ -83,26 +50,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 -- build-go image_pull_secrets: - custom_mirror_registry @@ -126,6 +73,23 @@ steps: api_key: from_secret: drone_api_key base_url: https://git.parravidales.es + +depends_on: +- build + +image_pull_secrets: +- custom_mirror_registry + +trigger: + event: + - tag + +--- +kind: pipeline +type: docker +name: notify + +steps: - name: send ntfy notification image: registry.cuzo.dev/parrazam/drone-ntfy when: @@ -134,18 +98,12 @@ steps: url: https://ntfy.parravidales.es topic: pipelines priority: low - tags: release - username: - from_secret: ntfy_user - password: - from_secret: ntfy_password - -depends_on: -- manifest + token: + from_secret: ntfy_token image_pull_secrets: - custom_mirror_registry -trigger: - event: - - tag +depends_on: +- build +- release diff --git a/Dockerfile b/Dockerfile index 2325933..3f9d75a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/cmd/drone-ntfy/config/config.go b/cmd/drone-ntfy/config/config.go index 803249d..d63380b 100644 --- a/cmd/drone-ntfy/config/config.go +++ b/cmd/drone-ntfy/config/config.go @@ -2,7 +2,7 @@ package config type App struct { BaseUrl string `env:"PLUGIN_URL" envDefault:"https://ntfy.sh"` - Topic string `env:"PLUGIN_TOPIC" envDefault:"undefined"` + Topic string `env:"PLUGIN_TOPIC,required"` Username string `env:"PLUGIN_USERNAME"` Password string `env:"PLUGIN_PASSWORD"` Token string `env:"PLUGIN_TOKEN"` @@ -21,6 +21,7 @@ type Drone struct { 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 { diff --git a/cmd/drone-ntfy/main.go b/cmd/drone-ntfy/main.go index 48c9400..185c65f 100644 --- a/cmd/drone-ntfy/main.go +++ b/cmd/drone-ntfy/main.go @@ -32,7 +32,7 @@ func main() { func loadCfg() Config { - envErr := godotenv.Load(".env") + envErr := godotenv.Load() if envErr != nil { fmt.Printf("unable to load .env file: %e\n", envErr) } @@ -64,7 +64,7 @@ func updateAppConfig(cfg *Config) { cfg.App.Tags = append(cfg.App.Tags, cfg.Drone.Tag) cfg.App.Message = "Tag " + cfg.Drone.Tag + " created" } else { - cfg.App.Tags = append(cfg.App.Tags, "branch "+cfg.Ci.CommitBranch) + cfg.App.Tags = append(cfg.App.Tags, cfg.Drone.RepoName+"/"+cfg.Ci.CommitBranch) cfg.App.Message = "[" + cfg.Ci.CommitSha[0:8] + "] " + cfg.Ci.CommitMessage } } @@ -83,7 +83,7 @@ func addTagsBasedOnResult(cfg *Config) { func sendNotification(cfg *Config) { req, _ := http.NewRequest("POST", - cfg.App.BaseUrl + "/" + cfg.App.Topic, + cfg.App.BaseUrl+"/"+cfg.App.Topic, strings.NewReader(cfg.App.Message)) if cfg.App.Token != "" { @@ -94,7 +94,7 @@ func sendNotification(cfg *Config) { req.Header.Set("Title", cfg.App.Title) req.Header.Set("Priority", cfg.App.Priority) req.Header.Set("Tags", strings.Join(cfg.App.Tags, ",")) - req.Header.Set("Actions", "view, Build, "+cfg.Drone.BuildLink+"; view, Changes, "+cfg.Drone.CommitLink) + req.Header.Set("Actions", getActions(cfg)) res, err := http.DefaultClient.Do(req) if err != nil { log.Fatalf("error trying to notify the result. Error: %+v", err) @@ -103,3 +103,11 @@ func sendNotification(cfg *Config) { log.Fatalf("error from server. HTTP status: %d. Error: %e", res.StatusCode, err) } } + +func getActions(cfg *Config) string { + var buildLink = "view, Build, " + cfg.Drone.BuildLink + if strings.Contains(cfg.Ci.CommitRef, "refs/tags/") { + return buildLink + } + return buildLink + "; view, Changes, " + cfg.Drone.CommitLink +}