将版本信息写入Go编译的二进制文件
Makefile
SHELL := /bin/bash
BASEDIR = $(shell pwd)
DOCKER_TARGET=/lee/hello
# build with version infos
versionDir = "main"
gitTag = $(shell if [ "`git describe --tags --abbrev=0 2>/dev/null`" != "" ];then git describe --tags --abbrev=0; else git log --pretty=format:'%h' -n 1; fi)
buildDate = $(shell TZ=UTC date +%FT%T%z)
gitCommit = $(shell git log --pretty=format:'%H' -n 1)
ldflags="-w -X ${versionDir}.gitTag=${gitTag} -X ${versionDir}.buildDate=${buildDate} -X ${versionDir}.gitCommit=${gitCommit}"
build:
go build -ldflags ${ldflags} -o main ./
docker-build:
docker build . -t ${DOCKER_TARGET}:$(gitTag)
docker-release:
docker push ${DOCKER_TARGET}:$(gitTag)
Dockerfile
# build stage
FROM golang:1.14 as build
ENV GOPROXY=""
ENV CGO_ENABLED=0
WORKDIR /app
COPY . /app
RUN make build
# production stage
FROM alpine as production
WORKDIR /app
#COPY ./conf/ /app/conf
COPY --from=build /app/main /app
EXPOSE 8081
ENTRYPOINT ["/app/main"]