简体   繁体   English

Jenkins为Go Projects构建设置

[英]Jenkins build setup for Go Projects

We are planning to setup Jenkin Build process for Go Projects. 我们计划为Go Projects设置Jenkin Build流程。 I setup Custom Workspace in jenkins and installing go1.6 from "Global Tool Configuration". 我在jenkins中设置了Custom Workspace并从“Global Tool Configuration”安装了go1.6。

I am getting error message while executing go build Below is the GOPATH and GOROOT 执行go build时收到错误信息下面是GOPATH和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. 除了让Go插件处理你的GOROOT之外,GOPATH在获取依赖关系方面也有一些细微差别。 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. 我们将* .go源文件放在我们的Git存储库的根目录中,因此可以通过Dev桌面上的go命令轻松管理它们。 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. 所以,我正在使用构建脚本来欺骗Go认为通过符号链接在/ src / main下有一个名为main的包,这样我就可以使用相同的脚本构建我所有的go包并拉出依赖项。 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

I use jenkin too but write build.sh by myself. 我也使用jenkin但是自己编写build.sh。 To give you a reference: 给你一个参考:

build.sh 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"

GOROOT should be the root of your go distribution. GOROOT应该是你的发行版的根源。 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. 如果仍然无法找到字节,请将GOROOT设置为您安装的目录。 Basically, this is the parent dir to wherever bin/go exists. 基本上,这是bin / go存在的任何地方的父目录。

because jenkins works very well with maven. 因为詹金斯与maven的合作非常好。 you can build golang projects under jenkins with mvn-golang plugin and also make reports compatible with jenkins 你可以使用mvn-golang插件jenkins下构建golang项目,并使报告与jenkins兼容

I would recommend using Go Plugin for managing golang installations. 我建议使用Go Plugin来管理golang安装。

Once you got it installed, it's important to understand golang environmental variables and their use, the mains are: 一旦安装完毕,了解golang环境变量及其使用非常重要,主要包括:

$GOROOT specified the location where go is installed $GOROOT指定了安装go的位置

$GOPATH determines where your code is located and go get installs packages, by default it's ${HOME}/go $GOPATH确定代码所在的位置并go get安装包,默认情况下为${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 通过上面的设置,我们将GOROOT设置为我们的自定义go安装,并将$PATH附加到我们使用go get和standard go下载的二进制文件的路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM