如何在流水线中使用Docker容器进行编译构建

网友投稿 287 2022-10-24

如何在流水线中使用Docker容器进行编译构建

配置DockerInDocker

配置JenkinsMaster挂载Docker

docker run --name jenkins -itd \ -p 8081:8080 \ -p 50000:50000 \ -v ~/jenkins:/var/jenkins_home \ -v /var/run/docker.sock:/var/run/docker.sock \ -v /usr/local/bin/docker:/usr/bin/docker \ jenkins/jenkins:lts

解决权限问题/以root用户运行

docker exec -it -u root jenkins bashusermod -aG root jenkins id jenkinsuid=1000(jenkins) gid=1000(jenkins) groups=1000(jenkins),0(root),1(daemon)

调试流水线

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

Started by user adminReplayed #40Running in Durability level: MAX_SURVIVABILITY[Pipeline] Start of Pipeline[Pipeline] nodeRunning on Jenkins in /var/jenkins_home/workspace/test[Pipeline] {[Pipeline] isUnix[Pipeline] sh+ docker inspect -f . maven:3.6.3-jdk-8.[Pipeline] withDockerContainerJenkins seems to be running inside container 5373edddcdadb63df5e5c6ed7f6149719ad749536242f10c06cbceca511a9898$ docker run -t -d -u 1000:1000 -w /var/jenkins_home/workspace/test --volumes-from 5373edddcdadb63df5e5c6ed7f6149719ad749536242f10c06cbceca511a9898 -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** maven:3.6.3-jdk-8 cat$ docker top 01102f26f8957fc7c46d0f9d1118e38316e313707357cdc5332c7d62836e1df2 -eo pid,comm[Pipeline] {[Pipeline] stage[Pipeline] { (Test)[Pipeline] sh+ mvn -vApache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)Maven home: /usr/share/mavenJava version: 1.8.0_242, vendor: Oracle Corporation, runtime: /usr/local/openjdk-8/jreDefault locale: en, platform encoding: UTF-8OS name: "linux", version: "4.19.76-linuxkit", arch: "amd64", family: "unix"[Pipeline] }[Pipeline] // stage[Pipeline] }$ docker stop --time=1 01102f26f8957fc7c46d0f9d1118e38316e313707357cdc5332c7d62836e1df2$ docker rm -f 01102f26f8957fc7c46d0f9d1118e38316e313707357cdc5332c7d62836e1df2[Pipeline] // withDockerContainer[Pipeline] }[Pipeline] // node[Pipeline] End of PipelineFinished: SUCCESS

每个阶段指定不同的Docker容器运行

pipeline { agent none stages { stage('ServiceBuild') { agent { docker { image 'maven:3.6.3-jdk-8' args '-v $HOME/.m2:/root/.m2' } } steps { sh 'mvn -v && sleep 15' } } stage('WebBuild') { agent { docker { image 'node:7-alpine' args '-v $HOME/.m2:/root/.m2' } } steps { sh 'node -v && sleep 15' } } }}

前端流水线

pipeline { agent none stages { stage('WebBuild') { agent { docker { image 'node:10.19.0-alpine' args '-u 0:0 -v /var/jenkins_home/.npm:/root/.npm' } } steps { sh """ id ls /root/.npm ls /root/ -a npm config set unsafe-perm=true npm config list npm config set cache /root/.npm #npm config set registry npm config list ls npm install --unsafe-perm=true && npm run build && ls -l dist/ && sleep 15 """ } } }}

FAQ

npm构建权限问题:使用root用户构建。设置容器运行用户 -u 0:0

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:数据流量(数据流量是什么)
下一篇:垃圾分类查询(厦门垃圾分类查询)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~