Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Go Jenkins CI Script - golang
# Go build script that runs cloc, cpd, lint, vet, test and coverage for Jenkins
# Outside tools include:
# gocov: go get
# gocov-xml: go get
# go2xunit: go get
# 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/...);
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
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/ .

This comment has been minimized.

Copy link

commented Jan 31, 2017

what will be in lint.txt and vet.txt and in which format ??.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.