From 240fa90a0a4e12ef73c75fe60bca566fa7b24e61 Mon Sep 17 00:00:00 2001 From: anjmao Date: Sun, 7 Apr 2019 16:39:02 +0300 Subject: [PATCH] chore: add release scripts --- .gitignore | 2 ++ Dockerfile | 3 ++- Makefile | 40 ++++++++++++++++++++++++++++++++++++---- README.md | 16 ++++++++++++++-- go.sum | 1 + main.go | 36 +++++++++++++++++++++++------------- 6 files changed, 78 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index eb1d888..eca6e86 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .idea vcheck +build +dist \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 4f57953..976f4c8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,8 @@ COPY go.mod . COPY go.sum . RUN go mod download COPY . . -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -o /app/vcheck +ARG VERSION +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -ldflags="-X main.Version=$VERSION" -o /app/vcheck FROM alpine RUN apk update && apk add ca-certificates diff --git a/Makefile b/Makefile index 131de7f..9501e13 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,40 @@ -VERSION:=$(shell git describe --tags --always --dirty) -IMAGE:=anjmao/vcheck:$(VERSION) +GHACCOUNT := anjmao +NAME := vcheck +VERSION := $(shell git describe --tags --always --dirty) +DOCKER_IMAGE_NAME := $(GHACCOUNT)/$(NAME):$(VERSION) +LATFORM := $(shell go env | grep GOHOSTOS | cut -d '"' -f 2) +ARCH := $(shell go env | grep GOARCH | cut -d '"' -f 2) +BRANCH := $(shell git rev-parse --abbrev-ref HEAD) +LDFLAGS := -ldflags "-X main.Version=$(VERSION)" container: - @docker build -t $(IMAGE) . + @docker build --build-arg VERSION=$(VERSION) -t $(DOCKER_IMAGE_NAME) . push: - @docker push $(IMAGE) \ No newline at end of file + @docker push $(DOCKER_IMAGE_NAME) + +compile: + @rm -rf build/ + @gox $(LDFLAGS) \ + -osarch="darwin/amd64" \ + -osarch="linux/amd64" \ + -osarch="windows/amd64" \ + -output "build/$(NAME)_$(VERSION)_{{.OS}}_{{.Arch}}/$(NAME)" \ + ./... + +dist: compile + $(eval FILES := $(shell ls build)) + @rm -rf dist && mkdir dist + @for f in $(FILES); do \ + (cd $(shell pwd)/build/$$f && tar -cvzf ../../dist/$$f.tar.gz *); \ + (cd $(shell pwd)/dist && shasum -a 512 $$f.tar.gz > $$f.sha512); \ + echo $$f; \ + done + +release: dist + @latest_tag=$$(git describe --tags `git rev-list --tags --max-count=1`); \ + comparison="$$latest_tag..HEAD"; \ + if [ -z "$$latest_tag" ]; then comparison=""; fi; \ + changelog=$$(git log $$comparison --oneline --no-merges --reverse); \ + github-release $(GHACCOUNT)/$(NAME) $(VERSION) $(BRANCH) "**Changelog**
$$changelog" 'dist/*'; \ + git pull \ No newline at end of file diff --git a/README.md b/README.md index 7e369d7..381ab5c 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ vcheck \ --target=staging.my.grpcapi.net:443 \ --method=/version.Version/GetVersion \ --client=grpc \ - --version=${MY_CI_BUILD_NUMBER} \ + --expect=${MY_CI_BUILD_NUMBER} \ --count=12 \ --sleep=5 ``` @@ -43,7 +43,7 @@ vcheck \ --target=http://staging.my.grpcapi.net \ --method=/api/version \ --client=http \ - --version=${MY_CI_BUILD_NUMBER} \ + --expect=${MY_CI_BUILD_NUMBER} \ --count=12 \ --sleep=5 ``` @@ -55,3 +55,15 @@ vheck util is also available as a docker image ```bash docker run --rm anjmao:vcheck --help ``` + +### Release + +Before release install gox and github-release tools +```bash +go GO111MODULE=off go get github.com/mitchellh/gox +go GO111MODULE=off go get github.com/c4milo/github-release +``` + +```bash +make release +``` \ No newline at end of file diff --git a/go.sum b/go.sum index 77afecb..58a5089 100644 --- a/go.sum +++ b/go.sum @@ -10,6 +10,7 @@ golang.org/x/net v0.0.0-20180826012351-8a410e7b638d h1:g9qWBGx4puODJTMVyoPrpoxPF golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522 h1:Ve1ORMCxvRmSXBwJK+t3Oy+V2vRW2OetUQBq4rJIkZE= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/main.go b/main.go index d3e942d..6dec56c 100644 --- a/main.go +++ b/main.go @@ -11,29 +11,33 @@ import ( ) var ( - target = flag.String("target", "", "-target staging.mpost-api.guardtek.net:443") - expectedVersion = flag.String("version", "", "Expected version") - method = flag.String("method", "/version.Version/GetVersion", "Version check endpoint path") - clientType = flag.String("client", "grpc", "Version check client type") - checkCount = flag.Int("count", 12, "Check count") - sleepAfterCheck = flag.Int("sleep", 5, "Sleep after check in seconds") - showHelp = flag.Bool("help", false, "Show help") + target = flag.String("target", "", "Version check endpoint target") + expectedBuildVersion = flag.String("expect", "", "Expected version") + method = flag.String("method", "/version.Version/GetVersion", "Version check endpoint path") + clientType = flag.String("client", "grpc", "Version check client type") + checkCount = flag.Int("count", 12, "Check count") + sleepAfterCheck = flag.Int("sleep", 5, "Sleep after check in seconds") + printHelp = flag.Bool("help", false, "Print help") + printVersion = flag.Bool("version", false, "Print vcheck version") ) +var Version = "dev" + const checkTimeout = 10 * time.Second const usageStr = ` Usage: vcheck [options] Options: --target Target host including port. (e.g --target service.mydomain.com:443) - --version Expected version (e.g -v 1.2.3) + --expect Expected version (e.g -v 1.2.3) --method Version check endpoint (default: /debug.Debug/GetVersion) --client Client type (grpc, http) --count Check count (default: 12) --sleep Sleep duration after check in seconds (default: 5) Other options: - --help + --help Print help + --version Print vcheck util version ` func usage() { @@ -43,7 +47,13 @@ func usage() { func main() { flag.Parse() - if *showHelp || *target == "" || *expectedVersion == "" { + + if *printVersion { + fmt.Println(Version) + os.Exit(0) + } + + if *printHelp || *target == "" || *expectedBuildVersion == "" { usage() } @@ -59,8 +69,8 @@ func main() { continue } actualVersion = v.BuildVersion - fmt.Printf("expected version %s, got %s\n", *expectedVersion, actualVersion) - if actualVersion == *expectedVersion { + fmt.Printf("expected version %s, got %s\n", *expectedBuildVersion, actualVersion) + if actualVersion == *expectedBuildVersion { fmt.Println("deployment successful") return } @@ -68,7 +78,7 @@ func main() { time.Sleep(sleep) } - log.Fatalf("deployment failed: expected version %s, got %s\n", *expectedVersion, actualVersion) + log.Fatalf("deployment failed: expected version %s, got %s\n", *expectedBuildVersion, actualVersion) } func getVersion(c client.Client) (*client.GetVersionReply, error) {