Jenkins job for private Go projects

3,905 views
Skip to first unread message

Herbert Fischer

unread,
Mar 26, 2013, 7:03:45 PM3/26/13
to golang-nuts
Hi,

I'm trying to setup a Jenkins job for a private Go project that:

- Pool git to watch repo for changes (one job for each branch I want to watch: master and develop);
- Fetch changes;
- Go get dependencies, updated ones, mostly public dependencies, but in the future I may have private ones also;
- Build project;
- Archive and upload artefacts;

I was able to do it for some public projects by using this script in the jobs:

export GOPATH="$JENKINS_HOME/jobs/$JOB_NAME/builds/$BUILD_ID/golang"
export GOBIN="$GOPATH/bin"
mkdir -p $GOPATH
go get -v
go build -v


But this have two issues:

- If my job is watching and fetching another branch than the master, "go get" will fetch all deps, including the "master" source and put it somewhere into $GOPATH. In other words, I'll always be building the master branch of the project, instead the source Jenkins fetched.

- This does not work with private repos. I also want to avoid using git inside this script, to manually clone the wanted repository inside $GOPATH, since Jenkins already fetched the right version for the build in the jobs directory. 

 Do you guys have some working Jenkins build server for Go projects and would like to share how your jobs are setup? How do you manage the $GOPATH/src/... with Jenkins?

tks,

HGF

Kamil Kisiel

unread,
Mar 26, 2013, 7:18:59 PM3/26/13
to golan...@googlegroups.com
I use the following script (or similar) for Jenkins jobs, assuming the project checkout is in $PWD/src and artifacts are archived from $PWD/dist

#!/bin/bash
set -x
export GOPATH=$(pwd)/gopath
export IMPORTPATH=hg.example.com/projectname
rm -rf $GOPATH && mkdir -p $GOPATH/bin $GOPATH/pkg $GOPATH/src/$IMPORTPATH && mv src/* $GOPATH/src/$IMPORTPATH
go get $IMPORTPATH
go test $IMPORTPATH || exit 1
rm -rf dist && mkdir dist && cp -a $GOPATH/bin/* $GOPATH/src/$IMPORTPATH/{js,font,css,html,img} dist/

Basically it moves the checkout done by Jenkin to the right spot under $PWD/gopath/src and goes from there. The "go get" will not fetch the sources again since it's not using -u, but it will fetch all the dependencies.

Eli Janssen

unread,
Mar 26, 2013, 8:23:57 PM3/26/13
to Herbert Fischer, golang-nuts

On Mar 26, 2013, at 4:03 PM, Herbert Fischer <herbert...@gmail.com> wrote:

> Hi,
>
> I'm trying to setup a Jenkins job for a private Go project that:
>
> - Pool git to watch repo for changes (one job for each branch I want to watch: master and develop);
> - Fetch changes;
> - Go get dependencies, updated ones, mostly public dependencies, but in the future I may have private ones also;
> - Build project;
> - Archive and upload artefacts;
>
> I was able to do it for some public projects by using this script in the jobs:
>
> export GOPATH="$JENKINS_HOME/jobs/$JOB_NAME/builds/$BUILD_ID/golang"
> export GOBIN="$GOPATH/bin"
> mkdir -p $GOPATH
> go get -v
> go build -v
>
> But this have two issues:
>
> - If my job is watching and fetching another branch than the master, "go get" will fetch all deps, including the "master" source and put it somewhere into $GOPATH. In other words, I'll always be building the master branch of the project, instead the source Jenkins fetched.
>
> - This does not work with private repos.

Random information, but I have found that .netrc files are honored by git (possibly other scm's?), which go get calls out to.
I use a .netrc with github to fetch private repos during build time.

see: https://help.github.com/articles/creating-an-oauth-token-for-command-line-use
example .netrc: https://gist.github.com/cactus/3925422

Herbert Fischer

unread,
Mar 27, 2013, 10:18:36 AM3/27/13
to Kamil Kisiel, golan...@googlegroups.com
Thanks! Got your idea and looking a bit more on Jenkins I ended up solving by doing this:

Configure "Custom Workspace Directory": workspace/project/src/bitbucket.org/user/project

Jenkins script:

#!/bin/bash
set -x
export GOPATH="$JENKINS_HOME/workspace/project"
export GOBIN="$GOPATH/bin"
make clean
make

Makefile:

all: build package

build:
        go get -v
        go build -v

package: project
        mkdir -p package/bin package/data
        cp -Rv data/* package/data
        cp project package/bin
        cd package; tar cvjf ../project.tar.gz bin data

clean:
        # remove to go get fresh new ones in every build
        rm -rf ../../../github.com/gorilla/mux
        rm -rf ../../../code.google.com/p/go-charset
        rm -rf package
        rm -f project.tar.gz


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Herbert Fischer

unread,
Mar 27, 2013, 10:19:53 AM3/27/13
to Eli Janssen, golang-nuts
Good to know. I don't use GitHub for private projects, and I'll check if this works with Bitbucket. However, I must use the version Jenkins cloned. If I rely on "go get" I will lost control of what version I'm building.

Eric

unread,
Mar 29, 2013, 6:27:33 PM3/29/13
to golan...@googlegroups.com
Hi


How do you manage the $GOPATH/src/... with Jenkins? 


Enjoy.
Reply all
Reply to author
Forward
0 new messages