diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..074afe5 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,76 @@ +workspace: + base: /go + path: src/github.com/appleboy/drone-line + +clone: + git: + image: plugins/git + depth: 50 + tags: true + +pipeline: + test: + image: appleboy/golang-testing + pull: true + environment: + TAGS: netgo + secrets: [ codecov_token, telegram_token, telegram_to ] + commands: + - make vet + - make lint + - make test-vendor + - make misspell-check + - 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}' ] + secrets: [ docker_username, docker_password ] + when: + event: [ tag ] + branch: [ refs/tags/* ] + + docker: + image: plugins/docker + repo: ${DRONE_REPO} + tags: [ 'latest' ] + secrets: [ docker_username, docker_password ] + when: + event: [ push ] + branch: [ master ] + + github: + image: plugins/github-release + secrets: [ github_token ] + files: + - dist/release/* + when: + event: [ tag ] + branch: [ refs/tags/* ] + + facebook: + image: appleboy/drone-facebook + secrets: [ fb_page_token, fb_verify_token ] + pull: true + to: 1234973386524610 + when: + status: [ changed, failure ] diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index c0c9621..0000000 --- a/.travis.yml +++ /dev/null @@ -1,51 +0,0 @@ -sudo: required -language: go - -services: - - docker - -go: - - 1.6.x - - 1.7.x - - 1.8.x - - tip - -env: - global: - - DOCKER_CACHE_FILE=${HOME}/docker/cache.tar.gz - -cache: - directories: - - vendor - - ${HOME}/docker - -before_install: - - mkdir -p $GOPATH/bin - - if [ -f ${DOCKER_CACHE_FILE} ]; then gunzip -c ${DOCKER_CACHE_FILE} | docker load; fi - -install: - - export GO15VENDOREXPERIMENT=1 - - make 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 '') | 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) - # deploy from master - - if [ "$TRAVIS_BRANCH" == "master" ] && [ "$TRAVIS_GO_VERSION" == "1.7.4" ] && [ "$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.4" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ] && [ "$TRAVIS_TAG" != "" ]; then - docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD"; - make docker_deploy tag=$TRAVIS_TAG; - fi diff --git a/Makefile b/Makefile index a5d91aa..04b3b51 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,18 @@ -.PHONY: install build test html update docker_build docker_image docker_deploy clean version +DIST := dist +EXECUTABLE := drone-telegram +GOFMT ?= gofmt "-s" -VERSION := $(shell git describe --tags --always || git rev-parse --short HEAD) -DEPLOY_ACCOUNT := "appleboy" -DEPLOY_IMAGE := "drone-telegram" +# for dockerhub +DEPLOY_ACCOUNT := appleboy +DEPLOY_IMAGE := $(EXECUTABLE) + +TARGETS ?= linux darwin windows +PACKAGES ?= $(shell go list ./... | grep -v /vendor/) +GOFILES := $(shell find . -name "*.go" -type f -not -path "./vendor/*") +SOURCES ?= $(shell find . -name "*.go" -type f) +TAGS ?= +LDFLAGS ?= -X 'main.Version=$(VERSION)' +TMPDIR := $(shell mktemp -d 2>/dev/null || mktemp -d -t 'tempdir') ifneq ($(shell uname), Darwin) EXTLDFLAGS = -extldflags "-static" $(null) @@ -10,33 +20,133 @@ else EXTLDFLAGS = endif -build: - go build -ldflags="$(EXTLDFLAGS)-s -w -X main.Version=$(VERSION)" +ifneq ($(DRONE_TAG),) + VERSION ?= $(DRONE_TAG) +else + VERSION ?= $(shell git describe --tags --always || git rev-parse --short HEAD) +endif -test: - go test -v -coverprofile=coverage.txt +all: build + +fmt: + $(GOFMT) -w $(GOFILES) + +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; + +unconvert: + @which unconvert > /dev/null; if [ $$? -ne 0 ]; then \ + go get -u github.com/mdempsky/unconvert; \ + fi + for PKG in $(PACKAGES); do unconvert -v $$PKG || exit 1; done; + +.PHONY: misspell-check +misspell-check: + @hash misspell > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ + go get -u github.com/client9/misspell/cmd/misspell; \ + fi + misspell -error $(GOFILES) + +.PHONY: misspell +misspell: + @hash misspell > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ + go get -u github.com/client9/misspell/cmd/misspell; \ + fi + misspell -w $(GOFILES) + +.PHONY: fmt-check +fmt-check: + # get all go files and run go fmt on them + @diff=$$($(GOFMT) -d $(GOFILES)); \ + if [ -n "$$diff" ]; then \ + echo "Please run 'make fmt' and commit the result:"; \ + echo "$${diff}"; \ + exit 1; \ + fi; + +.PHONY: test-vendor +test-vendor: + @hash govendor > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ + go get -u github.com/kardianos/govendor; \ + fi + govendor list +unused | tee "$(TMPDIR)/wc-gitea-unused" + [ $$(cat "$(TMPDIR)/wc-gitea-unused" | wc -l) -eq 0 ] || echo "Warning: /!\\ Some vendor are not used /!\\" + + govendor list +outside | tee "$(TMPDIR)/wc-gitea-outside" + [ $$(cat "$(TMPDIR)/wc-gitea-outside" | wc -l) -eq 0 ] || exit 1 + + govendor status || exit 1 + +test: fmt-check + for PKG in $(PACKAGES); do go test -cover -coverprofile $$GOPATH/src/$$PKG/coverage.txt $$PKG || exit 1; done; html: go tool cover -html=coverage.txt -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 -t $(DEPLOY_ACCOUNT)/$(DEPLOY_IMAGE) . -docker: docker_build docker_image +docker: static_build docker_image docker_deploy: ifeq ($(tag),) @echo "Usage: make $@ 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)