在Jenkins上使用Docker构建带有”vendor”目录的Go应用程序。

huangapple go评论94阅读模式
英文:

Building Go app with "vendor" directory on Jenkins with Docker

问题

我正在尝试设置一个Jenkins Pipeline,使用一个Jenkinsfiledocker.image().inside来构建和部署我的第一个Go项目。我无法弄清楚如何让Go在vendor/目录中找到依赖项。

当我运行构建时,会出现一堆错误:

+ goapp test ./...
src/dao/demo_dao.go:8:2: cannot find package "github.com/dgrijalva/jwt-go" in any of:
	/usr/lib/go_appengine/goroot/src/github.com/dgrijalva/jwt-go (from $GOROOT)
	/usr/lib/go_appengine/gopath/src/github.com/dgrijalva/jwt-go (from $GOPATH)
	/workspace/src/github.com/dgrijalva/jwt-go

为什么它没有找到vendor目录呢?

当我添加一些日志记录时,似乎在运行sh "cd /workspace/src/bitbucket.org/nalbion/go-demo"之后,下一个sh命令仍然在原始的${WORKSPACE}目录中。我很喜欢Jenkins文件的想法,但是我找不到任何合适的文档。

(编辑 - 这里有一份不错的文档here,但是dir("/workspace/src/bitbucket.org/nalbion/go-demo") {}docker.image().inside中似乎不起作用)

我的Docker文件如下:

FROM golang:1.6.2
# Google's App Engine Go SDK
RUN wget https://storage.googleapis.com/appengine-sdks/featured/go_appengine_sdk_linux_amd64-1.9.40.zip -q -O go_appengine_sdk.zip && \
    unzip -q go_appengine_sdk.zip -d /usr/lib/ && \
    rm go_appengine_sdk.zip
ENV PATH /usr/lib/go_appengine:/go/bin:/usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ENV GOPATH /usr/lib/go_appengine/gopath
# Add Jenkins user
RUN groupadd -g 132 jenkins && useradd -d "/var/jenkins_home" -u 122 -g 132 -m -s /bin/bash jenkins

我的Jenkinsfile如下:

node('docker') {
    currentBuild.result = "SUCCESS"

    try {
        stage 'Checkout'
        checkout scm

        stage 'Build and Test'
        env.WORKSPACE = pwd()
        docker.image('nalbion/go-web-build:latest').inside(
                "-v ${env.WORKSPACE}:/workspace/src/bitbucket.org/nalbion/go-demo " +
                "-e GOPATH=/usr/lib/go_appengine/gopath:/workspace") {

            // Debugging
            sh 'echo GOPATH: $GOPATH'
            sh "ls -al /workspace/src/bitbucket.org/nalbion/go-demo"
            sh "cd /workspace/src/bitbucket.org/nalbion/go-demo"
            sh "pwd"

            sh "go vet ./src/..."
            sh "goapp test ./..."
        }

        stage 'Deploy to DEV'
        docker.image('nalbion/go-web-build').inside {
            sh "goapp deploy --application go-demo --version v${v} app.yaml"
        }

        timeout(time:5, unit:'DAYS') {
            input message:'Approve deployment?', submitter: 'qa'
        }

        stage 'Deploy to PROD'
        docker.image('nalbion/go-web-build').inside {
            sh "goapp deploy --application go-demo --version v${v} app.yaml"
        }
    } catch (err) {
        currentBuild.result = "FAILURE"
        // send notifications
        throw err
    }
}
英文:

I'm trying to set up a Jenkins Pipeline to build and deploy my first Go project using a <code>Jenkinsfile</code> and <code>docker.image().inside </code>. I can't figure out how to get go to pick up the dependencies in the <code>vendor/</code> directory.

When I run the build, I get a bunch of errors:
<pre>

  • goapp test ./...
    src/dao/demo_dao.go:8:2: cannot find package "github.com/dgrijalva/jwt-go" in any of:
    /usr/lib/go_appengine/goroot/src/github.com/dgrijalva/jwt-go (from $GOROOT)
    /usr/lib/go_appengine/gopath/src/github.com/dgrijalva/jwt-go (from $GOPATH)
    /workspace/src/github.com/dgrijalva/jwt-go
    </pre>

