From e4737635387e41d73fd71dc3afb7fb55f15de7d7 Mon Sep 17 00:00:00 2001 From: Dorian Zedler Date: Sun, 17 Jul 2022 18:28:14 +0200 Subject: [PATCH] Chore: Initial commit --- .drone.yml | 21 +++++ .drone.yml.multiarch | 60 ++++++++++++++ .gitignore | 3 + LICENSE.md | 55 +++++++++++++ README.md | 53 +++++++++++- docker/Dockerfile | 10 +++ docker/Dockerfile.linux.arm | 10 +++ docker/Dockerfile.linux.arm64 | 10 +++ docker/manifest.tmpl | 31 +++++++ go.mod | 8 ++ go.sum | 9 ++ main.go | 48 +++++++++++ plugin/pipeline.go | 149 ++++++++++++++++++++++++++++++++++ plugin/plugin.go | 25 ++++++ plugin/plugin_test.go | 11 +++ plugin/util.go | 36 ++++++++ scripts/build.sh | 18 ++++ scripts/upgrade.sh | 3 + 18 files changed, 558 insertions(+), 2 deletions(-) create mode 100644 .drone.yml create mode 100644 .drone.yml.multiarch create mode 100644 .gitignore create mode 100644 LICENSE.md create mode 100644 docker/Dockerfile create mode 100644 docker/Dockerfile.linux.arm create mode 100644 docker/Dockerfile.linux.arm64 create mode 100644 docker/manifest.tmpl create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 plugin/pipeline.go create mode 100644 plugin/plugin.go create mode 100644 plugin/plugin_test.go create mode 100644 plugin/util.go create mode 100755 scripts/build.sh create mode 100755 scripts/upgrade.sh diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..8854a7c --- /dev/null +++ b/.drone.yml @@ -0,0 +1,21 @@ +kind: pipeline +type: docker +name: default + +steps: +- name: build + image: golang + commands: + - go build + - go test ./... + +- name: publish + image: plugins/docker + settings: + repo: itsblue.dev/plugins/codeberg-pages-deploy + auto_tag: true + dockerfile: docker/Dockerfile + username: + from_secret: docker_username + password: + from_secret: docker_password diff --git a/.drone.yml.multiarch b/.drone.yml.multiarch new file mode 100644 index 0000000..116c56e --- /dev/null +++ b/.drone.yml.multiarch @@ -0,0 +1,60 @@ +# if you want to publish a multi-architecture image, replace the +# contents of the .drone.yml file with the contents of this file + +kind: pipeline +type: docker +name: default + +steps: +- name: build + image: golang + commands: + - go build + - go test ./... + +- name: publish + image: plugins/docker + settings: + repo: itsblue.dev/plugins/codeberg-pages-deploy + auto_tag: true + auto_tag_suffix: linux-amd64 + dockerfile: docker/Dockerfile + username: + from_secret: docker_username + password: + from_secret: docker_password + +- name: publish_arm + image: plugins/docker + settings: + repo: itsblue.dev/plugins/codeberg-pages-deploy + auto_tag: true + auto_tag_suffix: linux-arm + dockerfile: docker/Dockerfile.linux.arm + username: + from_secret: docker_username + password: + from_secret: docker_password + +- name: publish_arm + image: plugins/docker + settings: + repo: itsblue.dev/plugins/codeberg-pages-deploy + auto_tag: true + auto_tag_suffix: linux-arm64 + dockerfile: docker/Dockerfile.linux.arm64 + username: + from_secret: docker_username + password: + from_secret: docker_password + +- name: manifest + image: plugins/manifest + settings: + spec: docker/manifest.tmpl + auto_tag: true + ignore_missing: true + password: + from_secret: docker_password + username: + from_secret: docker_username diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e052d74 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.env +release/ + diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..c5402b9 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,55 @@ +# Blue Oak Model License + +Version 1.0.0 + +## Purpose + +This license gives everyone as much permission to work with +this software as possible, while protecting contributors +from liability. + +## Acceptance + +In order to receive this license, you must agree to its +rules. The rules of this license are both obligations +under that agreement and conditions to your license. +You must not do anything with this software that triggers +a rule that you cannot or will not follow. + +## Copyright + +Each contributor licenses you to do everything with this +software that would otherwise infringe that contributor's +copyright in it. + +## Notices + +You must ensure that everyone who gets a copy of +any part of this software from you, with or without +changes, also gets the text of this license or a link to +. + +## Excuse + +If anyone notifies you in writing that you have not +complied with [Notices](#notices), you can keep your +license by taking all practical steps to comply within 30 +days after the notice. If you do not do so, your license +ends immediately. + +## Patent + +Each contributor licenses you to do everything with this +software that would otherwise infringe any patent claims +they can license or become able to license. + +## Reliability + +No contributor can revoke this license. + +## No Liability + +***As far as the law allows, this software comes as is, +without any warranty or condition, and no contributor +will be liable to anyone for any damages related to this +software or this license, under any kind of legal claim.*** diff --git a/README.md b/README.md index 84541ec..29bf344 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,52 @@ -# codeberg-pages-deploy +A plugin to deploy to codeberg pages. Basically takes a folder and pushes it to the "pages" branch in the same repo.. -A drone plugin to deploy to codeberg pages. Basically takes a folder and pushes it to the "pages" branch in the same repo. \ No newline at end of file +# Usage + +The following settings changes this plugin's behavior. + +* param1 (optional) does something. +* param2 (optional) does something different. + +Below is an example `.drone.yml` that uses this plugin. + +```yaml +kind: pipeline +name: default + +steps: +- name: run itsblue.dev/plugins/codeberg-pages-deploy plugin + image: itsblue.dev/plugins/codeberg-pages-deploy + pull: if-not-exists + settings: + param1: foo + param2: bar +``` + +# Building + +Build the plugin binary: + +```text +scripts/build.sh +``` + +Build the plugin image: + +```text +docker build -t itsblue.dev/plugins/codeberg-pages-deploy -f docker/Dockerfile . +``` + +# Testing + +Execute the plugin from your current working directory: + +```text +docker run --rm -e PLUGIN_PARAM1=foo -e PLUGIN_PARAM2=bar \ + -e DRONE_COMMIT_SHA=8f51ad7884c5eb69c11d260a31da7a745e6b78e2 \ + -e DRONE_COMMIT_BRANCH=master \ + -e DRONE_BUILD_NUMBER=43 \ + -e DRONE_BUILD_STATUS=success \ + -w /drone/src \ + -v $(pwd):/drone/src \ + itsblue.dev/plugins/codeberg-pages-deploy +``` diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..30bbf4b --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,10 @@ +FROM alpine:3.6 as alpine +RUN apk add -U --no-cache ca-certificates + +FROM alpine:3.6 +ENV GODEBUG netdns=go + +COPY --from=alpine /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ + +ADD release/linux/amd64/plugin /bin/ +ENTRYPOINT ["/bin/plugin"] \ No newline at end of file diff --git a/docker/Dockerfile.linux.arm b/docker/Dockerfile.linux.arm new file mode 100644 index 0000000..993c2e9 --- /dev/null +++ b/docker/Dockerfile.linux.arm @@ -0,0 +1,10 @@ +FROM alpine:3.6 as alpine +RUN apk add -U --no-cache ca-certificates + +FROM alpine:3.6 +ENV GODEBUG netdns=go + +COPY --from=alpine /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ + +ADD release/linux/arm/plugin /bin/ +ENTRYPOINT ["/bin/plugin"] \ No newline at end of file diff --git a/docker/Dockerfile.linux.arm64 b/docker/Dockerfile.linux.arm64 new file mode 100644 index 0000000..1223423 --- /dev/null +++ b/docker/Dockerfile.linux.arm64 @@ -0,0 +1,10 @@ +FROM alpine:3.6 as alpine +RUN apk add -U --no-cache ca-certificates + +FROM alpine:3.6 +ENV GODEBUG netdns=go + +COPY --from=alpine /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ + +ADD release/linux/arm64/plugin /bin/ +ENTRYPOINT ["/bin/plugin"] \ No newline at end of file diff --git a/docker/manifest.tmpl b/docker/manifest.tmpl new file mode 100644 index 0000000..dde2431 --- /dev/null +++ b/docker/manifest.tmpl @@ -0,0 +1,31 @@ +image: itsblue.dev/plugins/codeberg-pages-deploy:{{#if build.tag}}{{trimPrefix "v" build.tag}}{{else}}latest{{/if}} +{{#if build.tags}} +tags: +{{#each build.tags}} + - {{this}} +{{/each}} +{{/if}} +manifests: + - + image: itsblue.dev/plugins/codeberg-pages-deploy:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}linux-amd64 + platform: + architecture: amd64 + os: linux + - + image: itsblue.dev/plugins/codeberg-pages-deploy:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}linux-arm64 + platform: + variant: v8 + architecture: arm64 + os: linux + - + image: itsblue.dev/plugins/codeberg-pages-deploy:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}linux-arm + platform: + variant: v7 + architecture: arm + os: linux + - + image: itsblue.dev/plugins/codeberg-pages-deploy:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}linux-arm + platform: + variant: v6 + architecture: arm + os: linux \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..20b0448 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module itsblue.dev/plugins/codeberg-pages-deploy + +go 1.12 + +require ( + github.com/kelseyhightower/envconfig v1.4.0 + github.com/sirupsen/logrus v1.4.2 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..ca4ed32 --- /dev/null +++ b/go.sum @@ -0,0 +1,9 @@ +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8= +github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= \ No newline at end of file diff --git a/main.go b/main.go new file mode 100644 index 0000000..4de542f --- /dev/null +++ b/main.go @@ -0,0 +1,48 @@ +// Copyright 2020 the Drone Authors. All rights reserved. +// Use of this source code is governed by the Blue Oak Model License +// that can be found in the LICENSE file. + +package main + +import ( + "context" + + "itsblue.dev/plugins/codeberg-pages-deploy/plugin" + + "github.com/kelseyhightower/envconfig" + "github.com/sirupsen/logrus" +) + +func main() { + logrus.SetFormatter(new(formatter)) + + var args plugin.Args + if err := envconfig.Process("", &args); err != nil { + logrus.Fatalln(err) + } + + switch args.Level { + case "debug": + logrus.SetFormatter(textFormatter) + logrus.SetLevel(logrus.DebugLevel) + case "trace": + logrus.SetFormatter(textFormatter) + logrus.SetLevel(logrus.TraceLevel) + } + + if err := plugin.Exec(context.Background(), args); err != nil { + logrus.Fatalln(err) + } +} + +// default formatter that writes logs without including timestamp +// or level information. +type formatter struct {} +func (*formatter) Format(entry *logrus.Entry) ([]byte, error) { + return []byte(entry.Message), nil +} + +// text formatter that writes logs with level information +var textFormatter = &logrus.TextFormatter{ + DisableTimestamp: true, +} diff --git a/plugin/pipeline.go b/plugin/pipeline.go new file mode 100644 index 0000000..af82a23 --- /dev/null +++ b/plugin/pipeline.go @@ -0,0 +1,149 @@ +// Copyright 2020 the Drone Authors. All rights reserved. +// Use of this source code is governed by the Blue Oak Model License +// that can be found in the LICENSE file. + +package plugin + +// Pipeline provides Pipeline metadata from the environment. +type Pipeline struct { + // Build provides build metadata. + Build struct { + Branch string `envconfig:"DRONE_BUILD_BRANCH"` + Number int `envconfig:"DRONE_BUILD_NUMBER"` + Parent int `envconfig:"DRONE_BUILD_PARENT"` + Event string `envconfig:"DRONE_BUILD_EVENT"` + Action string `envconfig:"DRONE_BUILD_ACTION"` + Status string `envconfig:"DRONE_BUILD_STATUS"` + Created int64 `envconfig:"DRONE_BUILD_CREATED"` + Started int64 `envconfig:"DRONE_BUILD_STARTED"` + Finished int64 `envconfig:"DRONE_BUILD_FINISHED"` + Link string `envconfig:"DRONE_BUILD_LINK"` + } + + // Calver provides the calver details parsed from the + // git tag. If the git tag is empty or is not a valid + // calver, the values will be empty. + Calver struct { + Version string `envconfig:"DRONE_CALVER"` + Short string `envconfig:"DRONE_CALVER_SHORT"` + MajorMinor string `envconfig:"DRONE_CALVER_MAJOR_MINOR"` + Major string `envconfig:"DRONE_CALVER_MAJOR"` + Minor string `envconfig:"DRONE_CALVER_MINOR"` + Micro string `envconfig:"DRONE_CALVER_MICRO"` + Modifier string `envconfig:"DRONE_CALVER_MODIFIER"` + } + + // Card provides adaptive card configuration options. + Card struct { + Path string `envconfig:"DRONE_CARD_PATH"` + } + + // Commit provides the commit metadata. + Commit struct { + Rev string `envconfig:"DRONE_COMMIT_SHA"` + Before string `envconfig:"DRONE_COMMIT_BEFORE"` + After string `envconfig:"DRONE_COMMIT_AFTER"` + Ref string `envconfig:"DRONE_COMMIT_REF"` + Branch string `envconfig:"DRONE_COMMIT_BRANCH"` + Source string `envconfig:"DRONE_COMMIT_SOURCE"` + Target string `envconfig:"DRONE_COMMIT_TARGET"` + Link string `envconfig:"DRONE_COMMIT_LINK"` + Message string `envconfig:"DRONE_COMMIT_MESSAGE"` + + Author struct { + Username string `envconfig:"DRONE_COMMIT_AUTHOR"` + Name string `envconfig:"DRONE_COMMIT_AUTHOR_NAME"` + Email string `envconfig:"DRONE_COMMIT_AUTHOR_EMAIL"` + Avatar string `envconfig:"DRONE_COMMIT_AUTHOR_AVATAR"` + } + } + + // Deploy provides the deployment metadata. + Deploy struct { + ID string `envconfig:"DRONE_DEPLOY_TO"` + Target string `envconfig:"DRONE_DEPLOY_ID"` + } + + // Failed provides a list of failed steps and failed stages + // for the current pipeline. + Failed struct { + Steps []string `envconfig:"DRONE_FAILED_STEPS"` + Stages []string `envconfig:"DRONE_FAILED_STAGES"` + } + + // Git provides the git repository metadata. + Git struct { + HTTPURL string `envconfig:"DRONE_GIT_HTTP_URL"` + SSHURL string `envconfig:"DRONE_GIT_SSH_URL"` + } + + // PullRequest provides the pull request metadata. + PullRequest struct { + Number int `envconfig:"DRONE_PULL_REQUEST"` + } + + // Repo provides the repository metadata. + Repo struct { + Branch string `envconfig:"DRONE_REPO_BRANCH"` + Link string `envconfig:"DRONE_REPO_LINK"` + Namespace string `envconfig:"DRONE_REPO_NAMESPACE"` + Name string `envconfig:"DRONE_REPO_NAME"` + Private bool `envconfig:"DRONE_REPO_PRIVATE"` + Remote string `envconfig:"DRONE_GIT_HTTP_URL"` + SCM string `envconfig:"DRONE_REPO_SCM"` + Slug string `envconfig:"DRONE_REPO"` + Visibility string `envconfig:"DRONE_REPO_VISIBILITY"` + } + + // Stage provides the stage metadata. + Stage struct { + Kind string `envconfig:"DRONE_STAGE_KIND"` + Type string `envconfig:"DRONE_STAGE_TYPE"` + Name string `envconfig:"DRONE_STAGE_NAME"` + Number int `envconfig:"DRONE_STAGE_NUMBER"` + Machine string `envconfig:"DRONE_STAGE_MACHINE"` + OS string `envconfig:"DRONE_STAGE_OS"` + Arch string `envconfig:"DRONE_STAGE_ARCH"` + Variant string `envconfig:"DRONE_STAGE_VARIANT"` + Status string `envconfig:"DRONE_STAGE_STATUS"` + Started int64 `envconfig:"DRONE_STAGE_STARTED"` + Finished int64 `envconfig:"DRONE_STAGE_FINISHED"` + DependsOn []string `envconfig:"DRONE_STAGE_DEPENDS_ON"` + } + + // Step provides the step metadata. + Step struct { + Number int `envconfig:"DRONE_STEP_NUMBER"` + Name string `envconfig:"DRONE_STEP_NAME"` + } + + // Semver provides the semver details parsed from the + // git tag. If the git tag is empty or is not a valid + // semver, the values will be empty and the error field + // will be populated with the parsing error. + Semver struct { + Version string `envconfig:"DRONE_SEMVER"` + Short string `envconfig:"DRONE_SEMVER_SHORT"` + Major string `envconfig:"DRONE_SEMVER_MAJOR"` + Minor string `envconfig:"DRONE_SEMVER_MINOR"` + Patch string `envconfig:"DRONE_SEMVER_PATCH"` + Build string `envconfig:"DRONE_SEMVER_BUILD"` + PreRelease string `envconfig:"DRONE_SEMVER_PRERELEASE"` + Error string `envconfig:"DRONE_SEMVER_ERROR"` + } + + // System provides the Drone system metadata, including + // the system version of details required to create the + // drone website address. + System struct { + Proto string `envconfig:"DRONE_SYSTEM_PROTO"` + Host string `envconfig:"DRONE_SYSTEM_HOST"` + Hostname string `envconfig:"DRONE_SYSTEM_HOSTNAME"` + Version string `envconfig:"DRONE_SYSTEM_VERSION"` + } + + // Tag provides the git tag details. + Tag struct { + Name string `envconfig:"DRONE_TAG"` + } +} diff --git a/plugin/plugin.go b/plugin/plugin.go new file mode 100644 index 0000000..fee9c9d --- /dev/null +++ b/plugin/plugin.go @@ -0,0 +1,25 @@ +// Copyright 2020 the Drone Authors. All rights reserved. +// Use of this source code is governed by the Blue Oak Model License +// that can be found in the LICENSE file. + +package plugin + +import "context" + +// Args provides plugin execution arguments. +type Args struct { + Pipeline + + // Level defines the plugin log level. + Level string `envconfig:"PLUGIN_LOG_LEVEL"` + + // TODO replace or remove + Param1 string `envconfig:"PLUGIN_PARAM1"` + Param2 string `envconfig:"PLUGIN_PARAM2"` +} + +// Exec executes the plugin. +func Exec(ctx context.Context, args Args) error { + // write code here + return nil +} diff --git a/plugin/plugin_test.go b/plugin/plugin_test.go new file mode 100644 index 0000000..cf5d77a --- /dev/null +++ b/plugin/plugin_test.go @@ -0,0 +1,11 @@ +// Copyright 2020 the Drone Authors. All rights reserved. +// Use of this source code is governed by the Blue Oak Model License +// that can be found in the LICENSE file. + +package plugin + +import "testing" + +func TestPlugin(t *testing.T) { + t.Skip() +} diff --git a/plugin/util.go b/plugin/util.go new file mode 100644 index 0000000..caf1a05 --- /dev/null +++ b/plugin/util.go @@ -0,0 +1,36 @@ +// Copyright 2020 the Drone Authors. All rights reserved. +// Use of this source code is governed by the Blue Oak Model License +// that can be found in the LICENSE file. + +package plugin + +import ( + "encoding/base64" + "encoding/json" + "io" + "io/ioutil" + "os" +) + +func writeCard(path, schema string, card interface{}) { + data, _ := json.Marshal(map[string]interface{}{ + "schema": schema, + "data": card, + }) + switch { + case path == "/dev/stdout": + writeCardTo(os.Stdout, data) + case path == "/dev/stderr": + writeCardTo(os.Stderr, data) + case path != "": + ioutil.WriteFile(path, data, 0644) + } +} + +func writeCardTo(out io.Writer, data []byte) { + encoded := base64.StdEncoding.EncodeToString(data) + io.WriteString(out, "\u001B]1338;") + io.WriteString(out, encoded) + io.WriteString(out, "\u001B]0m") + io.WriteString(out, "\n") +} diff --git a/scripts/build.sh b/scripts/build.sh new file mode 100755 index 0000000..74efe8b --- /dev/null +++ b/scripts/build.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +# force go modules +export GOPATH="" + +# disable cgo +export CGO_ENABLED=0 + +set -e +set -x + +# linux +GOOS=linux GOARCH=amd64 go build -o release/linux/amd64/plugin +GOOS=linux GOARCH=arm64 go build -o release/linux/arm64/plugin +GOOS=linux GOARCH=arm go build -o release/linux/arm/plugin + +# windows +GOOS=windows go build -o release/windows/amd64/plugin.exe diff --git a/scripts/upgrade.sh b/scripts/upgrade.sh new file mode 100755 index 0000000..dd5e50b --- /dev/null +++ b/scripts/upgrade.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +curl https://raw.githubusercontent.com/drone/boilr-plugin/master/template/plugin/pipeline.go --output plugin/pipeline.go