-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
82 lines (68 loc) · 1.75 KB
/
build.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env bash
CURR_PATH=$(cd `dirname $0`;pwd)
VERSION_PATH="`cat $CURR_PATH/go.mod | grep ^module | awk '{print $2}'`/version"
function _info(){
local msg=$1
local now=`date '+%Y-%m-%d %H:%M:%S'`
echo "$now $msg"
}
function _version(){
local msg=$1
local now=`date '+%Y-%m-%d %H:%M:%S'`
echo "$now $msg"
}
function get_tag () {
local tag=$(git describe --tags 2>>/dev/null)
if [ -n "$tag" ];then
tag=$(echo $tag | cut -d '-' -f 1)
else
tag='unknown'
fi
echo $tag
}
function get_branch () {
local branch=$(git rev-parse --abbrev-ref HEAD)
if [ -z "$branch" ]; then
branch='unknown'
fi
echo $branch
}
function get_commit () {
local commit=$(git rev-parse HEAD)
if [ -z "$commit" ]; then
commit='unknown'
fi
echo $commit
}
function main() {
local platform=$1
local main_file=$2
_info "开始构建 ..."
TAG=$(get_tag)
BRANCH=$(get_branch)
COMMIT=$(get_commit)
DATE=$(date '+%Y-%m-%d %H:%M:%S')
version=$(go version | grep -o 'go[0-9].[0-9].*')
_version "构建版本的时间(Build Time): $DATE"
_version "当前构建的版本(Git Tag ): $TAG"
_version "当前构建的分支(Git Branch): $BRANCH"
_version "当前构建的提交(Git Commit): $COMMIT"
ldflags="-X '$VERSION_PATH.GitTAG=$TAG' -X '$VERSION_PATH.GitBranch=$BRANCH' -X '$VERSION_PATH.GitCommit=$COMMIT' -X '$VERSION_PATH.BuildTime=$DATE' -X '$VERSION_PATH.GoVersion=$version'"
case $platform in
"linux")
_info "开始构建Linux平台版本 ..."
GOOS=linux GOARCH=amd64 \
CGO_ENABLED=0 go build -ldflags "$ldflags" $main_file
;;
*)
_info "开始本地构建 ..."
CGO_ENABLED=0 go build -ldflags "$ldflags" $main_file
;;
esac
if [ $? -ne 0 ];then
_info "构建失败"
exit 1
fi
_info "程序构建完成: $CURR_PATH"
}
main ${1:-local} .