blob: cdca3b5268a885ded823a1867d574292fb2edccf [file] [log] [blame]
Sergio Slobodrianc2d86fb2017-07-18 20:35:46 -04001#!/bin/bash -e
2
3source /build_environment.sh
4
5mkdir buildreport
6novendor_dirs=$(go list ./... | grep -v '/vendor/')
7
8echo "Using nonvendor dirs:"
9echo "$novendor_dirs"
10echo "--------------------------------------"
11
12echo "* Run tests with race detector"
13go test -race ${novendor_dirs}
14
15echo "--------------------------------------"
16echo "* Run vet + golint"
17for f in go_vet.txt golint.txt
18do
19 touch buildreport/${f}
20done
21
22for d in $novendor_dirs
23do
24 go vet ${d} 2>> buildreport/go_vet.txt || true
25 golint ${d} >> buildreport/golint.txt || true
26done
27
28echo "--------------------------------------"
29echo "* Run errcheck"
30errcheck ${novendor_dirs} > buildreport/errcheck.txt || true
31
32
33# Run test coverage on each subdirectories and merge the coverage profile.
34echo "--------------------------------------"
35echo "* Building coverage report"
36
37echo "mode: count" > buildreport/profile.cov
38coverDirs=$novendor_dirs
39for dir in $coverDirs
40do
41 path="$GOPATH/src/$dir"
42 if ls $path/*.go &> /dev/null; then
43 go test -covermode=count -coverprofile=$path/profile.tmp $dir
44 if [ -f $path/profile.tmp ]
45 then
46 cat $path/profile.tmp | tail -n +2 >> buildreport/profile.cov
47 rm $path/profile.tmp
48 fi
49 fi
50done
51
52go tool cover -html buildreport/profile.cov -o buildreport/cover.html
53
54echo "--------------------------------------"
55main_packages=$(go list ./... |grep -v vendor |grep cmd || true)
56main_packages+=( ${pkgName} )
57for pkg in ${main_packages[@]}
58do
59 # Grab the last segment from the package name
60 name=${pkg##*/}
David K. Bainbridgeda617722018-01-29 09:54:40 -080061 echo "* Building Go binary: $pkg to goPath/src/$pkg/$name"
Sergio Slobodrianc2d86fb2017-07-18 20:35:46 -040062
63 flags=(-a -installsuffix cgo)
64 ldflags=('-s -X main.version='$BUILD_VERSION)
65
66 # Compile statically linked version of package
67 # see https://golang.org/cmd/link/ for all ldflags
68 CGO_ENABLED=${CGO_ENABLED:-0} go build \
69 "${flags[@]}" \
70 -ldflags "${ldflags[@]}" \
71 -o "$goPath/src/$pkg/$name" \
72 "$pkg"
73
74 if [[ $COMPRESS_BINARY == "true" ]];
75 then
76 goupx $name
77 fi
78
David K. Bainbridgeda617722018-01-29 09:54:40 -080079 if [ "$OUTPUT X" != " X" ]; then
80 echo "* Copy Go Binary to \"$OUTPUT/$name\""
81 mkdir -p $OUTPUT
82 cp "$goPath/src/$pkg/$name" "$OUTPUT/$name"
83 fi
84
Sergio Slobodrianc2d86fb2017-07-18 20:35:46 -040085 if [ -e "/var/run/docker.sock" ] && [ -e "$goPath/src/$pkg/Dockerfile" ];
86 then
87
88 # Default TAG_NAME to package name if not set explicitly
89 tagName=${tagName:-"$name":latest}
90 echo "--------------------------------------"
91 echo "* Building Docker image: $tagName"
92
93 # Build the image from the Dockerfile in the package directory
94 docker build --pull -t $tagName .
95 fi
David K. Bainbridgeda617722018-01-29 09:54:40 -080096done