Skip to content
This repository has been archived by the owner on Dec 20, 2022. It is now read-only.

[patch] add go binary version during build time #43

Merged
merged 7 commits into from
Jan 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ jobs:
<<: *default
steps:
- setup_remote_docker: *setup_remote_docker
- attach_workspace:
at: .
- checkout
- run:
name: check docker version
Expand All @@ -51,8 +53,17 @@ jobs:
- run:
name: docker image build
command: |
# env DOCKER_BUILDKIT=1 docker build -t ${REPO_NAME}/${IMAGE_NAME}:latest .
docker build -t ${REPO_NAME}/${IMAGE_NAME}:latest .
TAG=`cat ./.tag`
if [ ! -z "$TAG" ]; then
docker build --build-arg APP_VERSION=${TAG} -t ${REPO_NAME}/${IMAGE_NAME}:latest .
else
# env DOCKER_BUILDKIT=1 docker build -t ${REPO_NAME}/${IMAGE_NAME}:latest .
WindzCUHK marked this conversation as resolved.
Show resolved Hide resolved
docker build -t ${REPO_NAME}/${IMAGE_NAME}:latest .
fi
- run:
name: check build version
command: |
docker run --rm --name ${IMAGE_NAME} ${REPO_NAME}/${IMAGE_NAME}:latest --version
- run:
name: save image
command: |
Expand Down Expand Up @@ -80,7 +91,7 @@ jobs:
command: |
docker login -u ${DOCKER_USER} -p ${DOCKER_PASS}
- run:
name: push image to registory
name: push image to registry
command: |
docker push ${REPO_NAME}/${IMAGE_NAME}
publish:
Expand All @@ -102,7 +113,7 @@ jobs:
command: |
docker login -u ${DOCKER_USER} -p ${DOCKER_PASS}
- run:
name: push image to registory
name: push image to registry
command: |
docker push ${REPO_NAME}/${IMAGE_NAME}
versioning:
Expand Down Expand Up @@ -139,6 +150,11 @@ jobs:
echo "0.0.1" > ./.tag
fi
fi
- run:
name: echo version
command: |
TAG_FILE='./.tag' \
&& if [[ -s ${TAG_FILE} ]]; then echo "TAG: `cat "${TAG_FILE}"`"; else echo "TAG: (${TAG_FILE} is empty)"; fi
- persist_to_workspace:
root: .
paths:
Expand Down Expand Up @@ -186,7 +202,9 @@ workflows:
build:
jobs:
- test
- build
- build:
requires:
- versioning
- publish_nightly:
requires:
- test
Expand Down Expand Up @@ -222,4 +240,3 @@ workflows:
ignore: /.*/
tags:
only: /[0-9]+\.[0-9]+\.[0-9]+/

10 changes: 7 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
FROM golang:1.13-alpine AS base

RUN set -eux \
&& apk update \
&& apk --no-cache add ca-certificates \
&& apk --no-cache add --virtual build-dependencies cmake g++ make unzip curl upx git

Expand All @@ -14,18 +15,21 @@ RUN GO111MODULE=on go mod download
FROM base AS builder

ENV APP_NAME authorization-proxy
ARG APP_VERSION='development version'

COPY . .

RUN CGO_ENABLED=1 \
RUN BUILD_TIME=$(date -u +%Y%m%d-%H%M%S) \
&& GO_VERSION=$(go version | cut -d" " -f3,4) \
&& CGO_ENABLED=1 \
CGO_CXXFLAGS="-g -Ofast -march=native" \
CGO_FFLAGS="-g -Ofast -march=native" \
CGO_LDFLAGS="-g -Ofast -march=native" \
GOOS=$(go env GOOS) \
GOARCH=$(go env GOARCH) \
GO111MODULE=on \
go build --ldflags '-s -w -linkmode "external" -extldflags "-static -fPIC -m64 -pthread -std=c++11 -lstdc++"' -a -tags "cgo netgo" -installsuffix "cgo netgo" -o "${APP_NAME}" \
&& upx -9 -o "/usr/bin/${APP_NAME}" "${APP_NAME}"
go build --ldflags "-s -w -linkmode 'external' -extldflags '-static -fPIC -m64 -pthread -std=c++11 -lstdc++' -X 'main.Version=${APP_VERSION} at ${BUILD_TIME} by ${GO_VERSION}'" -a -tags "cgo netgo" -installsuffix "cgo netgo" -o "${APP_NAME}" \
&& upx --best -o "/usr/bin/${APP_NAME}" "${APP_NAME}"

RUN apk del build-dependencies --purge \
&& rm -rf "${GOPATH}"
Expand Down
19 changes: 18 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ import (
"github.com/yahoojapan/authorization-proxy/usecase"
)

// Version is set by the build command via LDFLAGS
var Version string

// params is the data model for Authorization Proxy command line arguments.
type params struct {
configFilePath string
Expand Down Expand Up @@ -124,7 +127,14 @@ func main() {
}

if p.showVersion {
glg.Infof("authorization-proxy version -> %s", config.GetVersion())
err := glg.Infof("authorization-proxy version -> %s", getVersion())
if err != nil {
glg.Fatal(err)
}
err = glg.Infof("authorization-proxy config version -> %s", config.GetVersion())
if err != nil {
glg.Fatal(err)
}
return
}

Expand All @@ -150,3 +160,10 @@ func main() {
return
}
}

func getVersion() string {
if Version == "" {
return "development version"
}
return Version
}
54 changes: 54 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"os"
"strings"
"testing"

"github.com/pkg/errors"
Expand Down Expand Up @@ -93,6 +94,28 @@ func Test_run(t *testing.T) {
checkFunc func(config.Config) error
}
tests := []test{
func() test {
return test{
name: "enable debug",
args: args{
cfg: config.Config{
Debug: true,
EnableColorLogging: true,
},
},
checkFunc: func(cfg config.Config) error {
got := run(cfg)
wantPrefix := "daemon init error"
if len(got) != 1 {
return errors.New("len(got) != 1")
}
if !strings.HasPrefix(got[0].Error(), wantPrefix) {
return errors.Errorf("got: [%v], wantPrefix: [%v]", got[0], wantPrefix)
}
return nil
},
}
}(),
func() test {
return test{
name: "run error",
Expand Down Expand Up @@ -149,3 +172,34 @@ func Test_run(t *testing.T) {
})
}
}

func Test_getVersion(t *testing.T) {
tests := []struct {
name string
want string
beforeFunc func()
}{
{
name: "default",
want: "development version",
beforeFunc: func() {},
},
{
name: "Version already set",
want: "1.2.333",
beforeFunc: func() {
Version = "1.2.333"
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.beforeFunc != nil {
tt.beforeFunc()
}
if got := getVersion(); got != tt.want {
t.Errorf("getVersion() = %v, want %v", got, tt.want)
}
})
}
}