From 109c8bbcb9ca4accd227929c341121058d0f8ee1 Mon Sep 17 00:00:00 2001 From: Ryan Eschinger Date: Tue, 25 Jun 2019 22:00:44 -0400 Subject: [PATCH] Goreleaser (#1) --- .gitignore | 3 ++- .goreleaser.yml | 43 +++++++++++++++++++++++++++++++++++++++++++ .travis.yml | 24 ++++++++++++++++++++++++ Dockerfile | 20 +++----------------- Makefile | 24 +++++++++++++----------- VERSION | 1 - cmd/root.go | 7 ++++++- cmd/version.go | 24 ------------------------ go.sum | 3 +++ 9 files changed, 94 insertions(+), 55 deletions(-) create mode 100644 .goreleaser.yml create mode 100644 .travis.yml delete mode 100644 VERSION delete mode 100644 cmd/version.go diff --git a/.gitignore b/.gitignore index 8e21434..37f28ff 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -/kfilt +kfilt +dist diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 0000000..9b9b610 --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,43 @@ +project_name: kfilt +before: + hooks: + - go mod download +builds: +- env: + - CGO_ENABLED=0 + goos: + - windows + - linux + - darwin + # Default is `-s -w -X main.version={{.Version}} -X main.commit={{.ShortCommit}} -X main.date={{.Date}} -X main.builtBy=goreleaser`. + ldflags: + - -s -w -X github.com/ryane/kfilt/cmd.Version={{.Version}} -X github.com/ryane/kfilt/cmd.GitCommit={{.ShortCommit}} +archives: +- format: binary +checksum: + name_template: 'checksums.txt' +snapshot: + name_template: "master" +changelog: + sort: asc + filters: + exclude: + - '^docs:' + - '^test:' + - Merge pull request + - Merge branch +dockers: +- goos: linux + goarch: amd64 + goarm: '' + binaries: + - kfilt + image_templates: + - "ryane/{{.ProjectName}}:latest" + - "ryane/{{.ProjectName}}:{{ .Tag }}" + skip_push: false + dockerfile: Dockerfile + build_flag_templates: + - "--label=org.label-schema.schema-version=1.0" + - "--label=org.label-schema.version={{.Version}}" + - "--label=org.label-schema.name={{.ProjectName}}" diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..9eee16c --- /dev/null +++ b/.travis.yml @@ -0,0 +1,24 @@ +language: go + +go: +- 1.12.x + +env: + global: + - GO111MODULE=on + +install: true + +services: +- docker + +after_success: +- test -n "$TRAVIS_TAG" && docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD" + +deploy: +- provider: script + skip_cleanup: true + script: curl -sL https://git.io/goreleaser | bash + on: + tags: true + condition: $TRAVIS_OS_NAME = linux diff --git a/Dockerfile b/Dockerfile index dd37a22..825e4ae 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,17 +1,3 @@ -FROM golang:1.12-alpine3.9 as builder - -RUN apk --no-cache add make git gcc musl-dev && rm -rf /var/cache/apk/* -WORKDIR /go/src/github.com/ryane/kfilt -RUN GO111MODULE=off go get github.com/ahmetb/govvv -ENV GO111MODULE=on -COPY go.mod . -COPY go.sum . -RUN go mod download - -COPY . . -RUN govvv install -pkg github.com/ryane/kfilt/cmd - -FROM alpine:3.9 -RUN apk --no-cache add ca-certificates && rm -rf /var/cache/apk/* -COPY --from=builder /go/bin/kfilt /bin/kfilt -ENTRYPOINT ["/bin/kfilt"] +FROM scratch +COPY kfilt / +ENTRYPOINT ["/kfilt"] diff --git a/Makefile b/Makefile index 6e854ad..55c5441 100644 --- a/Makefile +++ b/Makefile @@ -1,20 +1,22 @@ -VERSION ?= $(shell cat VERSION) -GIT_DIRTY = $(shell test -n "`git status --porcelain`" && echo "-dirty") +GIT_SHA = $(shell git rev-parse --short HEAD) +GIT_DIRTY = $(shell test -n "`git status --porcelain`" && echo "-dirty") +GIT_BRANCH = $(shell git rev-parse --abbrev-ref HEAD) .PHONY: all -all: build +all: test build .PHONY: build -build: deps - govvv build -pkg github.com/ryane/kfilt/cmd +build: + go build + +.PHONY: test +test: + go test -v ./... .PHONY: docker docker: - docker build -t ryane/kfilt:${VERSION}${GIT_DIRTY} . + CGO_ENABLED=0 GOOS=linux go build -a -tags netgo -ldflags "-w -X github.com/ryane/kfilt/cmd.Version=${GIT_BRANCH} -X github.com/ryane/kfilt/cmd.GitCommit=${GIT_SHA}${GIT_DIRTY}" . + docker build -t ryane/kfilt:${GIT_SHA}${GIT_DIRTY} . push: build - docker push ryane/kfilt:${VERSION}${GIT_DIRTY} - -.PHONY: deps -deps: - GO111MODULE=off go get github.com/ahmetb/govvv + docker push ryane/kfilt:${GIT_SHA}${GIT_DIRTY} diff --git a/VERSION b/VERSION deleted file mode 100644 index 4e379d2..0000000 --- a/VERSION +++ /dev/null @@ -1 +0,0 @@ -0.0.2 diff --git a/cmd/root.go b/cmd/root.go index a297e22..0f9189e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -13,6 +13,8 @@ import ( "github.com/spf13/cobra" ) +var GitCommit, Version string + type root struct { kind string name string @@ -31,13 +33,16 @@ func newRootCommand(args []string) *cobra.Command { os.Exit(1) } }, + Version: func() string { + return fmt.Sprintf("%s (%s)\n", Version, GitCommit) + }(), } rootCmd.Flags().StringVarP(&root.kind, "kind", "k", "", "Only include resources of kind") rootCmd.Flags().StringVarP(&root.name, "name", "n", "", "Only include resources of name") rootCmd.Flags().StringVarP(&root.filename, "filename", "f", "", "Read manifests from file") - rootCmd.AddCommand(newVersionCommand()) + rootCmd.SetVersionTemplate(`{{.Version}}`) return rootCmd } diff --git a/cmd/version.go b/cmd/version.go deleted file mode 100644 index 2fc7b18..0000000 --- a/cmd/version.go +++ /dev/null @@ -1,24 +0,0 @@ -package cmd - -import ( - "fmt" - - "github.com/spf13/cobra" -) - -var GitCommit, GitState, Version string - -func newVersionCommand() *cobra.Command { - return &cobra.Command{ - Use: "version", - Short: "Display the kfilt version", - Run: func(cmd *cobra.Command, args []string) { - - if GitState != "clean" { - fmt.Printf("%s (%s-%s)\n", Version, GitCommit, GitState) - } else { - fmt.Printf("%s (%s)\n", Version, GitCommit) - } - }, - } -} diff --git a/go.sum b/go.sum index 2552526..3aa08da 100644 --- a/go.sum +++ b/go.sum @@ -23,9 +23,11 @@ github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d/go.mod h1:sJBsC github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/json-iterator/go v0.0.0-20180701071628-ab8a2e0c74be h1:AHimNtVIpiBjPUhEF5KNCkrUyqTSA5zWUl8sQ2bfGBE= github.com/json-iterator/go v0.0.0-20180701071628-ab8a2e0c74be/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= +github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= @@ -75,6 +77,7 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20181227161524-e6919f6577db h1:6/JqlYfC1CCaLnGceQTI+sDGhC9UBSPAsBqI0Gun6kU= golang.org/x/text v0.3.1-0.20181227161524-e6919f6577db/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/inf.v0 v0.9.0 h1:3zYtXIO92bvsdS3ggAdA8Gb4Azj0YU+TVY1uGYNFA8o=