使用Docker在Jenkins上使用“vendor”目录构建Go app

时间:2016-08-17 11:54:37

标签: go jenkins docker jenkinsfile

我正在尝试使用Jenkinsfiledocker.image().inside 设置Jenkins管道来构建和部署我的第一个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

...为什么不选择供应商目录?

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

(编辑 - 有合适的文档heredir("/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

我的詹金斯文件:

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
    }
}

1 个答案:

答案 0 :(得分:1)

我设法通过将cd包含在同一sh声明中来实现目标:

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 ./...
      """
}