Created
November 11, 2020 02:24
-
-
Save ImSingee/b430c15958f7dc9e18a8dacfd39ffbf8 to your computer and use it in GitHub Desktop.
Go 程序编译脚本,在编译过程中自动加入 Git Tag、Git Commit ID、利用 Git Tag 提取出来的版本信息、编译环境信息、编译时间
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
echo Build All | |
echo Build for linux | |
(CGO_ENABLED=0 GOOS=linux ./build.sh) | |
echo Build for windows | |
(CGO_ENABLED=0 GOOS=windows ./build.sh) | |
echo Build for macOS | |
(CGO_ENABLED=0 GOOS=darwin ./build.sh) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
STATUS="$(git status -s | awk '{gsub(/^\s+|\s+$/, "");print}')" | |
DESCRIBE_TAG="$(git describe --tags 2>/dev/null)" | |
LATEST_TAG="$(git describe --tags --abbrev=0 2>/dev/null)" | |
LATEST_COMMIT="$(git rev-parse HEAD 2>/dev/null)" | |
if [ -n "$STATUS" ]; then | |
echo "WARNING: 工作区有尚未提交的内容,版本号增加 -dev" 1>&2 | |
VERSION_suffix="-dev" | |
COMMIT_suffix="*" | |
TAG_suffix="*" | |
fi | |
if [ -z "$LATEST_COMMIT" ]; then | |
COMMIT="UNKNOWN$COMMIT_suffix" | |
else | |
COMMIT="${LATEST_COMMIT:0:8}$COMMIT_suffix" | |
fi | |
if [ -z "$DESCRIBE_TAG" ]; then | |
TAG="UNKNOWN$TAG_suffix" | |
else | |
TAG="$DESCRIBE_TAG$TAG_suffix" | |
fi | |
echo "Commit: $COMMIT, Tag: $TAG" | |
if [ -z "$LATEST_TAG" ] || [[ $LATEST_TAG != v* ]]; then | |
echo "WARNING: 最新的 tag 不满足 vX.Y.Z 的格式,版本号会被标记为 UNKNOWN" 1>&2 | |
VERSION="UNKNOWN" | |
else | |
VERSION="${LATEST_TAG:1}" | |
fi | |
if [[ $DESCRIBE_TAG != "$LATEST_TAG" ]]; then | |
echo "WARNING: 在最近的 tag 版本后仍然有提交,版本号增加 -SNAPSHOT" 1>&2 | |
VERSION="${VERSION}-SNAPSHOT" | |
fi | |
VERSION="${VERSION}${VERSION_suffix}" | |
echo "Build Version $VERSION" | |
GOVERSION=$(go version | awk '{gsub(/^\s+|\s+$/, "");print}') | |
LDFLAGS="\ | |
-X \"main.VERSION=${VERSION}\" \ | |
-X \"main.GOVERSION=$GOVERSION\" \ | |
-X \"main.GITCOMMIT=${COMMIT}\" \ | |
-X \"main.GITTAG=${TAG}\" \ | |
-X \"main.BUILDTIME=$(date +%Y-%m-%dT%H:%M:%S%z)\" \ | |
" | |
OS="$(go env GOOS)" | |
FILENAME="out/p426-${OS}-${VERSION}" | |
if [ "$OS" == "windows" ]; then | |
FILENAME="${FILENAME}.exe" | |
fi | |
echo "Build to $FILENAME" | |
go build -ldflags "-s -w ${LDFLAGS}" -o "$FILENAME" main.go | |
chmod 644 "$FILENAME" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
) | |
var VERSION = "DEV" | |
var GOVERSION = "UNKNOWN" | |
var GITCOMMIT = "UNKNOWN*" | |
var GITTAG = "UNKNOWN*" | |
var BUILDTIME = "UNKNOWN" | |
func version() string { | |
return fmt.Sprintf(`%s | |
Git commit: %s, tag: %s, | |
Build with %s | |
Build at %s`, VERSION, GITCOMMIT, GITTAG, GOVERSION, BUILDTIME) | |
} | |
func main() { | |
args := os.Args | |
if len(args) == 2 { | |
switch args[1] { | |
case "-v", "--version", "version": | |
fmt.Println(version()) | |
os.Exit(1) | |
} | |
} | |
// Main Code | |
fmt.Println("Done") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment