blob: 0137347049e543609aa59857d5f3ff64e9a4fcec [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##*/}
61 echo "* Building Go binary: $pkg"
62
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
79 if [ -e "/var/run/docker.sock" ] && [ -e "$goPath/src/$pkg/Dockerfile" ];
80 then
81
82 # Default TAG_NAME to package name if not set explicitly
83 tagName=${tagName:-"$name":latest}
84 echo "--------------------------------------"
85 echo "* Building Docker image: $tagName"
86
87 # Build the image from the Dockerfile in the package directory
88 docker build --pull -t $tagName .
89 fi
90done