From db34a064f5200d8db2675ae66ce31a44cec719a3 Mon Sep 17 00:00:00 2001 From: "Stanislav N. aka pztrn" Date: Wed, 16 Oct 2019 21:05:54 +0500 Subject: [PATCH] Docker building and various fixes around the code. --- .drone.yml | 60 +++++++++++++++++++++++++++++++++++++++++++++ Dockerfile | 14 +++++++++++ env/env.go | 18 +++++++------- message/payload.go | 2 +- message/prepare.go | 8 ------ message/process.go | 2 +- message/template.go | 33 +++++++++++++------------ 7 files changed, 103 insertions(+), 34 deletions(-) create mode 100644 .drone.yml create mode 100644 Dockerfile delete mode 100644 message/prepare.go diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..5dd9ad0 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,60 @@ +--- +kind: pipeline +type: docker +name: build + +steps: + - name: notify-start + image: pztrn/discordrone + settings: + webhook_id: + from_secret: discord_webhook_id + webhook_token: + from_secret: discord_webhook_secret + message: 'Starting building **{{repo.name}}#{{build.number}}@{{commit.sha}}** @ {{datetime build.started "02-Jan-2006 15:04:05 MST" "Asia/Yekaterinburg"}} (See {{build.link}} for logs).' + + - name: lint + image: golangci/golangci-lint:latest + environment: + CGO_ENABLED: 0 + commands: + - golangci-lint run + + - name: test + image: golang:1.13.1-alpine + environment: + GOFLAGS: -mod=vendor + CGO_ENABLED: 0 + commands: + - go test ./... + + - name: docker + image: plugins/docker + when: + branch: + master + settings: + username: + from_secret: dockerhub_user + password: + from_secret: dockerhub_password + repo: pztrn/discordrone + auto_tag: true + + - name: notify-end + when: + status: + - success + - failure + image: pztrn/discordrone + settings: + webhook_id: + from_secret: discord_webhook_id + webhook_token: + from_secret: discord_webhook_secret + message: " + {{#success build.status}} + **{{repo.name}}#{{build.number}}@{{commit.sha}}** built in {{since build.started}} and pushed to hub.docker.com. + {{ else }} + **{{repo.name}}#{{build.number}}@{{commit.sha}}** failed. See {{build.link}}. + {{/success}}" \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b6123ea --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM golang:1.13.1-alpine AS build + +WORKDIR /discordrone +COPY . . + +RUN CGO_ENABLED=0 go build -tags netgo -ldflags '-w -extldflags "-static"' + +FROM alpine:3.10 +LABEL maintainer "Stanislav N. " + +COPY --from=build /discordrone/discordrone /app/discordrone +RUN apk add --no-cache ca-certificates + +ENTRYPOINT [ "/app/discordrone" ] diff --git a/env/env.go b/env/env.go index 237c5ed..d9b9660 100644 --- a/env/env.go +++ b/env/env.go @@ -17,15 +17,15 @@ type environmentData struct { Drone struct { Branch string `envconfig:"optional"` Build struct { - Action string `envconfig:"optional"` - Created int `envconfig:"optional"` - Event string `envconfig:"optional"` - Finished int `envconfig:"optional"` - Link string `envconfig:"optional"` - Number int `envconfig:"optional"` - Parent string `envconfig:"optional"` - Started int `envconfig:"optional"` - Status string `envconfig:"optional"` + Action string `envconfig:"optional"` + Created float64 `envconfig:"optional"` + Event string `envconfig:"optional"` + Finished float64 `envconfig:"optional"` + Link string `envconfig:"optional"` + Number int `envconfig:"optional"` + Parent string `envconfig:"optional"` + Started float64 `envconfig:"optional"` + Status string `envconfig:"optional"` } CommitHash string `envconfig:"DRONE_COMMIT,optional"` Commit struct { diff --git a/message/payload.go b/message/payload.go index b13dad5..91cc9b2 100644 --- a/message/payload.go +++ b/message/payload.go @@ -35,7 +35,7 @@ type ( Title string `json:"title"` Description string `json:"description"` URL string `json:"url"` - Color int `json:"color"` + Color int64 `json:"color"` Footer EmbedFooterObject `json:"footer"` Author EmbedAuthorObject `json:"author"` Fields []EmbedFieldObject `json:"fields"` diff --git a/message/prepare.go b/message/prepare.go deleted file mode 100644 index 19f2127..0000000 --- a/message/prepare.go +++ /dev/null @@ -1,8 +0,0 @@ -package message - -// stdlib - -// local - -// other -//"github.com/drone/drone-template-lib/template" diff --git a/message/process.go b/message/process.go index bd67dc9..38171e8 100644 --- a/message/process.go +++ b/message/process.go @@ -40,7 +40,7 @@ func Process() error { // If message was set - format it. if len(env.Data.Plugin.Message) > 0 { - text, err := template.RenderTrim(env.Data.Plugin.Message, env.Data) + text, err := template.RenderTrim(env.Data.Plugin.Message, env.Data.Drone) if err != nil { return err } diff --git a/message/template.go b/message/template.go index 5ea2a70..94e6c6c 100644 --- a/message/template.go +++ b/message/template.go @@ -13,7 +13,7 @@ const ( // DroneIconURL default drone logo url droneIconURL = "https://c1.staticflickr.com/5/4236/34957940160_435d83114f_z.jpg" // DroneDesc default drone description - droneDesc = "Powered by DiscoDrone Plugin" + droneDesc = "Powered by DiscorDrone Plugin" ) func createEmbed() EmbedObject { @@ -54,25 +54,28 @@ func createEmbed() EmbedObject { embed.Description = description // Compose color. - var color int + var color int64 if env.Data.Plugin.Color != "" { env.Data.Plugin.Color = strings.Replace(env.Data.Plugin.Color, "#", "", -1) - if s, err := strconv.ParseInt(env.Data.Plugin.Color, 16, 32); err == nil { - color = int(s) + s, err := strconv.ParseInt(env.Data.Plugin.Color, 16, 32) + if err == nil { + color = s + } + } else { + + switch env.Data.Drone.Build.Status { + case "success": + // green + color = 0x1ac600 + case "failure", "error", "killed": + // red + color = 0xff3232 + default: + // yellow + color = 0xffd930 } } - switch env.Data.Drone.Build.Status { - case "success": - // green - color = 0x1ac600 - case "failure", "error", "killed": - // red - color = 0xff3232 - default: - // yellow - color = 0xffd930 - } embed.Color = color return embed