企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
1、使用jenkins创建一个新的项目:dubbo-demo,选择流水线构建 ![](https://img.kancloud.cn/14/57/14573ede6cb954fca8df7bee615ccf4e_718x289.png) 2、 勾选保存构建历史和指定项目为参数化构建项目: ![](https://img.kancloud.cn/a0/67/a067b13ced83b0df7c2e1a8c55fddf59_395x438.png) 3、添加构建参数: 除了base_image和maven是choice parameter,其他都是string parameter ``` app_name 项目名称--例如:dubbo-demo-service image_name docker镜像的名称--例如:app/dubbo-demo-service git_repo 项目仓库git的地址 git_ver 项目在git中央仓库所对应的分支或版本号(cid)--例:master add_tag 给docker镜像添加标签组合的一部分,日期时间戳--例如:201229_1120 mvn_dir ./ 执行mvn编译的目录,默认是项目的跟目录--例:./ target_dir ./target 编译后的他人jar包的存放地址--例:./dubbo-demo-service/target mvn_cmd mvn clean package -Dmaven.test.skip=true 执行编译所用的命令 Name:base_image Choices: base/jre7:7u80 base/jre8:8u112 Description: 指定底包 Name:maven Choices: 3.6.1-8u212 3.2.5-7u045 2.2.1-6u025 Description:执行编译使用的mav软件版本 ``` ![](https://img.kancloud.cn/d1/dd/d1dd3b62d88b864c1bfdb7e13ab7ea65_610x700.png) 编写pipeline:仔细查看这个pipeline,里面都是我们上面编写的参数。 ``` pipeline { agent any stages { stage('pull') { //get project code from repo steps { sh "git clone ${params.git_repo} ${params.app_name}/${env.BUILD_NUMBER} && cd ${params.app_name}/${env.BUILD_NUMBER} && git checkout ${params.git_ver}" } } stage('build') { //exec mvn cmd steps { sh "cd ${params.app_name}/${env.BUILD_NUMBER} && /var/jenkins_home/maven-${params.maven}/bin/${params.mvn_cmd}" } } stage('package') { //move jar file into project_dir steps { sh "cd ${params.app_name}/${env.BUILD_NUMBER} && cd ${params.target_dir} && mkdir project_dir && mv *.jar ./project_dir" } } stage('image') { //build image and push to registry steps { writeFile file: "${params.app_name}/${env.BUILD_NUMBER}/Dockerfile", text: """FROM harbor.od.com/${params.base_image} ADD ${params.target_dir}/project_dir /opt/project_dir""" sh "cd ${params.app_name}/${env.BUILD_NUMBER} && docker build -t harbor.od.com/${params.image_name}:${params.git_ver}_${params.add_tag} . && docker push harbor.od.com/${params.image_name}:${params.git_ver}_${params.add_tag}" } } } } ``` ![](https://img.kancloud.cn/28/15/28150a8b1c34157df300f6306f76946a_1412x546.png) 填写完以后执行bulid ![](https://img.kancloud.cn/73/97/7397fc093b7580c997dbcdf70d033662_523x698.png) 经过漫长的等待后,已经构建完成了,可以点击open blue ocean 查看构建历史及过程: ![](https://img.kancloud.cn/84/af/84af3a346e343dffa1563119d94a8d25_1860x438.png) 检查harbor是否已经有这版镜像: ![](https://img.kancloud.cn/30/fe/30fe42487229988db434d8426b5a8110_498x303.png) 已经有了,接下来交付dubbo-demo-service服务到k8s: 准备k8s资源配置清单:7-200 ** 红色部分,需要根据自己构建镜像的tag来进行修改** mkdir /data/k8s-yaml/dubbo-server/ cd /data/k8s-yaml/dubbo-server vi dp.yaml ``` kind: Deployment apiVersion: extensions/v1beta1 metadata: name: dubbo-demo-service namespace: app labels: name: dubbo-demo-service spec: replicas: 1 selector: matchLabels: name: dubbo-demo-service template: metadata: labels: app: dubbo-demo-service name: dubbo-demo-service spec: containers: - name: dubbo-demo-service image: harbor.od.com/app/web:master_201230_0930 ports: - containerPort: 20880 protocol: TCP env: - name: JAR_BALL value: web_learn-0.0.1-SNAPSHOT.jar imagePullPolicy: IfNotPresent imagePullSecrets: - name: harbor restartPolicy: Always terminationGracePeriodSeconds: 30 securityContext: runAsUser: 0 schedulerName: default-scheduler strategy: type: RollingUpdate rollingUpdate: maxUnavailable: 1 maxSurge: 1 revisionHistoryLimit: 7 progressDeadlineSeconds: 600 ``` 由于我们使用的harbor私有镜像的项目是app,是个私有项目,所以需要创建secret资源: 创建 app命名空间: kubectl create ns app 创建secret资源: kubectl create secret docker-registry harbor --docker-server=harbor.od.com --docker-username=admin --docker-password=Harbor12345 -n app 应用资源配置文件: kubectl apply -f http://k8s-yaml.od.com/dubbo-server/dp.yaml 检查pod是否创建: kubectl get pod -n app ![](https://img.kancloud.cn/f2/52/f252436263e97087622367d8f47f3012_547x74.png) ![](https://img.kancloud.cn/8a/74/8a749d736617d812061a400ec74d752a_1634x768.png)