web开发在流水线中如何使用容器进行编译构建
这篇文章主要为大家展示了“web开发在流水线中如何使用容器进行编译构建”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“web开发在流水线中如何使用容器进行编译构建”这篇文章吧。
创新互联公司主营博州网站建设的网络公司,主营网站建设方案,app软件开发公司,博州h5微信小程序定制开发搭建,博州网站营销推广欢迎博州等地区企业咨询
配置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 bash usermod -aG root jenkins id jenkins uid=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 admin Replayed #40 Running in Durability level: MAX_SURVIVABILITY [Pipeline] Start of Pipeline [Pipeline] node Running on Jenkins in /var/jenkins_home/workspace/test [Pipeline] { [Pipeline] isUnix [Pipeline] sh + docker inspect -f . maven:3.6.3-jdk-8 . [Pipeline] withDockerContainer Jenkins 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 -v Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f) Maven home: /usr/share/maven Java version: 1.8.0_242, vendor: Oracle Corporation, runtime: /usr/local/openjdk-8/jre Default locale: en, platform encoding: UTF-8 OS 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 Pipeline Finished: 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 https://registry.npm.taobao.org npm config list ls npm install --unsafe-perm=true && npm run build && ls -l dist/ && sleep 15 """ } } } }
FAQ
npm构建权限问题:使用root用户构建。设置容器运行用户 -u 0:0
npm打包慢问题:
2.1 挂载缓存卷
-v /var/jenkins_home/.npm:/root/.npm
2.2 设置淘宝源
npm config set registry https://registry.npm.taobao.org
以上是“web开发在流水线中如何使用容器进行编译构建”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!
网站名称:web开发在流水线中如何使用容器进行编译构建
当前地址:http://pwwzsj.com/article/gosjei.html