gitlab-runner -v
Version:      11.10.1
Git revision: 1f513601
Git branch:   11-10-stable
GO version:   go1.8.7
Built:        2019-04-24T09:29:18+0000
OS/Arch:      linux/amd64
App Name:       app-api
App Version:    v2.0.1
Build version:  84d4ffb verdor
Build time:     2019-08-06T09:58:48+0800
Git revision:   84d4ffb
Git branch:     master
Golang Version: go version go1.12.2 linux/amd64
2019-07-24 10:53:34.732 11516: http server started listening on [:20000]
package main

import (
	"fmt"
)

var (
	AppName      string // 应用名称
	AppVersion   string // 应用版本
	BuildVersion string // 编译版本
	BuildTime    string // 编译时间
	GitRevision  string // Git版本
	GitBranch    string // Git分支
	GoVersion    string // Golang信息
)


func main() {
	Version()
	// 你的业务代码入口
}

// Version 版本信息
func Version() {
	fmt.Printf("App Name:t%sn", AppName)
	fmt.Printf("App Version:t%sn", AppVersion)
	fmt.Printf("Build version:t%sn", BuildVersion)
	fmt.Printf("Build time:t%sn", BuildTime)
	fmt.Printf("Git revision:t%sn", GitRevision)
	fmt.Printf("Git branch:t%sn", GitBranch)
	fmt.Printf("Golang Version: %sn", GoVersion)
}
#!/bin/bash

set -e

PROJECT_NAME="app-api"
BINARY="app-api"

OUTPUT_DIR=output
GOOS=$(go env GOOS)

APP_NAME=${PROJECT_NAME}
APP_VERSION=$(git log -1 --oneline)
BUILD_VERSION=$(git log -1 --oneline)
BUILD_TIME=$(date "+%FT%T%z")
GIT_REVISION=$(git rev-parse --short HEAD)
GIT_BRANCH=$(git name-rev --name-only HEAD)
GO_VERSION=$(go version)

CGO_ENABLED=0 go build -a -installsuffix cgo -v -mod=vendor 
-ldflags "-s -X 'main.AppName=${APP_NAME}' 
			-X 'main.AppVersion=${APP_VERSION}' 
			-X 'main.BuildVersion=${BUILD_VERSION}' 
			-X 'main.BuildTime=${BUILD_TIME}' 
			-X 'main.GitRevision=${GIT_REVISION}' 
			-X 'main.GitBranch=${GIT_BRANCH}' 
			-X 'main.GoVersion=${GO_VERSION}'" 
-o ${OUTPUT_DIR}/${BINARY} cmd/${BINARY}.go

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/1834/viewspace-2824750/,如需转载,请注明出处,否则将追究法律责任。