赞
踩
在部署公司前端项目测试环境时,通过实现镜像构建前端包来解决前端打包环境冲突问题。
目前公司的2台Jenkins都只能各自实现2个不同项目的前端构建,这是由于不同的前端开发团队所使用的node以及相关依赖版本不一致。要想解决这个问题,有2种解决方案。
这次部署使用的是第二种方案。核心思路是选取对应node版本的官方镜像,构建镜像时将代码拷贝到镜像中执行相应的命令(安装源切换工具、添加公司源、选择公司源、安装依赖包、打包),之后启动容器,将dist包复制到宿主机,最后远程发布即可。
pipeline { agent any stages { stage('Git Pull') { steps { git(url: 'https://xxxxxxxxx', branch: 'feature/ditest',credentialsId: 'guojx') echo 'pull seccess' } } stage('NPM Build') { steps { sh '''mkdir ../data-show-mobile cp -r * ../data-show-mobile mv ../data-show-mobile ./ cat>${WORKSPACE}/Dockerfile<<EOF FROM node:8.12.0 WORKDIR /opt/data-show-mobile/ COPY data-show-mobile /opt/data-show-mobile RUN npm install -g nrm && nrm add test http://xxxxxx:4873/ && nrm use test && npm install && npm run build EOF docker build -t ${IMAGE_NAME} . docker run --name ${CONTAINER_NAME} -itd ${IMAGE_NAME} docker cp ${CONTAINER_NAME}:/opt/data-show-mobile/dist ${WORKSPACE}/ mv dist/ h5/''' } post { always { sh 'rm -rf data-show-mobile' } } } stage('SSH') { steps { script { def remote = [:] remote.name = 'xxx' remote.host = 'xx.xx.xxx.xxx' remote.allowAnyHosts = true withCredentials([usernamePassword(credentialsId: 'IxxP', passwordVariable: 'password', usernameVariable: 'username')]) { remote.user = "${username}" remote.password = "${password}" } sshPut remote: remote, from: 'h5', into: '/usr/local/nginx/html' } } } } environment { IMAGE_NAME = 'xxx:xxx' CONTAINER_NAME = 'xxx_h5' PROJECT_NAME = 'data-xxx-xxxxxxx' } post { always { sh '''set +e environmental_clean(){ docker_ps=`docker ps | grep ${CONTAINER_NAME}` docker_psa=`docker ps -a | grep ${CONTAINER_NAME}` if [[ 0 -eq $docker_ps ]]; then #容器未启动 echo "容器${CONTAINER_NAME}未启动" else echo "停止容器" docker stop ${CONTAINER_NAME} fi if [[ 0 -eq $docker_psa ]]; then echo "容器${CONTAINER_NAME}不存在" else echo "删除容器" docker rm ${CONTAINER_NAME} fi } #docker 环境清理 environmental_clean docker rmi ${IMAGE_NAME}''' } } options { disableConcurrentBuilds() timeout(time: 1, unit: 'HOURS') } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。