diff --git a/.circleci/config.yml b/.circleci/config.yml index 91f7847..8a0e697 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -44,12 +44,18 @@ jobs: - run: name: Code Quality command: make lint + - run: + name: Release + command: | + export GOVERSION=$(go version | awk '{sub("^go","",$3);print $3;}') + scripts/goreleaser --rm-dist --skip-publish ${CIRCLE_TAG:-"--snapshot"} + rm -rf $(find ./dist/* -type d) || true - store_test_results: path: test-results/ - store_artifacts: path: test-results/ - store_artifacts: - path: /go/bin/vault-plugin-splunk + path: dist/ - save_cache: name: Saving Cache for vendor @@ -63,10 +69,8 @@ jobs: - /tmp/go/cache - persist_to_workspace: - root: /go/bin - # Must be relative path from root - paths: - - vault-plugin-splunk + root: dist/ + paths: . publish-github-release: docker: @@ -78,9 +82,8 @@ jobs: name: "Publish Release on GitHub" command: | go get github.com/tcnksm/ghr - VERSION=$(artifacts/vault-plugin-splunk -version | awk '{print $1}') - zip -jr vault-plugin-splunk artifacts/ - ghr -t ${GITHUB_TOKEN} -u ${CIRCLE_PROJECT_USERNAME} -r ${CIRCLE_PROJECT_REPONAME} -c ${CIRCLE_SHA1} -recreate ${VERSION} ./vault-plugin-splunk.zip + VERSION=$(echo artifacts/vault-plugin-splunk_*_*.zip | awk -F_ '{print $2;exit;}') + ghr -t ${GITHUB_TOKEN} -u ${CIRCLE_PROJECT_USERNAME} -r ${CIRCLE_PROJECT_REPONAME} -c ${CIRCLE_SHA1} -recreate ${VERSION} ./artifacts/ workflows: version: 2 diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 0000000..5d43687 --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,33 @@ +before: +# hooks: +# # you may remove this if you don't use vgo +# - go mod download +builds: +- main: cmd/vault-plugin-splunk/main.go + ldflags: + - -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.goVersion={{.Env.GOVERSION}} + env: + - CGO_ENABLED=0 + goos: + - linux + - darwin + goarch: + - amd64 +archives: +- replacements: + 386: i386 + format: zip + format_overrides: + - goos: linux + format: tar.bz2 + name_template: "{{.ProjectName}}_{{.Version}}_{{.Os}}_{{.Arch}}" +checksum: + name_template: 'checksums.txt' +snapshot: + name_template: "{{.Version}}-next" +changelog: + sort: asc + filters: + exclude: + - '^docs:' + - '^test:' diff --git a/Makefile b/Makefile index 87ee0cb..d68c5ed 100644 --- a/Makefile +++ b/Makefile @@ -1,18 +1,8 @@ -VERSION := $(shell git describe --tags --always 2>/dev/null) -SHORT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo dev) -GO_VERSION := $(shell go version | awk '{ print $$3}' | sed 's/^go//') - TESTREPORT := test-results.xml # XXX BUG(mweber) "go env GOBIN" is empty? GOBIN := $(shell go env GOPATH)/bin -LD_FLAGS_PKG ?= main -LD_FLAGS := -LD_FLAGS += -X "$(LD_FLAGS_PKG).version=$(VERSION)" -LD_FLAGS += -X "$(LD_FLAGS_PKG).commit=$(SHORT_COMMIT)" -LD_FLAGS += -X "$(LD_FLAGS_PKG).goVersion=$(GO_VERSION)" - .PHONY: all all: build lint test @@ -22,7 +12,7 @@ dep: prereq .PHONY: build build: dep vault.hcl - go install -ldflags '$(LD_FLAGS)' ./... + go install ./... vault.hcl: vault.hcl.in sed -e 's;@@GOBIN@@;$(GOBIN);g' < $< > $@ @@ -60,4 +50,4 @@ prereq: .PHONY: clean clean: # XXX clean - rm -rf vault.hcl $(TESTREPORT) vendor/ + rm -rf vault.hcl $(TESTREPORT) vendor/ dist/ diff --git a/cmd/vault-plugin-splunk/main.go b/cmd/vault-plugin-splunk/main.go index 42cbd5c..734630f 100644 --- a/cmd/vault-plugin-splunk/main.go +++ b/cmd/vault-plugin-splunk/main.go @@ -11,9 +11,12 @@ import ( splunk "github.com/splunk/vault-plugin-splunk" ) +// nolint: gochecknoglobals var ( - version string - goVersion string + version = "dev" + commit = "" + date = "" + goVersion = "" ) func main() { @@ -25,9 +28,17 @@ func main() { // #nosec G104 flags.Parse(os.Args[1:]) + printField := func(field, value string) { + if field != "" && value != "" { + fmt.Printf("%s: %s\n", field, value) + } + } switch { case *printVersion: - fmt.Printf("%s (golang %s)\n", version, goVersion) + printField("version", version) + printField("commit", commit) + printField("date", date) + printField("go", goVersion) os.Exit(0) } diff --git a/scripts/goreleaser b/scripts/goreleaser new file mode 100755 index 0000000..043e917 --- /dev/null +++ b/scripts/goreleaser @@ -0,0 +1,28 @@ +#!/bin/sh +set -e + +TAR_FILE="/tmp/goreleaser.tar.gz" +RELEASES_URL="https://github.com/goreleaser/goreleaser/releases" +test -z "$TMPDIR" && TMPDIR="$(mktemp -d)" + +last_version() { + curl -sL -o /dev/null -w %{url_effective} "$RELEASES_URL/latest" | + rev | + cut -f1 -d'/'| + rev +} + +download() { + test -z "$VERSION" && VERSION="$(last_version)" + test -z "$VERSION" && { + echo "Unable to get goreleaser version." >&2 + exit 1 + } + rm -f "$TAR_FILE" + curl -s -L -o "$TAR_FILE" \ + "$RELEASES_URL/download/$VERSION/goreleaser_$(uname -s)_$(uname -m).tar.gz" +} + +download +tar -xf "$TAR_FILE" -C "$TMPDIR" +"${TMPDIR}/goreleaser" "$@"