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

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

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

问题

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

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

  1. + goapp test ./...
  2. src/dao/demo_dao.go:8:2: cannot find package "github.com/dgrijalva/jwt-go" in any of:
  3. /usr/lib/go_appengine/goroot/src/github.com/dgrijalva/jwt-go (from $GOROOT)
  4. /usr/lib/go_appengine/gopath/src/github.com/dgrijalva/jwt-go (from $GOPATH)
  5. /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文件如下:

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

我的Jenkinsfile如下:

  1. node('docker') {
  2. currentBuild.result = "SUCCESS"
  3. try {
  4. stage 'Checkout'
  5. checkout scm
  6. stage 'Build and Test'
  7. env.WORKSPACE = pwd()
  8. docker.image('nalbion/go-web-build:latest').inside(
  9. "-v ${env.WORKSPACE}:/workspace/src/bitbucket.org/nalbion/go-demo " +
  10. "-e GOPATH=/usr/lib/go_appengine/gopath:/workspace") {
  11. // Debugging
  12. sh 'echo GOPATH: $GOPATH'
  13. sh "ls -al /workspace/src/bitbucket.org/nalbion/go-demo"
  14. sh "cd /workspace/src/bitbucket.org/nalbion/go-demo"
  15. sh "pwd"
  16. sh "go vet ./src/..."
  17. sh "goapp test ./..."
  18. }
  19. stage 'Deploy to DEV'
  20. docker.image('nalbion/go-web-build').inside {
  21. sh "goapp deploy --application go-demo --version v${v} app.yaml"
  22. }
  23. timeout(time:5, unit:'DAYS') {
  24. input message:'Approve deployment?', submitter: 'qa'
  25. }
  26. stage 'Deploy to PROD'
  27. docker.image('nalbion/go-web-build').inside {
  28. sh "goapp deploy --application go-demo --version v${v} app.yaml"
  29. }
  30. } catch (err) {
  31. currentBuild.result = "FAILURE"
  32. // send notifications
  33. throw err
  34. }
  35. }
英文:

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"

  1. try {
  2. stage &#39;Checkout&#39;
  3. checkout scm
  4. stage &#39;Build and Test&#39;
  5. env.WORKSPACE = pwd()
  6. docker.image(&#39;nalbion/go-web-build:latest&#39;).inside(
  7. &quot;-v ${env.WORKSPACE}:/workspace/src/bitbucket.org/nalbion/go-demo &quot; +
  8. &quot;-e GOPATH=/usr/lib/go_appengine/gopath:/workspace&quot;) {
  9. // Debugging
  10. sh &#39;echo GOPATH: $GOPATH&#39;
  11. sh &quot;ls -al /workspace/src/bitbucket.org/nalbion/go-demo&quot;
  12. sh &quot;cd /workspace/src/bitbucket.org/nalbion/go-demo&quot;
  13. sh &quot;pwd&quot;
  14. sh &quot;go vet ./src/...&quot;
  15. sh &quot;goapp test ./...&quot;
  16. }
  17. stage &#39;Deploy to DEV&#39;
  18. docker.image(&#39;nalbion/go-web-build&#39;).inside {
  19. sh &quot;goapp deploy --application go-demo --version v${v} app.yaml&quot;
  20. }
  21. timeout(time:5, unit:&#39;DAYS&#39;) {
  22. input message:&#39;Approve deployment?&#39;, submitter: &#39;qa&#39;
  23. }
  24. stage &#39;Deploy to PROD&#39;
  25. docker.image(&#39;nalbion/go-web-build&#39;).inside {
  26. sh &quot;goapp deploy --application go-demo --version v${v} app.yaml&quot;
  27. }
  28. } catch (err) {
  29. currentBuild.result = &quot;FAILURE&quot;
  30. // send notifications
  31. throw err
  32. }

}
</pre>

答案1

得分: 2

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

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

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:

确定