使用maven与jenkins的docker镜像 - 无法创建本地存储库

问题描述 投票:0回答:2

我是Jenkins和Docker的新手,我正试图获得maven的docker图像来构建我的项目。

我的詹金斯脚本是相当简单的atm:

pipeline {
  agent {
    docker {
        image 'maven:3-alpine'
        args '-v /root/.m2:/root/.m2'
    }
  }
  stages {
    stage('Build') {
        steps {
            sh 'mvn -B -DskipTests clean install'
        }
    }
  }
}

这会产生错误“无法在/var/empty/.m2/repository创建本地存储库”。如果我在命令行中指定一个本地repo,我会为我指定的任何路径得到相同的错误。

Jenkins安装并运行在tomcat服务器上,而不是在docker中,Jenkins每次运行都会提到它,并说“Jenkins似乎没有在容器内运行”。

Jenkins控制台输出如下所示:

[Pipeline] withDockerContainer
Jenkins does not seem to be running inside a container
$ docker run -t -d -u 123:128 -v /root/.m2:/root/.m2 -w /usr/share/tomcat7/.jenkins/workspace/MyProject -v /usr/share/tomcat7/.jenkins/workspace/MyProject:/usr/share/tomcat7/.jenkins/workspace/MyProject:rw,z -v /usr/share/tomcat7/.jenkins/workspace/MyProject@tmp:/usr/share/tomcat7/.jenkins/workspace/MyProject@tmp:rw,z -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** maven:3-alpine cat
$ docker top 70c607bda0e397ed8ff79a183f33b2953ed94d82e4f36eb26dea87fa8c67adf3 -eo pid,comm
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] sh
[OMRS] Running shell script
+ mvn -B -DskipTests clean install
[ERROR] Could not create local repository at /var/empty/.m2/repository -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/LocalRepositoryNotAccessibleException

任何帮助将不胜感激。谢谢。

maven docker jenkins
2个回答
2
投票

而不是将root / .m2文件夹挂载到容器,您可能只需要以root用户身份运行:

pipeline {


agent {
    docker {
        image 'maven:3-alpine'
        args '-u root'
    }
  }
  stages {
    stage('Build') {
        steps {
            sh 'mvn -B -DskipTests clean install'
        }
    }
  }
}

0
投票

要在jenkins管道中以非root用户身份使用当前maven图像,请尝试以下docker volume mapping和maven环境配置:

args '-v $HOME/.m2:/var/maven/.m2:z -e MAVEN_CONFIG=/var/maven/.m2 -e MAVEN_OPTS="-Duser.home=/var/maven"'

https://github.com/carlossg/docker-maven#running-as-non-root

© www.soinside.com 2019 - 2024. All rights reserved.