jenkins配置流水线过程
1.新建item
加入相关的参数就行了。
流水线脚本设置
后端脚本
node {
stage 'checkout'
sh"""
#每次打包清空工作空间目录
rm -rf $workspace/*
cd $workspace
#到工作空间下从远端svn服务端拉取代码
svn co svn://10.1.19.21/repo/技术中台/低代码开发平台/projects/iiot-eqp-app/iiot-eqp-app --username "lipeigen" --password "DG-svn-123456"
"""
stage 'Maven Build'
sh"""
cd $workspace/$app_name
#编译后台包,生成jar包
#/app/maven3.6/bin/mvn -e -U clean install -Dmaven.test.skip=true -P test --settings /app/maven3.6/conf/settings-iiot.xml
/app/maven-3.6.2/bin/mvn -e clean install -Dmaven.test.skip=true -P test --settings /app/maven-3.6.2/conf/settings-iiot.xml
"""
stage 'deploy'
sh"""
echo "========start docker build========="
cd $workspace/$app_name
docker login -u admin -p Harbor12345 10.1.19.21:21020
sed -i 's/env_file/$env_file/g' Dockerfile
sed -i 's/app_name/$app_name/g' Dockerfile
sed -i 's/app_version/$app_version/g' Dockerfile
sed -i 's/harbor_registry/10.1.19.21:21020/g' Dockerfile
docker build -t 10.1.19.21:21020/pro/$app_name:$image_version .
docker push 10.1.19.21:21020/pro/$app_name:$image_version
docker rmi 10.1.19.21:21020/pro/$app_name:$image_version
sed -i 's/image_version/$image_version/g;s/appname/$app_name/g;s/harbor_registry/10.1.19.21:21020/g;s/name_space/$name_space/g;s/containerport/$containerport/g;s/appport/$appport/g;s/replicas_num/$replicas_num/g' k8s-deployment-service.yml
echo "========start k8s deployment========="
kubectl apply -f k8s-deployment-service.yml --namespace=$name_space --kubeconfig=/root/.kube/config
"""
}
前端脚本
前端打包分两种:
1种:手动本地 npm run build后上传dist前端打包文件
2种:本地不build,直接在jenkins上build,然后jenkins服务器上需要上传node_modules文件,例如
cp -r /data/node_modules .
npm run build
node {
stage 'checkout'
sh"""
#每次打包清空工作空间目录
rm -rf $workspace/*
cd $workspace
#到工作空间下从远端svn服务端拉取代码
svn co svn://10.1.19.21/repo/技术中台/低代码开发平台/projects/iiot-eqp-app/iiot-eqp-web --username "lipeigen" --password "DG-svn-123456"
"""
stage 'Vue Build'
sh"""
cd $workspace/$app_name
#编译前台包,生成dist
if [ -d "$workspace/$app_name/dist/" ];then
echo "dist文件夹已存在,跳过打包..."
else
echo "dist文件夹不存在,系统打包..."
cp -r /data/node_modules .
npm run build
fi
"""
stage 'deploy'
sh"""
echo "========start docker build========="
cd $workspace/$app_name
docker login -u admin -p Harbor12345 10.1.19.21:21020
sed -i 's/env_file/$env_file/g' Dockerfile
sed -i 's/app_name/$app_name/g' Dockerfile
sed -i 's/app_version/$app_version/g' Dockerfile
sed -i 's/harbor_registry/10.1.19.21:21020/g' Dockerfile
docker build -t 10.1.19.21:21020/pro/$app_name:$image_version .
docker push 10.1.19.21:21020/pro/$app_name:$image_version
docker rmi 10.1.19.21:21020/pro/$app_name:$image_version
sed -i 's/image_version/$image_version/g;s/appname/$app_name/g;s/harbor_registry/10.1.19.21:21020/g;s/name_space/$name_space/g;s/containerport/$containerport/g;s/appport/$appport/g;s/replicas_num/$replicas_num/g' k8s-deployment-service.yml
echo "========start k8s deployment========="
kubectl apply -f k8s-deployment-service.yml --namespace=$name_space --kubeconfig=/root/.kube/config
tar -zcf vue.tar.gz nginx.conf
sshpass -p "e5hfz^I^X98LxQtC" scp -P 20000 -o StrictHostKeyChecking=no -r $workspace/$app_name/vue.tar.gz Oper1@10.1.19.11:/usr/local/nginx/vhost/vue.tar.gz
sshpass -p "e5hfz^I^X98LxQtC" ssh -p 20000 Oper1@10.1.19.11 /bin/bash -x /app2/deploy-vue.sh $app_name
sshpass -p "RCJ5ADeKKtBWMwIe" scp -P 20000 -o StrictHostKeyChecking=no -r $workspace/$app_name/vue.tar.gz Oper1@10.1.19.12:/usr/local/nginx/vhost/vue.tar.gz
sshpass -p "RCJ5ADeKKtBWMwIe" ssh -p 20000 Oper1@10.1.19.12 /bin/bash -x /app2/deploy-vue.sh $app_name
"""
}
添加测试和生产的分组
上传jenkins插件 ,在文件中可以下载
vue多环境配置切换
增加env的三个配置环境文件,设置NODE_ENV="dev"
修改package.json文件,配置不同环境build配置:
打包dev环境命令: npm run build
打包test环境命令: npm run test
打包prod环境命令: npm run prod
对应的jenkins脚本中的命令也进行修改
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build --mode development",
"build:prod": "vue-cli-service build --mode production",
"build:test": "vue-cli-service build --mode test",
"lint": "vue-cli-service lint",
"fix-memory-limit": "cross-env LIMIT=8096 increase-memory-limit"
},