Skip to content

Instantly share code, notes, and snippets.

@wavded
Last active March 1, 2023 23:30
Show Gist options
  • Save wavded/5e6b0d5016c2a3c05237 to your computer and use it in GitHub Desktop.
Save wavded/5e6b0d5016c2a3c05237 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/ .
@srbhkskr
Copy link

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