-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.sh
executable file
·94 lines (77 loc) · 2.06 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
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/bash -e
# Check if src directory is not empty
if ( find /src -maxdepth 0 -empty | read v );
then
echo "Error: Must mount Go source code into /src directory"
exit 990
fi
# Grab Go package name from canonical import path annotation (package main // import "github.com/fabriziopandini/hello")
pkgName="$(go list -e -f '{{.ImportComment}}' 2>/dev/null || true)"
if [ -z "$pkgName" ];
then
echo "Error: Must add canonical import path to root package"
exit 992
fi
# Grab the last segment from the package name
name=${pkgName##*/}
# Grab the image tag name from command line arguments or default it to package name if not set explicitly
tagName=$1
tagName=${tagName:-"$name":latest}
# Grab just first path listed in GOPATH
goPath="${GOPATH%%:*}"
# Construct Go package path
pkgPath="$goPath/src/$pkgName"
# Set-up src directory tree in GOPATH
mkdir -p "$(dirname "$pkgPath")"
# Link source dir into GOPATH
ln -sf /src "$pkgPath"
# Get dependencies
echo "Get package dependencies"
(
go get -t -d -v ./...
)
#
# Optional OUTPUT env var to use the "-o" go build switch
# forces build to write the resulting executable or object
# to the named output file
#
output=""
if [[ ! -z "${OUTPUT}" ]];
then
output="-o ${OUTPUT}"
fi
# Compile statically linked version of package
echo "Building package $pkgName"
(
CGO_ENABLED=${CGO_ENABLED:-0} \
go build \
-a \
${output} \
--installsuffix cgo \
--ldflags="${LDFLAGS:--s}" \
$pkgName
)
# Check if /var/run/docker.sock is mounted
if [[ -e "/var/run/docker.sock" ]];
then
#
# Optional DOCKERFILE env var to use the "-f" docker build switch
# forces docker to use a different dockerfile
#
dockerfile=""
if [[ ! -z "${DOCKERFILE}" ]];
then
dockerfile="-f ${DOCKERFILE}"
fi
# Check if ./Dockerfile is in src directory
if [[ ! -e "./${DOCKERFILE:-Dockerfile}" ]];
then
echo "Error: Must have ./Dockerfile into /src directory"
exit 990
fi
# Build the image from the Dockerfile in the package directory
echo "Building image $tagName"
(
docker build -t $tagName ${dockerfile} .
)
fi