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##*/} |
David K. Bainbridge | da61772 | 2018-01-29 09:54:40 -0800 | [diff] [blame^] | 61 | echo "* Building Go binary: $pkg to goPath/src/$pkg/$name" |
Sergio Slobodrian | c2d86fb | 2017-07-18 20:35:46 -0400 | [diff] [blame] | 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 | |
David K. Bainbridge | da61772 | 2018-01-29 09:54:40 -0800 | [diff] [blame^] | 79 | 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 Slobodrian | c2d86fb | 2017-07-18 20:35:46 -0400 | [diff] [blame] | 85 | 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. Bainbridge | da61772 | 2018-01-29 09:54:40 -0800 | [diff] [blame^] | 96 | done |