Compare commits

..

13 Commits

Author SHA1 Message Date
Bo-Yi Wu 7ba6170e12 fix: show error if missing jobs. 2017-01-16 15:04:12 +08:00
Bo-Yi Wu 4ce3a1e347 docs: update 2017-01-16 14:09:27 +08:00
Bo-Yi Wu 1898550d88 feat: support build binary (#10) 2017-01-16 14:07:17 +08:00
Bo-Yi Wu 6ef9ab8751 docs: [ci skip] update docs. 2017-01-16 11:58:55 +08:00
Bo-Yi Wu 7275c0c66b feat: replace jenkins with drone (#7) 2017-01-16 11:55:28 +08:00
Bo-Yi Wu bb23a33bf0 feat: update glide 2017-01-16 10:48:37 +08:00
Bo-Yi Wu c4cd67adc5 refactor: remove 1.5.x testing and docker services. (#6) 2017-01-16 10:46:35 +08:00
Bo-Yi Wu 788418c56f refactor: remove unnecessary config. (#5) 2017-01-16 10:42:55 +08:00
Bo-Yi Wu 2211e06f29 Merge pull request #2 from appleboy/cli
Support load configuration from files.
2016-11-21 11:19:12 +08:00
Bo-Yi Wu 226713ff24 Support load configuration from files.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2016-11-21 11:16:28 +08:00
Bo-Yi Wu 186ee76d3c update Version comment.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2016-11-19 22:36:05 +08:00
Bo-Yi Wu 4917613ed8 add version command.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2016-11-19 21:19:38 +08:00
Bo-Yi Wu e6e2562e1e update to 1.7.3 testing.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2016-10-25 16:34:32 +08:00
11 changed files with 361 additions and 215 deletions
+80
View File
@@ -0,0 +1,80 @@
workspace:
base: /srv/app
path: src/github.com/appleboy/drone-jenkins
pipeline:
clone:
image: plugins/git
tags: true
# restore the cache from an sftp server
restore_cache:
image: appleboy/drone-sftp-cache
pull: true
restore: true
mount: [ .glide, vendor ]
ignore_branch: true
test:
image: appleboy/golang-testing
pull: true
environment:
TAGS: netgo
GOPATH: /srv/app
commands:
- make dep_install
- make vet
- make lint
- make test
- make coverage
- make build
# build binary for docker image
- make static_build
when:
event: [ push, tag, pull_request ]
release:
image: appleboy/golang-testing
pull: true
environment:
TAGS: netgo
GOPATH: /srv/app
commands:
- make release
when:
event: [ tag ]
branch: [ refs/tags/* ]
docker:
image: plugins/docker
repo: ${DRONE_REPO}
tags: [ '${DRONE_TAG}' ]
when:
event: [ tag ]
branch: [ refs/tags/* ]
docker:
image: plugins/docker
repo: ${DRONE_REPO}
tags: [ 'latest' ]
when:
event: [ push ]
branch: [ master ]
github:
image: plugins/github-release
files:
- dist/release/*
when:
event: [ tag ]
branch: [ refs/tags/* ]
# rebuild the cache on the sftp server
rebuild_cache:
image: appleboy/drone-sftp-cache
pull: true
rebuild: true
mount: [ .glide, vendor ]
ignore_branch: true
when:
branch: master
+1
View File
@@ -0,0 +1 @@
eyJhbGciOiJIUzI1NiJ9.d29ya3NwYWNlOgogIGJhc2U6IC9zcnYvYXBwCiAgcGF0aDogc3JjL2dpdGh1Yi5jb20vYXBwbGVib3kvZHJvbmUtamVua2lucwoKcGlwZWxpbmU6CiAgY2xvbmU6CiAgICBpbWFnZTogcGx1Z2lucy9naXQKICAgIHRhZ3M6IHRydWUKCiAgIyByZXN0b3JlIHRoZSBjYWNoZSBmcm9tIGFuIHNmdHAgc2VydmVyCiAgcmVzdG9yZV9jYWNoZToKICAgIGltYWdlOiBhcHBsZWJveS9kcm9uZS1zZnRwLWNhY2hlCiAgICBwdWxsOiB0cnVlCiAgICByZXN0b3JlOiB0cnVlCiAgICBtb3VudDogWyAuZ2xpZGUsIHZlbmRvciBdCiAgICBpZ25vcmVfYnJhbmNoOiB0cnVlCgogIHRlc3Q6CiAgICBpbWFnZTogYXBwbGVib3kvZ29sYW5nLXRlc3RpbmcKICAgIHB1bGw6IHRydWUKICAgIGVudmlyb25tZW50OgogICAgICBUQUdTOiBuZXRnbwogICAgICBHT1BBVEg6IC9zcnYvYXBwCiAgICBjb21tYW5kczoKICAgICAgLSBtYWtlIGRlcF9pbnN0YWxsCiAgICAgIC0gbWFrZSB2ZXQKICAgICAgLSBtYWtlIGxpbnQKICAgICAgLSBtYWtlIHRlc3QKICAgICAgLSBtYWtlIGNvdmVyYWdlCiAgICAgIC0gbWFrZSBidWlsZAogICAgICAjIGJ1aWxkIGJpbmFyeSBmb3IgZG9ja2VyIGltYWdlCiAgICAgIC0gbWFrZSBzdGF0aWNfYnVpbGQKICAgIHdoZW46CiAgICAgIGV2ZW50OiBbIHB1c2gsIHRhZywgcHVsbF9yZXF1ZXN0IF0KCiAgcmVsZWFzZToKICAgIGltYWdlOiBhcHBsZWJveS9nb2xhbmctdGVzdGluZwogICAgcHVsbDogdHJ1ZQogICAgZW52aXJvbm1lbnQ6CiAgICAgIFRBR1M6IG5ldGdvCiAgICAgIEdPUEFUSDogL3Nydi9hcHAKICAgIGNvbW1hbmRzOgogICAgICAtIG1ha2UgcmVsZWFzZQogICAgd2hlbjoKICAgICAgZXZlbnQ6IFsgdGFnIF0KICAgICAgYnJhbmNoOiBbIHJlZnMvdGFncy8qIF0KCiAgZG9ja2VyOgogICAgaW1hZ2U6IHBsdWdpbnMvZG9ja2VyCiAgICByZXBvOiAke0RST05FX1JFUE99CiAgICB0YWdzOiBbICcke0RST05FX1RBR30nIF0KICAgIHdoZW46CiAgICAgIGV2ZW50OiBbIHRhZyBdCiAgICAgIGJyYW5jaDogWyByZWZzL3RhZ3MvKiBdCgogIGRvY2tlcjoKICAgIGltYWdlOiBwbHVnaW5zL2RvY2tlcgogICAgcmVwbzogJHtEUk9ORV9SRVBPfQogICAgdGFnczogWyAnbGF0ZXN0JyBdCiAgICB3aGVuOgogICAgICBldmVudDogWyBwdXNoIF0KICAgICAgYnJhbmNoOiBbIG1hc3RlciBdCgogIGdpdGh1YjoKICAgIGltYWdlOiBwbHVnaW5zL2dpdGh1Yi1yZWxlYXNlCiAgICBmaWxlczoKICAgICAgLSBkaXN0L3JlbGVhc2UvKgogICAgd2hlbjoKICAgICAgZXZlbnQ6IFsgdGFnIF0KICAgICAgYnJhbmNoOiBbIHJlZnMvdGFncy8qIF0KCiAgIyByZWJ1aWxkIHRoZSBjYWNoZSBvbiB0aGUgc2Z0cCBzZXJ2ZXIKICByZWJ1aWxkX2NhY2hlOgogICAgaW1hZ2U6IGFwcGxlYm95L2Ryb25lLXNmdHAtY2FjaGUKICAgIHB1bGw6IHRydWUKICAgIHJlYnVpbGQ6IHRydWUKICAgIG1vdW50OiBbIC5nbGlkZSwgdmVuZG9yIF0KICAgIGlnbm9yZV9icmFuY2g6IHRydWUKICAgIHdoZW46CiAgICAgIGJyYW5jaDogbWFzdGVyCg.6JgOuyvwKlOK6Hs1hD2dFaWGrtnRjVClYH-jQk-Wgq4
+2
View File
@@ -25,3 +25,5 @@ _testmain.go
vendor
drone-jenkins
coverage.txt
.env
dist
+3 -34
View File
@@ -1,53 +1,22 @@
sudo: required
language: go
services:
- docker
go:
- 1.5.4
- 1.6.3
- 1.7.1
- 1.6.x
- 1.7.x
- tip
env:
global:
- DOCKER_CACHE_FILE=${HOME}/docker/cache.tar.gz
cache:
directories:
- vendor
- ${HOME}/.glide
- ${HOME}/docker
before_install:
- mkdir -p $GOPATH/bin
- curl https://glide.sh/get | sh
- if [ -f ${DOCKER_CACHE_FILE} ]; then gunzip -c ${DOCKER_CACHE_FILE} | docker load; fi
install:
- export GO15VENDOREXPERIMENT=1
- make install
- make dep_install
script:
- make test
- make docker
- if [ "$TRAVIS_BRANCH" == "master" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ]; then
mkdir -p $(dirname ${DOCKER_CACHE_FILE});
docker save $(docker history -q $TRAVIS_REPO_SLUG:latest | grep -v '<missing>') | gzip > ${DOCKER_CACHE_FILE};
fi
after_success:
# ignore main.go coverage
- sed -i '/main.go/d' coverage.txt
- bash <(curl -s https://codecov.io/bash) -f coverage.txt
# deploy from master
- if [ "$TRAVIS_BRANCH" == "master" ] && [ "$TRAVIS_GO_VERSION" == "1.7.1" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ]; then
docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD";
make docker_deploy tag=latest;
fi
# deploy from tag
- if [ "$TRAVIS_GO_VERSION" == "1.7.1" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ] && [ "$TRAVIS_TAG" != "" ]; then
docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD";
make docker_deploy tag=$TRAVIS_TAG;
fi
+84 -15
View File
@@ -1,8 +1,17 @@
.PHONY: install build test html update docker_build docker_image docker docker_deploy clean
.PHONY: test drone-jenkins build fmt vet errcheck lint install update release-dirs release-build release-copy release-check release coverage
VERSION := $(shell git describe --tags || git rev-parse --short HEAD)
DEPLOY_ACCOUNT := "appleboy"
DEPLOY_IMAGE := "drone-jenkins"
DIST := dist
EXECUTABLE := drone-jenkins
# for dockerhub
DEPLOY_ACCOUNT := appleboy
DEPLOY_IMAGE := $(EXECUTABLE)
TARGETS ?= linux darwin windows
PACKAGES ?= $(shell go list ./... | grep -v /vendor/)
SOURCES ?= $(shell find . -name "*.go" -type f)
TAGS ?=
LDFLAGS += -X 'main.Version=$(VERSION)'
ifneq ($(shell uname), Darwin)
EXTLDFLAGS = -extldflags "-static" $(null)
@@ -10,36 +19,96 @@ else
EXTLDFLAGS =
endif
install:
glide install
ifneq ($(DRONE_TAG),)
VERSION ?= $(DRONE_TAG)
else
VERSION ?= $(shell git describe --tags --always || git rev-parse --short HEAD)
endif
build:
go build -ldflags="$(EXTLDFLAGS)-s -w -X main.Version=$(VERSION)"
all: build
fmt:
find . -name "*.go" -type f -not -path "./vendor/*" | xargs gofmt -s -w
vet:
go vet $(PACKAGES)
errcheck:
@which errcheck > /dev/null; if [ $$? -ne 0 ]; then \
go get -u github.com/kisielk/errcheck; \
fi
errcheck $(PACKAGES)
lint:
@which golint > /dev/null; if [ $$? -ne 0 ]; then \
go get -u github.com/golang/lint/golint; \
fi
for PKG in $(PACKAGES); do golint -set_exit_status $$PKG || exit 1; done;
test:
go test -v -coverprofile=coverage.txt
for PKG in $(PACKAGES); do go test -v -cover -coverprofile $$GOPATH/src/$$PKG/coverage.txt $$PKG || exit 1; done;
html:
go tool cover -html=coverage.txt
update:
dep_install:
glide install
dep_update:
glide up
docker_build:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -tags netgo -ldflags="-X main.Version=$(VERSION)"
install: $(SOURCES)
go install -v -tags '$(TAGS)' -ldflags '$(EXTLDFLAGS)-s -w $(LDFLAGS)'
build: $(EXECUTABLE)
$(EXECUTABLE): $(SOURCES)
go build -v -tags '$(TAGS)' -ldflags '$(EXTLDFLAGS)-s -w $(LDFLAGS)' -o $@
release: release-dirs release-build release-copy release-check
release-dirs:
mkdir -p $(DIST)/binaries $(DIST)/release
release-build:
@which gox > /dev/null; if [ $$? -ne 0 ]; then \
go get -u github.com/mitchellh/gox; \
fi
gox -os="$(TARGETS)" -arch="amd64 386" -tags="$(TAGS)" -ldflags="-s -w $(LDFLAGS)" -output="$(DIST)/binaries/$(EXECUTABLE)-$(VERSION)-{{.OS}}-{{.Arch}}"
release-copy:
$(foreach file,$(wildcard $(DIST)/binaries/$(EXECUTABLE)-*),cp $(file) $(DIST)/release/$(notdir $(file));)
release-check:
cd $(DIST)/release; $(foreach file,$(wildcard $(DIST)/release/$(EXECUTABLE)-*),sha256sum $(notdir $(file)) > $(notdir $(file)).sha256;)
# for docker.
static_build:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -tags '$(TAGS)' -ldflags '$(EXTLDFLAGS)-s -w $(LDFLAGS)' -o $(DEPLOY_IMAGE)
docker_image:
docker build --rm -t $(DEPLOY_ACCOUNT)/$(DEPLOY_IMAGE) .
docker build -t $(DEPLOY_ACCOUNT)/$(DEPLOY_IMAGE) .
docker: docker_build docker_image
docker: static_build docker_image
docker_deploy:
ifeq ($(tag),)
@echo "Usage: make $@ tag=<tag>"
@exit 1
endif
# deploy image
docker tag $(DEPLOY_ACCOUNT)/$(DEPLOY_IMAGE):latest $(DEPLOY_ACCOUNT)/$(DEPLOY_IMAGE):$(tag)
docker push $(DEPLOY_ACCOUNT)/$(DEPLOY_IMAGE):$(tag)
coverage:
sed -i '/main.go/d' coverage.txt
curl -s https://codecov.io/bash > .codecov && \
chmod +x .codecov && \
./.codecov -f coverage.txt
clean:
rm -rf coverage.txt ${DEPLOY_IMAGE}
go clean -x -i ./...
rm -rf coverage.txt $(EXECUTABLE) $(DIST) vendor
version:
@echo $(VERSION)
+83 -21
View File
@@ -1,25 +1,29 @@
# drone-jenkins
[![Build Status](https://travis-ci.org/appleboy/drone-jenkins.svg?branch=master)](https://travis-ci.org/appleboy/drone-jenkins) [![codecov](https://codecov.io/gh/appleboy/drone-jenkins/branch/master/graph/badge.svg)](https://codecov.io/gh/appleboy/drone-jenkins) [![Go Report Card](https://goreportcard.com/badge/github.com/appleboy/drone-jenkins)](https://goreportcard.com/report/github.com/appleboy/drone-jenkins)
[![GoDoc](https://godoc.org/github.com/appleboy/drone-jenkins?status.svg)](https://godoc.org/github.com/appleboy/drone-jenkins) [![Build Status](http://drone.wu-boy.com/api/badges/appleboy/drone-jenkins/status.svg)](http://drone.wu-boy.com/appleboy/drone-jenkins) [![codecov](https://codecov.io/gh/appleboy/drone-jenkins/branch/master/graph/badge.svg)](https://codecov.io/gh/appleboy/drone-jenkins) [![Go Report Card](https://goreportcard.com/badge/github.com/appleboy/drone-jenkins)](https://goreportcard.com/report/github.com/appleboy/drone-jenkins) [![Docker Pulls](https://img.shields.io/docker/pulls/appleboy/drone-jenkins.svg)](https://hub.docker.com/r/appleboy/drone-jenkins/) [![](https://images.microbadger.com/badges/image/appleboy/drone-jenkins.svg)](https://microbadger.com/images/appleboy/drone-jenkins "Get your own image badge on microbadger.com")
[Drone](https://github.com/drone/drone) plugin for trigger [Jenkins](https://jenkins.io/) jobs.
## Build
## Build or Download a binary
Build the binary with the following commands:
The pre-compiled binaries can be downloaded from [release page](https://github.com/appleboy/drone-jenkins/releases). Support the following OS type.
* Windows amd64/386
* Linux amd64/386
* Darwin amd64/386
With `Go` installed
```
$ go get -u -v github.com/appleboy/drone-jenkins
```
or build the binary with the following command:
```
$ make build
```
## Testing
Test the package with the following command:
```
$ make test
```
## Docker
Build the docker image with the following commands:
@@ -38,23 +42,81 @@ docker: Error response from daemon: Container command
## Usage
There are three ways to trigger jenkins jobs.
* [usage from binary](#usage-from-binary)
* [usage from docker](#usage-from-docker)
* [usage from drone ci](#usage-from-drone-ci)
<a name="usage-from-binary"></a>
### Usage from binary
trigger single job.
```bash
drone-jenkins \
--host http://jenkins.example.com/ \
--user appleboy \
--token XXXXXXXX \
--job drone-jenkins-plugin
```
trigger multiple jobs.
```bash
drone-jenkins \
--host http://jenkins.example.com/ \
--user appleboy \
--token XXXXXXXX \
--job drone-jenkins-plugin-1 \
--job drone-jenkins-plugin-2
```
<a name="usage-from-docker"></a>
### Usage from docker
trigger single job.
```bash
docker run --rm \
-e JENKINS_BASE_URL=http://jenkins.example.com/
-e JENKINS_USER=appleboy
-e JENKINS_TOKEN=xxxxxxx
-e JENKINS_JOB=drone-jenkins-plugin
appleboy/drone-jenkins
```
trigger multiple jobs.
```bash
docker run --rm \
-e JENKINS_BASE_URL=http://jenkins.example.com/
-e JENKINS_USER=appleboy
-e JENKINS_TOKEN=xxxxxxx
-e JENKINS_JOB=drone-jenkins-plugin-1,drone-jenkins-plugin-2
appleboy/drone-jenkins
```
<a name="usage-from-drone-ci"></a>
### Usage from drone ci
Execute from the working directory:
```
docker run --rm \
-e PLUGIN_BASE_URL=http://example.com \
-e PLUGIN_USERNAME=xxxxxxx \
-e PLUGIN_URL=http://example.com \
-e PLUGIN_USER=xxxxxxx \
-e PLUGIN_TOKEN=xxxxxxx \
-e PLUGIN_JOB=xxxxxxx \
-e DRONE_REPO_OWNER=appleboy \
-e DRONE_REPO_NAME=go-hello \
-e DRONE_COMMIT_SHA=e5e82b5eb3737205c25955dcc3dcacc839b7be52 \
-e DRONE_COMMIT_BRANCH=master \
-e DRONE_COMMIT_AUTHOR=appleboy \
-e DRONE_BUILD_NUMBER=1 \
-e DRONE_BUILD_STATUS=success \
-e DRONE_BUILD_LINK=http://github.com/appleboy/go-hello \
-v $(pwd):$(pwd) \
-w $(pwd) \
appleboy/drone-jenkins
```
## Testing
Test the package with the following command:
```
$ make test
```
Generated
+5 -5
View File
@@ -1,12 +1,12 @@
hash: 0ddd797a78c8eef869d73907846b08ce2ba1dc91ddb0c644b80e28a949f79819
updated: 2016-10-18T10:10:10.836829677+08:00
hash: 33ad75827779208440690394b479984ac46bc9d6c817389fb6d98f9fddbdf813
updated: 2017-01-16T10:47:58.426849408+08:00
imports:
- name: github.com/joho/godotenv
version: 4ed13390c0acd2ff4e371e64d8b97c8954138243
version: a01a834e1654b4c9ca5b3ad05159445cc9c7ad08
subpackages:
- autoload
- name: github.com/urfave/cli
version: 55f715e28c46073d0e217e2ce8eb46b0b45e3db6
version: 0bdeddeeb0f650497d603c4ad7b20cfe685682f6
testImports:
- name: github.com/davecgh/go-spew
version: 6d212800a42e8ab5c146b8ace3490ee17e5225f9
@@ -17,6 +17,6 @@ testImports:
subpackages:
- difflib
- name: github.com/stretchr/testify
version: 976c720a22c8eb4eb6a0b4348ad85ad12491a506
version: 69483b4bd14f5845b5a1e55bca19e954e827f1d0
subpackages:
- assert
+3
View File
@@ -1,10 +1,13 @@
package: github.com/appleboy/drone-jenkins
import:
- package: github.com/joho/godotenv
version: ^1.0.0
subpackages:
- autoload
- package: github.com/urfave/cli
version: ^1.19.1
testImport:
- package: github.com/stretchr/testify
version: ^1.1.4
subpackages:
- assert
+63 -80
View File
@@ -3,119 +3,102 @@ package main
import (
"os"
"github.com/joho/godotenv"
_ "github.com/joho/godotenv/autoload"
"github.com/urfave/cli"
)
// Version for command line
// Version set at compile-time
var Version string
func main() {
app := cli.NewApp()
app.Name = "jenkins plugin"
app.Usage = "jenkins plugin"
app.Usage = "trigger jenkins jobs"
app.Copyright = "Copyright (c) 2017 Bo-Yi Wu"
app.Authors = []cli.Author{
cli.Author{
Name: "Bo-Yi Wu",
Email: "appleboy.tw@gmail.com",
},
}
app.Action = run
app.Version = Version
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "base.url",
Name: "host",
Usage: "jenkins base url",
EnvVar: "PLUGIN_BASE_URL,JENKINS_BASE_URL",
EnvVar: "PLUGIN_URL,JENKINS_URL",
},
cli.StringFlag{
Name: "username",
Name: "user,u",
Usage: "jenkins username",
EnvVar: "PLUGIN_USERNAME,JENKINS_USERNAME",
EnvVar: "PLUGIN_USER,JENKINS_USER",
},
cli.StringFlag{
Name: "token",
Name: "token,t",
Usage: "jenkins token",
EnvVar: "PLUGIN_TOKEN,JENKINS_TOKEN",
},
cli.StringSliceFlag{
Name: "job",
Name: "job,j",
Usage: "jenkins job",
EnvVar: "PLUGIN_JOB",
EnvVar: "PLUGIN_JOB,JENKINS_JOB",
},
cli.StringFlag{
Name: "repo.owner",
Usage: "repository owner",
EnvVar: "DRONE_REPO_OWNER",
},
cli.StringFlag{
Name: "repo.name",
Usage: "repository name",
EnvVar: "DRONE_REPO_NAME",
},
cli.StringFlag{
Name: "commit.sha",
Usage: "git commit sha",
EnvVar: "DRONE_COMMIT_SHA",
},
cli.StringFlag{
Name: "commit.branch",
Value: "master",
Usage: "git commit branch",
EnvVar: "DRONE_COMMIT_BRANCH",
},
cli.StringFlag{
Name: "commit.author",
Usage: "git author name",
EnvVar: "DRONE_COMMIT_AUTHOR",
},
cli.StringFlag{
Name: "commit.message",
Usage: "commit message",
EnvVar: "DRONE_COMMIT_MESSAGE",
},
cli.StringFlag{
Name: "build.event",
Value: "push",
Usage: "build event",
EnvVar: "DRONE_BUILD_EVENT",
},
cli.IntFlag{
Name: "build.number",
Usage: "build number",
EnvVar: "DRONE_BUILD_NUMBER",
},
cli.StringFlag{
Name: "build.status",
Usage: "build status",
Value: "success",
EnvVar: "DRONE_BUILD_STATUS",
},
cli.StringFlag{
Name: "build.link",
Usage: "build link",
EnvVar: "DRONE_BUILD_LINK",
Name: "env-file",
Usage: "source env file",
EnvVar: "ENV_FILE",
Value: ".env",
},
}
// Override a template
cli.AppHelpTemplate = `
________ ____. __ .__
\______ \_______ ____ ____ ____ | | ____ ____ | | _|__| ____ ______
| | \_ __ \/ _ \ / \_/ __ \ ______ | |/ __ \ / \| |/ / |/ \ / ___/
| | \ | \( <_> ) | \ ___/ /_____/ /\__| \ ___/| | \ <| | | \\___ \
/_______ /__| \____/|___| /\___ > \________|\___ >___| /__|_ \__|___| /____ >
\/ \/ \/ \/ \/ \/ \/ \/
version: {{.Version}}
NAME:
{{.Name}} - {{.Usage}}
USAGE:
{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}
{{if len .Authors}}
AUTHOR:
{{range .Authors}}{{ . }}{{end}}
{{end}}{{if .Commands}}
COMMANDS:
{{range .Commands}}{{if not .HideHelp}} {{join .Names ", "}}{{ "\t"}}{{.Usage}}{{ "\n" }}{{end}}{{end}}{{end}}{{if .VisibleFlags}}
GLOBAL OPTIONS:
{{range .VisibleFlags}}{{.}}
{{end}}{{end}}{{if .Copyright }}
COPYRIGHT:
{{.Copyright}}
{{end}}{{if .Version}}
VERSION:
{{.Version}}
{{end}}
REPOSITORY:
Github: https://github.com/appleboy/drone-line
`
app.Run(os.Args)
}
func run(c *cli.Context) error {
if c.String("env-file") != "" {
_ = godotenv.Load(c.String("env-file"))
}
plugin := Plugin{
Repo: Repo{
Owner: c.String("repo.owner"),
Name: c.String("repo.name"),
},
Build: Build{
Number: c.Int("build.number"),
Event: c.String("build.event"),
Status: c.String("build.status"),
Commit: c.String("commit.sha"),
Branch: c.String("commit.branch"),
Author: c.String("commit.author"),
Message: c.String("commit.message"),
Link: c.String("build.link"),
},
Config: Config{
BaseURL: c.String("base.url"),
Username: c.String("username"),
Token: c.String("token"),
Job: c.StringSlice("job"),
},
BaseURL: c.String("host"),
Username: c.String("user"),
Token: c.String("token"),
Job: c.StringSlice("job"),
}
return plugin.Exec()
+16 -37
View File
@@ -2,43 +2,17 @@ package main
import (
"errors"
"log"
"strings"
)
type (
// Repo information.
Repo struct {
Owner string
Name string
}
// Build information.
Build struct {
Event string
Number int
Commit string
Message string
Branch string
Author string
Status string
Link string
}
// Config for the plugin.
Config struct {
// Plugin values.
Plugin struct {
BaseURL string
Username string
Token string
Job []string
}
// Plugin values.
Plugin struct {
Repo Repo
Build Build
Config Config
}
)
func trimElement(keys []string) []string {
@@ -58,19 +32,24 @@ func trimElement(keys []string) []string {
// Exec executes the plugin.
func (p Plugin) Exec() error {
if len(p.Config.BaseURL) == 0 || len(p.Config.Username) == 0 || len(p.Config.Token) == 0 {
log.Println("missing jenkins config")
if len(p.BaseURL) == 0 || len(p.Username) == 0 || len(p.Token) == 0 {
return errors.New("missing jenkins config")
}
auth := &Auth{
Username: p.Config.Username,
Token: p.Config.Token,
}
jenkins := NewJenkins(auth, p.Config.BaseURL)
jobs := trimElement(p.Job)
for _, value := range trimElement(p.Config.Job) {
if len(jobs) == 0 {
return errors.New("missing jenkins job")
}
auth := &Auth{
Username: p.Username,
Token: p.Token,
}
jenkins := NewJenkins(auth, p.BaseURL)
for _, value := range jobs {
jenkins.trigger(value, nil)
}
+21 -23
View File
@@ -16,9 +16,7 @@ func TestMissingConfig(t *testing.T) {
func TestMissingJenkinsConfig(t *testing.T) {
plugin := Plugin{
Config: Config{
BaseURL: "http://example.com",
},
BaseURL: "http://example.com",
}
err := plugin.Exec()
@@ -26,28 +24,28 @@ func TestMissingJenkinsConfig(t *testing.T) {
assert.NotNil(t, err)
}
func TestMissingJenkinsJob(t *testing.T) {
plugin := Plugin{
BaseURL: "http://example.com",
Username: "foo",
Token: "bar",
}
err := plugin.Exec()
assert.NotNil(t, err)
plugin.Job = []string{" "}
err = plugin.Exec()
assert.NotNil(t, err)
}
func TestPluginTriggerBuild(t *testing.T) {
plugin := Plugin{
Repo: Repo{
Name: "go-hello",
Owner: "appleboy",
},
Build: Build{
Number: 101,
Status: "success",
Link: "https://github.com/appleboy/go-hello",
Author: "Bo-Yi Wu",
Branch: "master",
Message: "update by drone line plugin.",
Commit: "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2",
},
Config: Config{
BaseURL: "http://example.com",
Username: "foo",
Token: "bar",
Job: []string{"drone-jenkins"},
},
BaseURL: "http://example.com",
Username: "foo",
Token: "bar",
Job: []string{"drone-jenkins"},
}
err := plugin.Exec()