gpt4 book ai didi

maven - 如何使用带有管道的 Docker 缓存本地 Maven 存储库?

转载 作者:行者123 更新时间:2023-12-04 14:53:55 24 4
gpt4 key购买 nike

我想在 Docker 容器中运行我的 Maven 构建。我不想在每次构建时上传所有依赖项,所以我尝试挂载主机的本地 Maven 存储库,如 Using Docker with Pipeline 所述。 :

Caching data for containers

[...]

Pipeline supports adding custom arguments which are passed to Docker, allowing users to specify custom Docker Volumes to mount, which can be used for caching data on the agent between Pipeline runs. The following example will cache ~/.m2 between Pipeline runs utilizing the maven container, thereby avoiding the need to re-download dependencies for subsequent runs of the Pipeline.

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


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

日志
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on jenkins-docker in /home/jenkins/workspace/Test/Docker Test@2
[Pipeline] {
[Pipeline] sh
+ docker inspect -f . maven:3-alpine
.
[Pipeline] withDockerContainer
jenkins-docker does not seem to be running inside a container
$ docker run -t -d -u 1000:1000 -v /home/jenkins/.m2:/root/.m2 -w "/home/jenkins/workspace/Test/Docker Test@2" -v "/home/jenkins/workspace/Test/Docker Test@2:/home/jenkins/workspace/Test/Docker Test@2:rw,z" -v "/home/jenkins/workspace/Test/Docker Test@2@tmp:/home/jenkins/workspace/Test/Docker Test@2@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

[...]

[DEBUG] Reading global settings from /usr/share/maven/conf/settings.xml
[DEBUG] Reading user settings from ?/.m2/settings.xml
[DEBUG] Reading global toolchains from /usr/share/maven/conf/toolchains.xml
[DEBUG] Reading user toolchains from ?/.m2/toolchains.xml
[DEBUG] Using local repository at /home/jenkins/workspace/Test/Docker Test@2/?/.m2/repository
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10.0 for /home/jenkins/workspace/Test/Docker Test@2/?/.m2/repository

问题

建好后 ~/m2目录为空,未添加文件/目录。所有文件都添加到 /home/jenkins/workspace/Test/Docker Test@2/?/.m2 下(Test 是文件夹的名称,Docker Test 是管道的名称)。

问题是这个目录只用于这个特定的管道而不用于其他管道,所以我无法与不同的管道/作业共享本地 Maven 存储库。

还有我的 settings.xml没有使用,因为它保存在 ~/m2下.

是否有使用 Docker 与不同管道共享本地 Maven 存储库和 Maven 设置的解决方案?

最佳答案

我找到了解决方法,请参阅 Local settings.xml not picked up by Jenkins agent :

The issue is related to the -u uid:gid that jenkins uses to run the container. As you may know the image you are running only has the user root created, so when jenkins pass its own uid and gid , there is no entry for the user and consequentially no $HOME declared for it.

If you only want to run the build independently of the user, you can use the follow as agent:

agent {
docker {
image 'maven:3-alpine'
args '-v $HOME/.m2:/root/.m2:z -u root'
reuseNode true
}
}

A few notes:

  1. if you notice the volume I am using with the flag z, as I am going to build with root, I need to tell docker that this volume will be shared among another containers, and then preventing access denied from my jenkins container (running with the user jenkins not root)
  2. I tell jenkins to reuseNode, so any other stage using the same image, will be executing on the same container (it is just to speed up the provisioning time)


日志
[DEBUG] Reading global settings from /usr/share/maven/conf/settings.xml
[DEBUG] Reading user settings from /root/.m2/settings.xml
[DEBUG] Reading global toolchains from /usr/share/maven/conf/toolchains.xml
[DEBUG] Reading user toolchains from /root/.m2/toolchains.xml
[DEBUG] Using local repository at /root/.m2/repository
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10.0 for /root/.m2/repository

不幸的是,本地存储库中的文件 /home/jenkins/.m2现在归用户 root 所有而不是用户 jenkins .这可能会导致其他问题。

关于maven - 如何使用带有管道的 Docker 缓存本地 Maven 存储库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55104543/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com