Sergio Slobodrian | c2d86fb | 2017-07-18 20:35:46 -0400 | [diff] [blame] | 1 | #!/bin/bash -e |
Zack Williams | 41513bf | 2018-07-07 20:08:35 -0700 | [diff] [blame] | 2 | # 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 Slobodrian | c2d86fb | 2017-07-18 20:35:46 -0400 | [diff] [blame] | 15 | |
| 16 | source /build_environment.sh |
| 17 | |
| 18 | mkdir buildreport |
| 19 | novendor_dirs=$(go list ./... | grep -v '/vendor/') |
| 20 | |
| 21 | echo "Using nonvendor dirs:" |
| 22 | echo "$novendor_dirs" |
| 23 | echo "--------------------------------------" |
| 24 | |
| 25 | echo "* Run tests with race detector" |
| 26 | go test -race ${novendor_dirs} |
| 27 | |
| 28 | echo "--------------------------------------" |
| 29 | echo "* Run vet + golint" |
| 30 | for f in go_vet.txt golint.txt |
| 31 | do |
| 32 | touch buildreport/${f} |
| 33 | done |
| 34 | |
| 35 | for d in $novendor_dirs |
| 36 | do |
| 37 | go vet ${d} 2>> buildreport/go_vet.txt || true |
| 38 | golint ${d} >> buildreport/golint.txt || true |
| 39 | done |
| 40 | |
| 41 | echo "--------------------------------------" |
| 42 | echo "* Run errcheck" |
| 43 | errcheck ${novendor_dirs} > buildreport/errcheck.txt || true |
| 44 | |
| 45 | |
| 46 | # Run test coverage on each subdirectories and merge the coverage profile. |
| 47 | echo "--------------------------------------" |
| 48 | echo "* Building coverage report" |
| 49 | |
| 50 | echo "mode: count" > buildreport/profile.cov |
| 51 | coverDirs=$novendor_dirs |
| 52 | for dir in $coverDirs |
| 53 | do |
| 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 |
| 63 | done |
| 64 | |
| 65 | go tool cover -html buildreport/profile.cov -o buildreport/cover.html |
| 66 | |
| 67 | echo "--------------------------------------" |
| 68 | main_packages=$(go list ./... |grep -v vendor |grep cmd || true) |
| 69 | main_packages+=( ${pkgName} ) |
| 70 | for pkg in ${main_packages[@]} |
| 71 | do |
| 72 | # Grab the last segment from the package name |
| 73 | name=${pkg##*/} |
David K. Bainbridge | 10a7a7e | 2018-01-29 09:54:40 -0800 | [diff] [blame] | 74 | echo "* Building Go binary: $pkg to goPath/src/$pkg/$name" |
Sergio Slobodrian | c2d86fb | 2017-07-18 20:35:46 -0400 | [diff] [blame] | 75 | |
| 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. Bainbridge | 10a7a7e | 2018-01-29 09:54:40 -0800 | [diff] [blame] | 92 | 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 Slobodrian | c2d86fb | 2017-07-18 20:35:46 -0400 | [diff] [blame] | 98 | 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. Bainbridge | 10a7a7e | 2018-01-29 09:54:40 -0800 | [diff] [blame] | 109 | done |