Skip to content

Instantly share code, notes, and snippets.

@eodgooch
Forked from wavded/go-ci-build.sh
Last active October 24, 2016 20:50
Show Gist options
  • Save eodgooch/b604dadc70bf90b7beb2f7bbf38ca7e1 to your computer and use it in GitHub Desktop.
Save eodgooch/b604dadc70bf90b7beb2f7bbf38ca7e1 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 GO=/var/lib/jenkins/tools/org.jenkinsci.plugins.golang.GolangInstallation/go-1.7.1
export PATH=$PATH:$GO/bin
export PRJ=`git config --get remote.origin.url | sed 's|^https:\/\/||' | sed 's|\.git$||' | sed 's|^git@||' | sed 's|:|/|'`
export GODOCPATH=$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