Skip to content

Instantly share code, notes, and snippets.

@lsgrep
Forked from wavded/go-ci-build.sh
Created July 18, 2017 07:56
Show Gist options
  • Save lsgrep/6f54ffc44394ed5dde122ff102afc1ea to your computer and use it in GitHub Desktop.
Save lsgrep/6f54ffc44394ed5dde122ff102afc1ea to your computer and use it in GitHub Desktop.
Go Jenkins CI Script - golang
#!/bin/bash
# Go build script that runs cloc, cpd, lint, vet, test and coverage for Jenkins
#
# Outside tools include:
# gocov: go get github.com/axw/gocov
# gocov-xml: go get github.com/t-yuki/gocov-xml
# go2xunit: go get bitbucket.org/tebeka/go2xunit
# jscpd: npm i jscpd -g
# cloc: npm i cloc -g
set -x
# Set up environment
export PATH=$PATH:/var/lib/jenkins/go/bin
export PRJ=`git config --get remote.origin.url | sed 's/^https:\/\///' | sed 's/\.git$//'`
export GODOCPATH=/var/lib/jenkins/go/src
export GOPATH=`pwd`
# Clean directory
git clean -dfx
# Move project into GOPATH
mkdir -p src/$PRJ
ls -1 | grep -v ^src | xargs -I{} mv {} src/$PRJ/
# Copy project to GODOCPATH for documentation hosting
mkdir -p $GODOCPATH/$PRJ
rsync -azu --delete --exclude="vendor" --exclude="Godeps" src/$PRJ/* $GODOCPATH/$PRJ
# LOC reporting (SLOCCount plugin)
cloc --by-file --xml --out=cloc.xml --exclude-dir=vendor,Godeps .
# Copy-paste detection (DRY plugin)
jscpd -e {**/vendor/**,**/Godeps/**,**/*.min.*} -o cpd.xml
# Make distribution directory
mkdir dist
# Run tests (JUnit plugin)
rm -f test.out
echo "mode: set" > coverage.out
for pkg in $(go list $PRJ/...);
do
if [[ $pkg != *"vendor"* ]]; then
echo "testing... $pkg"
go test -v -coverprofile=tmp.out $pkg >> test.out
if [ -f tmp.out ]; then
cat tmp.out | grep -v "mode: set" >> coverage.out
fi
fi
done
rm -f ./tmp.out
cat test.out | go2xunit -output tests.xml
# Generate coverage reports (Cobertura plugin)
gocov convert coverage.out | gocov-xml > cobertura-coverage.xml
# Run lint tools (Compiler warning plugin)
golint $PRJ > lint.txt
# Run vet tools (Compiler warning plugin)
go vet $PRJ > vet.txt
# Build application
go build -o dist/app $PRJ
# Create distro
rsync -az $GOPATH/src/$PRJ/* --exclude="vendor" --exclude="Godeps" --exclude="**/*.go" --exclude=".git*" dist/
# Archive artifact
tar -zcf archive.tar.gz -C dist/ .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment