Sergio Slobodrian | c2d86fb | 2017-07-18 20:35:46 -0400 | [diff] [blame] | 1 | #!/bin/bash -e |
| 2 | |
| 3 | source /build_environment.sh |
| 4 | |
| 5 | mkdir buildreport |
| 6 | novendor_dirs=$(go list ./... | grep -v '/vendor/') |
| 7 | |
| 8 | echo "Using nonvendor dirs:" |
| 9 | echo "$novendor_dirs" |
| 10 | echo "--------------------------------------" |
| 11 | |
| 12 | echo "* Run tests with race detector" |
| 13 | go test -race ${novendor_dirs} |
| 14 | |
| 15 | echo "--------------------------------------" |
| 16 | echo "* Run vet + golint" |
| 17 | for f in go_vet.txt golint.txt |
| 18 | do |
| 19 | touch buildreport/${f} |
| 20 | done |
| 21 | |
| 22 | for d in $novendor_dirs |
| 23 | do |
| 24 | go vet ${d} 2>> buildreport/go_vet.txt || true |
| 25 | golint ${d} >> buildreport/golint.txt || true |
| 26 | done |
| 27 | |
| 28 | echo "--------------------------------------" |
| 29 | echo "* Run errcheck" |
| 30 | errcheck ${novendor_dirs} > buildreport/errcheck.txt || true |
| 31 | |
| 32 | |
| 33 | # Run test coverage on each subdirectories and merge the coverage profile. |
| 34 | echo "--------------------------------------" |
| 35 | echo "* Building coverage report" |
| 36 | |
| 37 | echo "mode: count" > buildreport/profile.cov |
| 38 | coverDirs=$novendor_dirs |
| 39 | for dir in $coverDirs |
| 40 | do |
| 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 |
| 50 | done |
| 51 | |
| 52 | go tool cover -html buildreport/profile.cov -o buildreport/cover.html |
| 53 | |
| 54 | echo "--------------------------------------" |
| 55 | main_packages=$(go list ./... |grep -v vendor |grep cmd || true) |
| 56 | main_packages+=( ${pkgName} ) |
| 57 | for pkg in ${main_packages[@]} |
| 58 | do |
| 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 |
| 90 | done |