之前写了一个是使用更新已存在deployment镜像的方式进行更新,这种方式有个什么问题呢,比如你想在本次上线的时候增加一个目录挂载,就需要配置yaml,重启后才能再次进行上线操作也就是更新镜像。
原来的流程:
手动编写deploy的yaml,然后启动pod
下次上线直接set image修改deploy,而不是修改yaml文件
现在的流程:
将yaml文件写在pipeline脚本中,每次上线都使用yaml文件
脚本:
pipeline {
agent any
environment {
//jenkins服务器上maven路径
MAVEN_HOME = "/usr/local/maven3.6"
//程序包名称
ServiceName = "crm"
//镜像名称
ImageName = "uat-crm-pipeline"
//内存限制
MEM_LIMIT = "2048"
//包类型
TYPE = "jar"
//程序端口
PORT = "18080"
//jar程序启动指定环境配置文件参数
ENV = "uat"
//uat环境镜像仓库
Reg_NameSpace = "uat"
//当前项目下jar包路径
JENKINS_DIR = "crm-web/target"
//仓库地址
VPC_REGISTRY = "registry-vpc.cn-hangzhou.aliyuncs.com"
//k8s上deploy名称
Deployment = "uat-crm-app"
//namespace
NameSpace_name = "uat"
//pod日志目录
Pod_log = "/workspace/logs/crm"
//host日志目录
Host_log = "/uat-pod-log/uat_crm"
//cpu
L_CPU = "2000m"
R_CPU = "500m"
//memory
L_Memory = "2Gi"
R_Memory = "2Gi"
//pod数量
Replicas_Num = "1"
//pod端口
Container_Port = "18080"
}
stages {
stage('下载代码') {
steps {
checkout([$class: 'SubversionSCM', additionalCredentials: [], excludedCommitMessages: '', excludedRegions: '', excludedRevprop: '', excludedUsers: '', filterChangelog: false, ignoreDirPropChanges: false, includedRegions: '', locations: [[cancelProcessOnExternalsFail: true, credentialsId: 'b8dbc5f5-b93f-430c-ac5a-a74a59c4899e', depthOption: 'infinity', ignoreExternalsOption: true, local: '.', remote: 'https://192.168.0.100:4431/svn/online/com_hills/crm-backend/@${svn_version}']], quietOperation: true, workspaceUpdater: [$class: 'UpdateUpdater']])
}
}
stage('构建') {
steps {
sh "${MAVEN_HOME}/bin/mvn clean package -Dmaven.test.skip=true"
}
}
stage('创建docker镜像Dockerfile和程序启动脚本') {
steps {
sh '''start_command="java -Xms${MEM_LIMIT}m -Xmx${MEM_LIMIT}m -javaagent:/usr/local/agent/skywalking-agent.jar -Dskywalking.agent.service_name=uat-crm -jar $ServiceName.$TYPE --debug=false --server.port=$PORT --spring.cloud.nacos.discovery.server-addr=jsb-nacos:8848 --spring.profiles.active=$ENV"
docker_image="registry-vpc.cn-hangzhou.aliyuncs.com/uat/java-sky-agent:v4"
echo -e "#!/bin/bash\\n${start_command}" > docker-entrypoint.sh
echo "${start_command}" > startsvc
cat << EOF > Dockerfile
FROM ${docker_image}
WORKDIR /data
COPY ${JENKINS_DIR}/$ServiceName.$TYPE /data
COPY docker-entrypoint.sh /usr/local/bin
ENV TZ Asia/Shanghai
ENV LANG en_US.UTF-8
RUN chmod +x /usr/local/bin/docker-entrypoint.sh && ln -s /usr/local/bin/docker-entrypoint.sh
ENTRYPOINT ["docker-entrypoint.sh"]
EOF'''
}
}
stage('构建镜像') {
steps {
sh "docker build -t ${ImageName} ."
}
}
stage('修改镜像名称') {
steps {
sh "docker tag ${ImageName}:latest ${VPC_REGISTRY}/${Reg_NameSpace}/${ImageName}:${BUILD_NUMBER}"
}
}
stage('上传镜像到仓库') {
steps {
sh "docker push ${VPC_REGISTRY}/${Reg_NameSpace}/${ImageName}:${BUILD_NUMBER}"
}
}
stage('远程执行镜像更新') {
steps {
script {
def remote = [:]
remote.name = 'uat'
remote.host = '192.168.0.200'
remote.user = 'root'
remote.password = '1111111111'
remote.allowAnyHosts = true
stage('push yaml file') {
writeFile file: "${ServiceName}.yaml", text: """kind: Deployment
apiVersion: apps/v1
metadata:
labels:
app: uat-${ServiceName}-app
name: uat-${ServiceName}-app
namespace: ${NameSpace_name}
spec:
minReadySeconds: 100
progressDeadlineSeconds: 600
replicas: ${Replicas_Num}
revisionHistoryLimit: 10
selector:
matchLabels:
app: uat-${ServiceName}-app
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
type: RollingUpdate
template:
metadata:
annotations:
labels:
app: uat-${ServiceName}-app
spec:
containers:
- env:
- name: LANG
value: C.UTF-8
- name: TZ
value: Asia/Shanghai
image: ${VPC_REGISTRY}/${Reg_NameSpace}/${ImageName}:${BUILD_NUMBER}
imagePullPolicy: IfNotPresent
name: ${ServiceName}-app
ports:
- containerPort: ${Container_Port}
protocol: TCP
resources:
limits:
cpu: ${L_CPU}
memory: ${L_Memory}
requests:
cpu: ${R_CPU}
memory: ${R_Memory}
volumeMounts:
- mountPath: ${Pod_log}
name: log
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 100
volumes:
- hostPath:
path: ${Host_log}
type: ''
name: log"""
sshPut remote: remote, from: "${ServiceName}.yaml", into: '/tmp'
}
stage('远程执行kubectl更新') {
sshCommand remote: remote, command: "kubectl apply -f /tmp/${ServiceName}.yaml"
}
}
}
}
}
}
远程执行镜像更新处需要安装插件:SSH pipeline steps