目录
系列文章目录
一、需求背景
二、编译任务构建配置
1、新建item
2、配置编译任务项目安全
3、配置编译任务构建策略
4、配置构建参数选项
5、配置构建源码编辑
6、配置构建触发器编辑
7、配置构建工具
8、配置构建脚本
一、需求背景
统一各环境构建流程,提高CICD成功率以及时效性
二、编译任务构建配置
1、新建item
按需选择构建任务模板类型
2、配置编译任务项目安全
授权当前构建任务相关用户组/用户所需权限
3、配置编译任务构建策略
合理的构建策略可用于更好的项目复盘
4、配置构建参数选项
配置构建环境布尔参数
auto_deploy_test #自动部署到TEST环境
auto_deploy_uat #自动部署到UAT环境
auto_deploy_pre #自动部署到PRE环境
auto_deploy_prod #自动部署到生产环境
up_pro_img #上传到生产环境镜像服务器
配置构建分支Git参数:
branch
配置构建服务参数:
配置构建服务布尔参数:
bladex构建服务选项:
blade_resource
blade_system
blade_admin
blade_auth
blade_log
blade_desk
blade_gateway
blade_openapi_gateway
blade_develop
blade_job
blade_swagger
blade_report
blade_flow
5、配置构建源码
6、配置构建触发器
7、配置构建工具
使用开发环境相同maven版本,避免版本差异问题。
clean package -U -Dmaven.test.skip=true -e
8、配置构建脚本
#!/bin/bash
#jenkins自动化构建并部署到k8s脚本
#VERSION=`mvn help:evaluate -Dexpression=project.version | grep "^[^\[]"`
#echo 'VERSION>>>>'$VERSION
tag=3.1.0.RELEASE`date +%Y%m%d%H%M`
echo 'tag>>>>'$tag
jenkins_job=bladex-base-x.x
workspace=/data/jenkins/workspace/bladex-base-3.1
#harbor
harbor_name=xxxxxx
harbor_passwd=xxxxxx
harbor_url=harbor.xxxx.tech
harbor_name_pro=xxxx@aliyun.com
harbor_passwd_pro=xxxx
harbor_url_pro=registry.cn-shenzhen.aliyuncs.com
#私有仓库地址和库名
harbor=harbor.xxxx.tech/k8s
harbor_pro=registry.cn-shenzhen.aliyuncs.com/xxxx
if [ $blade_auth = true ]
then
build_project="$build_project"" blade-auth"
# dockerfile=$(find /var/lib/jenkins/workspace/bladex-base-3.1 -name blade-auth | awk 'NR==1')
# image=$harbor/blade-auth:$tag
# image_pro=$harbor_pro/blade-auth:$tag
#docker build -t $image $dockerfile/.
#workspace=/var/lib/jenkins/workspace/blade-base-k8s/
fi
if [ $blade_gateway = true ]
then
build_project="$build_project"" blade-gateway"
# dockerfile=$(find /var/lib/jenkins/workspace/bladex-base-3.1 -name blade-gateway | awk 'NR==1')
# image=$harbor/blade-gateway:$tag
# image_pro=$harbor_pro/blade-gateway:$tag
# docker build -t $image $dockerfile/.
#workspace=/var/lib/jenkins/workspace/blade-base-k8s/
fi
if [ $blade_admin = true ]
then
build_project="$build_project"" blade-admin"
# dockerfile=$(find /var/lib/jenkins/workspace/bladex-base-3.1 -name blade-admin | awk 'NR==1')
# image=$harbor/blade-admin:$tag
# image_pro=$harbor_pro/blade-admin:$tag
#docker build -t $image $dockerfile/.
#workspace=/var/lib/jenkins/workspace/blade-base-k8s/blade-ops/blade-admin
fi
if [ $blade_report = true ]
then
build_project="$build_project"" blade-report"
# dockerfile=$(find /var/lib/jenkins/workspace/bladex-base-3.1 -name blade-report | awk 'NR==1')
# image=$harbor/blade-report:$tag
# image_pro=$harbor_pro/blade-report:$tag
#docker build -t $image $dockerfile/.
#workspace=/var/lib/jenkins/workspace/blade-base-k8s/blade-ops/blade-develop
fi
if [ $blade_flow = true ]
then
build_project="$build_project"" blade-flow"
# dockerfile=$(find /var/lib/jenkins/workspace/bladex-base-3.1 -name blade-flow | awk 'NR==1')
# image=$harbor/blade-flow:$tag
# image_pro=$harbor_pro/blade-flow:$tag
# docker build -t $image $dockerfile/.
#workspace=/var/lib/jenkins/workspace/blade-base-k8s/blade-ops/blade-flow
fi
if [ $blade_log = true ]
then
build_project="$build_project"" blade-log"
# dockerfile=$(find /var/lib/jenkins/workspace/bladex-base-3.1 -name blade-log | awk 'NR==1')
# image=$harbor/blade-log:$tag
# image_pro=$harbor_pro/blade-log:$tag
#docker build -t $image $dockerfile/.
#workspace=/var/lib/jenkins/workspace/blade-base-k8s/blade-ops/blade-log
fi
if [ $blade_resource = true ]
then
build_project="$build_project"" blade-resource"
# dockerfile=$(find /var/lib/jenkins/workspace/bladex-base-3.1 -name blade-resource | awk 'NR==1')
# image=$harbor/blade-resource:$tag
# image_pro=$harbor_pro/blade-resource:$tag
# docker build -t $image $dockerfile/.
#workspace=/var/lib/jenkins/workspace/blade-base-k8s/blade-ops/blade-resource
fi
if [ $blade_desk = true ]
then
build_project="$build_project"" blade-desk"
# dockerfile=$(find /var/lib/jenkins/workspace/bladex-base-3.1 -name blade-desk | awk 'NR==1')
# image=$harbor/blade-desk:$tag
# image_pro=$harbor_pro/blade-desk:$tag
#docker build -t $image $dockerfile/.
#workspace=/var/lib/jenkins/workspace/blade-base-k8s/blade-service/blade-desk
fi
if [ $blade_system = true ]
then
build_project="$build_project"" blade-system"
#dockerfile=$(find /var/lib/jenkins/workspace/bladex-base-3.1 -name blade-system | awk 'NR==1')
# image=$harbor/blade-system:$tag
# image_pro=$harbor_pro/blade-system:$tag
# docker build -t $image $dockerfile/.
#workspace=/var/lib/jenkins/workspace/blade-base-k8s/blade-service/blade-system
fi
if [ $blade_swagger = true ]
then
build_project="$build_project"" blade-swagger"
#dockerfile=$(find /var/lib/jenkins/workspace/bladex-base-3.1 -name blade-system | awk 'NR==1')
# image=$harbor/blade-system:$tag
# image_pro=$harbor_pro/blade-system:$tag
# docker build -t $image $dockerfile/.
#workspace=/var/lib/jenkins/workspace/blade-base-k8s/blade-service/blade-system
fi
if [ $blade_develop = true ]
then
build_project="$build_project"" blade-develop"
#dockerfile=$(find /var/lib/jenkins/workspace/bladex-base-3.1 -name blade-system | awk 'NR==1')
# image=$harbor/blade-system:$tag
# image_pro=$harbor_pro/blade-system:$tag
# docker build -t $image $dockerfile/.
#workspace=/var/lib/jenkins/workspace/blade-base-k8s/blade-service/blade-system
fi
if [ $blade_xxljob_admin = true ]
then
build_project="$build_project"" blade-xxljob-admin"
# dockerfile=$(find /var/lib/jenkins/workspace/blade-base-k8s/blade-ops -name blade-xxljob-admin)
#workspace=/var/lib/jenkins/workspace/blade-base-k8s/blade-ops/blade-xxljob-admin
fi
if [ $blade_openapi_gateway = true ]
then
build_project="$build_project"" blade-gateway"
fi
if [ $blade_job = true ]
then
build_project="$build_project"" blade-job"
fi
#if [ $blade_workflow = true ]
#then
# build_project="$build_project"" blade-workflow"
# export workspace=/var/lib/jenkins/workspace/$jenkins_job/blade-plugin
#fi
for project in $build_project
do
dockerfile=$(find $workspace -name $project | awk 'NR==1')
#dockerfile=$(find $workspace -name $project)
echo "$project项目更新需要重新创建docker image"
image=$harbor/$project:$tag
image_pro=$harbor_pro/$project:$tag
#部署配置文件名
#deploy_file=$project.yaml
#dockerfile=$(find $workspace -name $project)
#dockerfile 绝对文件路径
echo "$image"
echo "$dockerfile"
docker build -t $image $dockerfile/.
echo "$image 构建成功"
sleep 2
echo "上传$image 到Harbor私有仓库"
docker login -u$harbor_name -p$harbor_passwd $harbor_url >/dev/null 2>&1
docker push $image
echo "上传基础镜像到中台k8s生产镜像服务器: $up_pro_img"
if [ $up_pro_img == "true" ]
then
docker tag $image $image_pro
echo "上传$image_pro 到Harbor外网仓库"
docker login -u$harbor_name_pro -p$harbor_passwd_pro $harbor_url_pro >/dev/null 2>&1
docker push $image_pro
sleep 2
docker rmi $image_pro
fi
echo "是否部署镜像到测试k8s环境: $auto_deploy_test"
if [ $auto_deploy_test == "true" ]
then
echo "正在部署 $project 到test k8s集群中........................................."
#远程连接到测试k8s-master更新服务
sudo ssh root@192.168.6.60 > /dev/null 2>&1 << EOF
kubectl set image -n bladex3-test deployment/$project $project=$image
exit
EOF
fi
#删除本地构建的镜像
echo "删除$image"
docker rmi $image
if [ $auto_deploy_test == "true" ] && [ $blade_openapi_gateway == "true" ]
then
echo "正在部署 $project 到test k8s集群中........................................."
#远程连接到测试k8s-master更新服务
sudo ssh root@192.168.6.60 > /dev/null 2>&1 << EOF
kubectl set image -n bladex3-test deployment/blade-openapi-gateway blade-openapi-gateway=harbor.xxxx.tech/k8s/blade-gateway:$tag --record
exit
EOF
fi
if [ $auto_deploy_uat == "true" ] && [ $blade_openapi_gateway == "true" ]
then
echo "正在部署 $project 到uat k8s集群中........................................."
#远程连接到测试k8s-master更新服务
sudo ssh root@192.168.6.60 > /dev/null 2>&1 << EOF
kubectl set image -n bladex3-uat deployment/blade-openapi-gateway blade-openapi-gateway=harbor.xxxx.tech/k8s/blade-gateway:$tag --record
exit
EOF
fi
if [ $auto_deploy_pre == "true" ] && [ $blade_openapi_gateway == "true" ]
then
echo "正在部署 $project 到pre k8s集群中........................................."
#远程连接到pre k8s-master更新服务
sudo ssh root@119.23.206.95 > /dev/null 2>&1 << EOF
kubectl set image deployment/blade-openapi-gateway blade-openapi-gateway=harbor.xxxx.tech/k8s/blade-gateway:$tag --record
exit
EOF
fi
if [ $auto_deploy_prod == "true" ] && [ $blade_openapi_gateway == "true" ]
then
echo "正在部署 $project 到pre k8s集群中........................................."
#远程连接到pre k8s-master更新服务
sudo ssh -p 17711 root@120.77.180.163 > /dev/null 2>&1 << EOF
kubectl set image deployment/blade-openapi-gateway blade-openapi-gateway=registry.cn-shenzhen.aliyuncs.com/xxxx/blade-gateway:$tag --record
exit
EOF
fi
if [ $auto_deploy_prod == "true" ]
then
#部署到生产k8s环境 #更新deployment中的image
echo "正在部署 $project 到生产k8s集群中........................................."
#远程连接到生产k8s-master更新服务
sudo ssh -p 17711 root@120.77.180.163 > /dev/null 2>&1 << EOF
kubectl set image deployment/$project $project=$image_pro --record
exit
EOF
fi
echo "是否部署镜像到开发k8s环境: $auto_deploy_dev"
if [ $auto_deploy_dev == "true" ]
then
#部署到开发k8s环境
#更新deployment中的image
echo "正在部署 $project 到开发k8s集群中........................................."
#远程连接到开发k8s-master更新服务
sudo ssh root@192.168.6.60 > /dev/null 2>&1 << EOF
kubectl set image -n bladex3-test deployment/$project $project=$image
exit
EOF
fi
echo "是否部署镜像到uat环境: $auto_deploy_uat"
if [ $auto_deploy_uat == "true" ]
then
echo "正在部署 $project 到uat k8s集群中........................................."
#远程连接到uat k8s-master更新服务
sudo ssh root@192.168.6.60 > /dev/null 2>&1 << EOF
kubectl set image -n bladex3-uat deployment/$project $project=$image
exit
EOF
fi
echo "是否部署镜像到pre环境: $auto_deploy_pre"
if [ $auto_deploy_pre == "true" ]
then
echo "正在部署 $project 到pre k8s集群中........................................."
#远程连接到pre k8s-master更新服务
sudo ssh root@119.23.206.95 > /dev/null 2>&1 << EOF
kubectl set image deployment/$project $project=$image --record
exit
EOF
fi
done