Migrate project to Go #1

Merged
parra merged 5 commits from migrate-to-go into main 2023-02-23 21:04:26 +01:00
4 changed files with 45 additions and 79 deletions
Showing only changes of commit 6aed161156 - Show all commits

View file

@ -1,39 +1,21 @@
kind: pipeline kind: pipeline
type: docker type: docker
name: build-go name: build
steps: steps:
- name: Build service - name: Build Go app
image: registry.cuzo.dev/library/golang image: registry.cuzo.dev/library/golang
volumes: volumes:
- name: deps - name: deps
path: /go path: /go
commands: commands:
- go build -o bin/ cmd/drone-ntfy/main.go - GOOS=linux GOARCH=amd64 CGO_ENABLED=0 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 - name: Build docker image
image: registry.cuzo.dev/plugins/docker image: registry.cuzo.dev/plugins/docker
privileged: true
when: when:
branch: branch:
- main - main
privileged: true
volumes: volumes:
- name: manifest - name: manifest
path: docker path: docker
@ -46,26 +28,11 @@ steps:
from_secret: docker_hub_user from_secret: docker_hub_user
password: password:
from_secret: docker_hub_pass 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 - name: Upload manifest
image: registry.cuzo.dev/plugins/manifest image: registry.cuzo.dev/plugins/manifest
privileged: true privileged: true
depends_on:
- Build docker image
when: when:
branch: branch:
- main - main
@ -83,26 +50,6 @@ steps:
from_secret: docker_hub_pass from_secret: docker_hub_pass
platforms: platforms:
- linux/amd64 - 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: image_pull_secrets:
- custom_mirror_registry - custom_mirror_registry
@ -126,6 +73,23 @@ steps:
api_key: api_key:
from_secret: drone_api_key from_secret: drone_api_key
base_url: https://git.parravidales.es 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 - name: send ntfy notification
image: registry.cuzo.dev/parrazam/drone-ntfy image: registry.cuzo.dev/parrazam/drone-ntfy
when: when:
@ -134,18 +98,12 @@ steps:
url: https://ntfy.parravidales.es url: https://ntfy.parravidales.es
topic: pipelines topic: pipelines
priority: low priority: low
tags: release token:
username: from_secret: ntfy_token
from_secret: ntfy_user
password:
from_secret: ntfy_password
depends_on:
- manifest
image_pull_secrets: image_pull_secrets:
- custom_mirror_registry - custom_mirror_registry
trigger: depends_on:
event: - build
- tag - release

View file

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

View file

@ -2,7 +2,7 @@ package config
type App struct { type App struct {
BaseUrl string `env:"PLUGIN_URL" envDefault:"https://ntfy.sh"` 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"` Username string `env:"PLUGIN_USERNAME"`
Password string `env:"PLUGIN_PASSWORD"` Password string `env:"PLUGIN_PASSWORD"`
Token string `env:"PLUGIN_TOKEN"` Token string `env:"PLUGIN_TOKEN"`
@ -21,6 +21,7 @@ type Drone struct {
Tag string `env:"DRONE_TAG"` Tag string `env:"DRONE_TAG"`
BuildLink string `env:"DRONE_BUILD_LINK"` BuildLink string `env:"DRONE_BUILD_LINK"`
CommitLink string `env:"DRONE_COMMIT_LINK"` CommitLink string `env:"DRONE_COMMIT_LINK"`
RepoName string `env:"DRONE_REPO_NAME"`
} }
type Ci struct { type Ci struct {

View file

@ -32,7 +32,7 @@ func main() {
func loadCfg() Config { func loadCfg() Config {
envErr := godotenv.Load(".env") envErr := godotenv.Load()
if envErr != nil { if envErr != nil {
fmt.Printf("unable to load .env file: %e\n", envErr) 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.Tags = append(cfg.App.Tags, cfg.Drone.Tag)
cfg.App.Message = "Tag " + cfg.Drone.Tag + " created" cfg.App.Message = "Tag " + cfg.Drone.Tag + " created"
} else { } 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 cfg.App.Message = "[" + cfg.Ci.CommitSha[0:8] + "] " + cfg.Ci.CommitMessage
} }
} }
@ -83,7 +83,7 @@ func addTagsBasedOnResult(cfg *Config) {
func sendNotification(cfg *Config) { func sendNotification(cfg *Config) {
req, _ := http.NewRequest("POST", req, _ := http.NewRequest("POST",
cfg.App.BaseUrl + "/" + cfg.App.Topic, cfg.App.BaseUrl+"/"+cfg.App.Topic,
strings.NewReader(cfg.App.Message)) strings.NewReader(cfg.App.Message))
if cfg.App.Token != "" { if cfg.App.Token != "" {
@ -94,7 +94,7 @@ func sendNotification(cfg *Config) {
req.Header.Set("Title", cfg.App.Title) req.Header.Set("Title", cfg.App.Title)
req.Header.Set("Priority", cfg.App.Priority) req.Header.Set("Priority", cfg.App.Priority)
req.Header.Set("Tags", strings.Join(cfg.App.Tags, ",")) 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) res, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {
log.Fatalf("error trying to notify the result. Error: %+v", err) 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) 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
}