forked from cadence-workflow/cadence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-ldflags.sh
executable file
·47 lines (39 loc) · 1.45 KB
/
get-ldflags.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
#!/bin/bash
set -eo pipefail
# add verbose env var for debugging
test -n "$V" && set -x
MODE=$1
if [ "$MODE" != "LDFLAG" ] && [ "$MODE" != "ECHO" ]; then
echo "Usage: $0 <LDFLAG|ECHO>"
exit 1
fi
GIT_REVISION=$(git log -1 --format=%cI-%h) # use commit date time and hash: e.g. 2021-07-27 19:36:53 -0700-40c5f1896, doc: https://git-scm.com/docs/pretty-formats
GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
BUILD_DATE=$(date '+%F-%T') # outputs something in this format 2017-08-21-18:58:45
BUILD_TS_UNIX=$(date '+%s') # second since epoch
BASE_PACKAGE=github.com/uber/cadence/common/metrics
if [ -z ${CADENCE_RELEASE_VERSION} ]; then
# If not set CADENCE_RELEASE_VERSION, then use the most recent tag.
RELEASE_VERSION=$(git describe --tags --abbrev=0 --dirty 2>/dev/null || echo unknown)
else
# If passing a CADENCE_RELEASE_VERSION explicitly, then use it
RELEASE_VERSION=${CADENCE_RELEASE_VERSION}
fi
if [ "$MODE" = "LDFLAG" ]; then
LD_FLAGS="-X ${BASE_PACKAGE}.Revision=${GIT_REVISION} \
-X ${BASE_PACKAGE}.Branch=${GIT_BRANCH} \
-X ${BASE_PACKAGE}.ReleaseVersion=${RELEASE_VERSION} \
-X ${BASE_PACKAGE}.BuildDate=${BUILD_DATE} \
-X ${BASE_PACKAGE}.BuildTimeUnix=${BUILD_TS_UNIX}"
echo $LD_FLAGS
fi
if [ "$MODE" = "ECHO" ]; then
cat <<EOF
BASE_PACKAGE=${BASE_PACKAGE}
GIT_REVISION=${GIT_REVISION}
GIT_BRANCH=${GIT_BRANCH}
GIT_VERSION=${GIT_VERSION}
BUILD_DATE=${BUILD_DATE}
BUILD_TS_UNIX=${BUILD_TS_UNIX}
EOF
fi