blob: d35bec6783b2cbf7009183e4073a47e68bc38f6c [file] [log] [blame]
Sergio Slobodrianc2d86fb2017-07-18 20:35:46 -04001#!/bin/bash -e
Zack Williams41513bf2018-07-07 20:08:35 -07002# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
Sergio Slobodrianc2d86fb2017-07-18 20:35:46 -040015
16source /build_environment.sh
17
18mkdir buildreport
19novendor_dirs=$(go list ./... | grep -v '/vendor/')
20
21echo "Using nonvendor dirs:"
22echo "$novendor_dirs"
23echo "--------------------------------------"
24
25echo "* Run tests with race detector"
26go test -race ${novendor_dirs}
27
28echo "--------------------------------------"
29echo "* Run vet + golint"
30for f in go_vet.txt golint.txt
31do
32 touch buildreport/${f}
33done
34
35for d in $novendor_dirs
36do
37 go vet ${d} 2>> buildreport/go_vet.txt || true
38 golint ${d} >> buildreport/golint.txt || true
39done
40
41echo "--------------------------------------"
42echo "* Run errcheck"
43errcheck ${novendor_dirs} > buildreport/errcheck.txt || true
44
45
46# Run test coverage on each subdirectories and merge the coverage profile.
47echo "--------------------------------------"
48echo "* Building coverage report"
49
50echo "mode: count" > buildreport/profile.cov
51coverDirs=$novendor_dirs
52for dir in $coverDirs
53do
54 path="$GOPATH/src/$dir"
55 if ls $path/*.go &> /dev/null; then
56 go test -covermode=count -coverprofile=$path/profile.tmp $dir
57 if [ -f $path/profile.tmp ]
58 then
59 cat $path/profile.tmp | tail -n +2 >> buildreport/profile.cov
60 rm $path/profile.tmp
61 fi
62 fi
63done
64
65go tool cover -html buildreport/profile.cov -o buildreport/cover.html
66
67echo "--------------------------------------"
68main_packages=$(go list ./... |grep -v vendor |grep cmd || true)
69main_packages+=( ${pkgName} )
70for pkg in ${main_packages[@]}
71do
72 # Grab the last segment from the package name
73 name=${pkg##*/}
David K. Bainbridge10a7a7e2018-01-29 09:54:40 -080074 echo "* Building Go binary: $pkg to goPath/src/$pkg/$name"
Sergio Slobodrianc2d86fb2017-07-18 20:35:46 -040075
76 flags=(-a -installsuffix cgo)
77 ldflags=('-s -X main.version='$BUILD_VERSION)
78
79 # Compile statically linked version of package
80 # see https://golang.org/cmd/link/ for all ldflags
81 CGO_ENABLED=${CGO_ENABLED:-0} go build \
82 "${flags[@]}" \
83 -ldflags "${ldflags[@]}" \
84 -o "$goPath/src/$pkg/$name" \
85 "$pkg"
86
87 if [[ $COMPRESS_BINARY == "true" ]];
88 then
89 goupx $name
90 fi
91
David K. Bainbridge10a7a7e2018-01-29 09:54:40 -080092 if [ "$OUTPUT X" != " X" ]; then
93 echo "* Copy Go Binary to \"$OUTPUT/$name\""
94 mkdir -p $OUTPUT
95 cp "$goPath/src/$pkg/$name" "$OUTPUT/$name"
96 fi
97
Sergio Slobodrianc2d86fb2017-07-18 20:35:46 -040098 if [ -e "/var/run/docker.sock" ] && [ -e "$goPath/src/$pkg/Dockerfile" ];
99 then
100
101 # Default TAG_NAME to package name if not set explicitly
102 tagName=${tagName:-"$name":latest}
103 echo "--------------------------------------"
104 echo "* Building Docker image: $tagName"
105
106 # Build the image from the Dockerfile in the package directory
107 docker build --pull -t $tagName .
108 fi
David K. Bainbridge10a7a7e2018-01-29 09:54:40 -0800109done