Jenkins为Go Projects构建设置

We are planning to setup Jenkin Build process for Go Projects. I setup Custom Workspace in jenkins and installing go1.6 from "Global Tool Configuration".

I am getting error message while executing go build Below is the GOPATH and GOROOT

GOPATH /var/lib/jenkins/workspace/project/go
GOROOT  /var/lib/jenkins/workspace

ain.go:20:2: cannot find package "bytes" in any of:
    /var/lib/jenkins/workspace/src/pkg/bytes (from $GOROOT)
    /var/lib/jenkins/workspace/project/go/src/bytes (from $GOPATH)

What I am missing here?.. Thanks for your help..

In addition to letting the Go plugin handle your GOROOT, there are some nuances to the GOPATH as well when it comes to getting dependencies. We are putting our *.go source files in the root of our Git repositories, so they are easily managed via go commands on the Dev desktops. So, I am using a build script to trick Go into thinking there is a package called main under /src/main via a symlink so that I can use the same script to build all of my go packages and pull the dependencies. Here is my build script:

#!/usr/bin/bash export GOPATH=$WORKSPACE mkdir -p $GOPATH/src ln -f -s $WORKSPACE $GOPATH/src/main go get main CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main main

GOROOT should be the root of your go distribution. Normally you don't need to set it.

Clear that and try again. If it still can't find bytes, set GOROOT to to directory where you installed go. Basically, this is the parent dir to wherever bin/go exists.

I use jenkin too but write build.sh by myself. To give you a reference:

build.sh

#!/usr/bin/bash

WORKROOT=$(pwd)
cd ${WORKROOT}

# unzip go environment
go_env="go1.6.2.linux-amd64.tar.gz"
wget -c http://path/to/go/go1.6.2.linux-amd64.tar.gz
tar -zxf $go_env
if [ $? -ne 0 ];
then
    echo "fail in extract go"
    exit 1
fi
echo "OK for extract go"
rm -rf $go_env

# prepare PATH, GOROOT and GOPATH
export PATH=$(pwd)/go/bin:$PATH
export GOROOT=$(pwd)/go
export GOPATH=$(pwd)

# build
cd path/to/your/project
go build
if [ $? -ne 0 ];
then
    echo "fail to go build"
    exit 1
fi
echo "OK for go build"

because jenkins works very well with maven. you can build golang projects under jenkins with mvn-golang plugin and also make reports compatible with jenkins

I would recommend using Go Plugin for managing golang installations.

Once you got it installed, it's important to understand golang environmental variables and their use, the mains are:

$GOROOT specified the location where go is installed

$GOPATH determines where your code is located and go get installs packages, by default it's ${HOME}/go

withEnv(["GOROOT=${root}", "PATH+GO=${root}/bin:${HOME}/go/bin"]) {
    sh "go get gopkg.in/alecthomas/gometalinter.v1"
    sh "gometalinter.v1 --help"
}

With the setup above, we set GOROOT to our custom go installation and append $PATH with paths to binaries that we downloaded using go get and standard go