使用 Docker 在 Jenkins 上使用 "vendor" 目录构建 Go 应用程序

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

我正在尝试设置 Jenkins 管道以使用 Jenkinsfiledocker.image().inside 构建和部署我的第一个 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 文件的想法,但我找不到任何合适的文档。

(编辑 - 有不错的文档 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

还有我的 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
    }
}

我设法通过在同一个 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 ./...
      """
}