1.登录到jenkins,新建一个项目
2.新建流水线
3.设置保留的天数及份数
4. 添加参数
# 参数
. name: git_repo
type: string
description: 项目在git版本仓库的地址,如 https://gitee.com/xxx/dubbo-demo-service.git . name: app_name
type: string
description: 项目名称,如 dubbo-demo-service . name: git_ver
type: string
description: 项目在git仓库中对应的分支或者版本号 . name: maven
type: choice
description: 编译时使用的maven目录中的版本号部分 . name: mvn_cmd
type: string
default: mvn clean package -Dmaven.test.skip=true
description: 执行编译所用的指令 . name: mvn_dir
type: string
default: ./
description: 在哪个目录执行编译,由开发同事提供 . name: target_dir
type: string
default: ./target
description: 编译的jar/war文件存放目录,由开发同事提供 . name: base_image
type: choice
default:
description: 项目使用的jre底包 . name: image_name
type: string
description: docker镜像名称,如 app/dubbo-demo-service . name: add_tag
type: string
default:
description: 日期-时间,和git_ver拼在一起组成镜像的tag,如:
5.设置流水线构建脚本
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.fx.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.fx.com/${params.image_name}:${params.git_ver}_${params.add_tag} . && docker push harbor.fx.com/${params.image_name}:${params.git_ver}_${params.add_tag}"
}
}
}
}
6.保存配置
7.私有仓库创建一个名称为app的项目,属性为私有
8.执行流水线
9.输入相关参数
10.构建成功
11.查看私有仓库的app项目是否已经有docker镜像并打开 Blue Ocean可以查看到构建的步骤
12. 准备dubbo-demo-service的k8s资源配置清单文件
[root@hdss7- dubbo-demo-service]# vim deployment.yaml
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
name: dubbo-demo-service
namespace: app
labels:
name: dubbo-demo-service
spec:
replicas:
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.fx.com/app/dubbo-demo-service:master_20200617_1849
ports:
- containerPort:
protocol: TCP
env:
- name: JAR_BALL
value: dubbo-server.jar
imagePullPolicy: IfNotPresent
imagePullSecrets:
- name: harbor
restartPolicy: Always
terminationGracePeriodSeconds:
securityContext:
runAsUser:
schedulerName: default-scheduler
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable:
maxSurge:
revisionHistoryLimit:
progressDeadlineSeconds:
13.查看zookeeper的状态
[root@hdss7- bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/bin/../conf/zoo.cfg
Client port found: . Client address: localhost.
Mode: follower
[root@hdss7- bin]# ./zkCli.sh -server localhost:
[zk: localhost:(CONNECTED) ] ls /
[zookeeper]
[zk: localhost:(CONNECTED) ]
14.应用dubbo-demo-service的k8s的资源配置清单
[root@hdss7- ~]# kubectl create namespace app
[root@hdss7- ~]# kubectl create secret docker-registry harbor --docker-server=harbor.fx.com --docker-username=admin --docker-password=Harbor12345 -n app
[root@hdss7- ~]# kubectl apply -f http://k8s-yaml.fx.com/dubbo-demo-service/deployment.yaml
15.再次查看zookeeper
[zk: localhost:(CONNECTED) ] ls /
[dubbo, zookeeper]
[zk: localhost:(CONNECTED) ]