...why isn't it picking up the Vendor directory?

When I throw in some logging, it seems that after running <code>sh "cd /workspace/src/bitbucket.org/nalbion/go-demo"</code> the next <code>sh</code> command is still in the original <code>${WORKSPACE}</code> directory. I really like the idea of the Jenkins file, but I can't find any decent documentation for it.

(Edit - there is decent documentation here but <code>dir("/workspace/src/bitbucket.org/nalbion/go-demo") {}</code> doesn't seem to work within <code>docker.image().inside)</code>

My Docker file resembles:
<pre>
FROM golang:1.6.2

Google's App Engine Go SDK

RUN wget https://storage.googleapis.com/appengine-sdks/featured/go_appengine_sdk_linux_amd64-1.9.40.zip -q -O go_appengine_sdk.zip &&
unzip -q go_appengine_sdk.zip -d /usr/lib/ &&
rm go_appengine_sdk.zip
ENV PATH /usr/lib/go_appengine:/go/bin:/usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ENV GOPATH /usr/lib/go_appengine/gopath

Add Jenkins user

RUN groupadd -g 132 jenkins && useradd -d "/var/jenkins_home" -u 122 -g 132 -m -s /bin/bash jenkins
</pre>

And my Jenkinsfile:
<pre>
node('docker') {
currentBuild.result = "SUCCESS"

try {
    stage &#39;Checkout&#39;
    checkout scm

    stage &#39;Build and Test&#39;
    env.WORKSPACE = pwd()
    docker.image(&#39;nalbion/go-web-build:latest&#39;).inside(
            &quot;-v ${env.WORKSPACE}:/workspace/src/bitbucket.org/nalbion/go-demo &quot; +
            &quot;-e GOPATH=/usr/lib/go_appengine/gopath:/workspace&quot;) {

        // Debugging
        sh &#39;echo GOPATH: $GOPATH&#39;
        sh &quot;ls -al /workspace/src/bitbucket.org/nalbion/go-demo&quot;
        sh &quot;cd /workspace/src/bitbucket.org/nalbion/go-demo&quot;
        sh &quot;pwd&quot;

        sh &quot;go vet ./src/...&quot;
        sh &quot;goapp test ./...&quot;
    }

    stage &#39;Deploy to DEV&#39;
    docker.image(&#39;nalbion/go-web-build&#39;).inside {
        sh &quot;goapp deploy --application go-demo --version v${v} app.yaml&quot;
    }

    timeout(time:5, unit:&#39;DAYS&#39;) {
        input message:&#39;Approve deployment?&#39;, submitter: &#39;qa&#39;
    }

    stage &#39;Deploy to PROD&#39;
    docker.image(&#39;nalbion/go-web-build&#39;).inside {
        sh &quot;goapp deploy --application go-demo --version v${v} app.yaml&quot;
    }
} catch (err) {
    currentBuild.result = &quot;FAILURE&quot;
    // send notifications
    throw err
}

}
</pre>

答案1

得分: 2

我通过在同一个sh语句中包含cd命令来使其正常工作:

docker.image('nalbion/go-web-build:latest')
      .inside("-v ${env.WORKSPACE}:/workspace/src/bitbucket.org/nalbion/go-demo " +
              "-e GOPATH=/usr/lib/go_appengine/gopath:/workspace") {
   sh """
       cd /workspace/src/bitbucket.org/nalbion/go-demo
       go vet ./src/...
       goapp test ./...
      """
}
英文:

I managed to get it working by including the <code>cd</code> in the same <code>sh</code> statement:

<pre>
docker.image('nalbion/go-web-build:latest')
.inside("-v ${env.WORKSPACE}:/workspace/src/bitbucket.org/nalbion/go-demo " +
"-e GOPATH=/usr/lib/go_appengine/gopath:/workspace") {
sh """
cd /workspace/src/bitbucket.org/nalbion/go-demo
go vet ./src/...
goapp test ./...
"""
}
</pre>

huangapple
  • 本文由 发表于 2016年8月17日 19:54:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/38995933.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定