@ -1,21 +1,26 @@
pipeline {
pipeline {
parameters {
string(name: 'PROJECT_NAME', defaultValue: 'my-project', description: '项目名称')
string(name: 'DOCKER_REPOSITORY_CREDENTIAL_ID', defaultValue: 'docker-repo-cred', description: 'Docker 仓库凭证 ID')
string(name: 'HARBOR_HOST', defaultValue: 'harbor.mycompany.com', description: 'Harbor 主机地址')
string(name: 'NAMESPACE_NAME', defaultValue: 'my-namespace', description: 'Kubernetes 命名空间')
string(name: 'JAVA_TAG', defaultValue: 'latest', description: 'Java 镜像标签')
string(name: 'NODE_TAG', defaultValue: 'latest', description: 'Node.js 镜像标签')
string(name: 'JAVA_REPLICAS', defaultValue: '2', description: 'Java 部署副本数量')
string(name: 'NODE_REPLICAS', defaultValue: '2', description: 'Node.js 部署副本数量')
string(name: 'JENKINS_URL', defaultValue: 'http://jenkins.mycompany.com', description: 'Jenkins URL')
}
environment {
environment {
PROJECT_NAME = "${PROJECT_NAME}"
DOCKER_REPOSITORY_CREDENTIAL_ID = "${DOCKER_REPOSITORY_CREDENTIAL_ID}"
HARBOR_HOST = "${HARBOR_HOST}"
NAMESPACE_NAME = "${NAMESPACE_NAME}"
JAVA_REPOSITORY_NAME = "ruoyi-java"
JAVA_REPOSITORY_NAME = "ruoyi-java"
NODE_REPOSITORY_NAME = "rupyi-node"
NODE_REPOSITORY_NAME = "rupyi-node"
JAVA_TAG = "${JAVA_TAG}"
NODE_TAG = "${NODE_TAG}"
JAVA_REPLICAS = "${JAVA_REPLICAS}"
NODE_REPLICAS = "${NODE_REPLICAS}"
YAML_PATH = "cloud/deploy/"
YAML_PATH = "cloud/deploy/"
JAVA_DEPLOYMENT_NAME = "ruoyi-java.yaml"
JAVA_DEPLOYMENT_NAME = "ruoyi-java.yaml"
NODE_DEPLOYMENT_NAME = "ruoyi-nginx.yaml"
NODE_DEPLOYMENT_NAME = "ruoyi-nginx.yaml"
STATUS_URL = "${JENKINS_URL}/job/ruoyi/${BUILD_NUMBER}"
STATUS_URL = "${params.JENKINS_URL}/job/ruoyi/${BUILD_NUMBER}"
CONSOLE_URL = "${JENKINS_URL}/job/ruoyi/${BUILD_NUMBER}/console"
CONSOLE_URL = "${params. JENKINS_URL}/job/ruoyi/${BUILD_NUMBER}/console"
}
}
agent {
agent {
kubernetes {
kubernetes {
cloud "kubernetes"
cloud "kubernetes"
@ -64,7 +69,7 @@ pipeline {
name: "kubeconfig"
name: "kubeconfig"
readOnly: false
readOnly: false
- name: docker
- name: docker
image: docker:19.03.15 -git
image: docker:24.0.0 -git
command: ['cat']
command: ['cat']
tty: true
tty: true
volumeMounts:
volumeMounts:
@ -93,6 +98,7 @@ pipeline {
"""
"""
}
}
}
}
stages {
stages {
stage('Maven 打包') {
stage('Maven 打包') {
steps {
steps {
@ -108,15 +114,15 @@ pipeline {
dingtalk (
dingtalk (
robot: '66dc8be5-85f4-46dd-89f5-d4efb3abab12',
robot: '66dc8be5-85f4-46dd-89f5-d4efb3abab12',
type: 'MARKDOWN',
type: 'MARKDOWN',
title: '后端打包成功 ${PROJECT_NAME}' ,
title: "后端打包成功 ${params.PROJECT_NAME}" ,
text: [
text: [
'### 项目 ${PROJECT_NAME}' ,
"### 项目 ${params.PROJECT_NAME}" ,
'---' ,
"---" ,
'- 状态:<font color=#00FF00 >成功</font>' ,
"- 状态:<font color=#00FF00 >成功</font>" ,
'- 阶段:后端打包' ,
"- 阶段:后端打包" ,
'- 版本:${BUILD_NUMBER}' ,
"- 版本:${BUILD_NUMBER}" ,
'- [查看部署详情](${STATUS_URL})' ,
"- [查看部署详情](${STATUS_URL})" ,
'- [查看日志Console](${CONSOLE_URL})'
"- [查看日志Console](${CONSOLE_URL})"
],
],
at: ['17694993496']
at: ['17694993496']
)
)
@ -126,15 +132,15 @@ pipeline {
dingtalk (
dingtalk (
robot: '66dc8be5-85f4-46dd-89f5-d4efb3abab12',
robot: '66dc8be5-85f4-46dd-89f5-d4efb3abab12',
type: 'MARKDOWN',
type: 'MARKDOWN',
title: '后端打包失败 ${PROJECT_NAME}' ,
title: "后端打包失败 ${params.PROJECT_NAME}" ,
text: [
text: [
'### 项目 ${PROJECT_NAME}' ,
"### 项目 ${params.PROJECT_NAME}" ,
'---' ,
"---" ,
'- 状态:<font color=#FF0000 >失败</font>' ,
"- 状态:<font color=#FF0000 >失败</font>" ,
'- 阶段:后端打包' ,
"- 阶段:后端打包" ,
'- 版本:${BUILD_NUMBER}' ,
"- 版本:${BUILD_NUMBER}" ,
'- [查看部署详情](${STATUS_URL})' ,
"- [查看部署详情](${STATUS_URL})" ,
'- [查看日志Console](${CONSOLE_URL})'
"- [查看日志Console](${CONSOLE_URL})"
],
],
at: ['17694993496']
at: ['17694993496']
)
)
@ -156,15 +162,15 @@ pipeline {
dingtalk (
dingtalk (
robot: '66dc8be5-85f4-46dd-89f5-d4efb3abab12',
robot: '66dc8be5-85f4-46dd-89f5-d4efb3abab12',
type: 'MARKDOWN',
type: 'MARKDOWN',
title: '前端打包成功 ${PROJECT_NAME}' ,
title: "前端打包成功 ${params.PROJECT_NAME}" ,
text: [
text: [
'### 项目 ${PROJECT_NAME}' ,
"### 项目 ${params.PROJECT_NAME}" ,
'---' ,
"---" ,
'- 状态:<font color=#00FF00 >成功</font>' ,
"- 状态:<font color=#00FF00 >成功</font>" ,
'- 阶段:前端打包' ,
"- 阶段:前端打包" ,
'- 版本:${BUILD_NUMBER}' ,
"- 版本:${BUILD_NUMBER}" ,
'- [查看部署详情](${STATUS_URL})' ,
"- [查看部署详情](${STATUS_URL})" ,
'- [查看日志Console](${CONSOLE_URL})'
"- [查看日志Console](${CONSOLE_URL})"
],
],
at: ['17694993496']
at: ['17694993496']
)
)
@ -174,15 +180,15 @@ pipeline {
dingtalk (
dingtalk (
robot: '66dc8be5-85f4-46dd-89f5-d4efb3abab12',
robot: '66dc8be5-85f4-46dd-89f5-d4efb3abab12',
type: 'MARKDOWN',
type: 'MARKDOWN',
title: '前端打包失败 ${PROJECT_NAME}' ,
title: "前端打包失败 ${params.PROJECT_NAME}" ,
text: [
text: [
'### 项目 ${PROJECT_NAME}' ,
"### 项目 ${params.PROJECT_NAME}" ,
'---' ,
"---" ,
'- 状态:<font color=#FF0000 >失败</font>' ,
"- 状态:<font color=#FF0000 >失败</font>" ,
'- 阶段:前端打包' ,
"- 阶段:前端打包" ,
'- 版本:${BUILD_NUMBER}' ,
"- 版本:${BUILD_NUMBER}" ,
'- [查看部署详情](${STATUS_URL})' ,
"- [查看部署详情](${STATUS_URL})" ,
'- [查看日志Console](${CONSOLE_URL})'
"- [查看日志Console](${CONSOLE_URL})"
],
],
at: ['17694993496']
at: ['17694993496']
)
)
@ -192,14 +198,14 @@ pipeline {
stage('构建镜像') {
stage('构建镜像') {
steps {
steps {
withCredentials([usernamePassword(credentialsId: env .DOCKER_REPOSITORY_CREDENTIAL_ID, passwordVariable: 'PASSWORD', usernameVariable: 'USERNAME')]) {
withCredentials([usernamePassword(credentialsId: params .DOCKER_REPOSITORY_CREDENTIAL_ID, passwordVariable: 'PASSWORD', usernameVariable: 'USERNAME')]) {
container('docker') {
container('docker') {
sh """
sh """
docker build -t ${env.HARBOR_HOST}/${env.NAMESPACE_NAME}/${env.JAVA_REPOSITORY_NAME}:${env .JAVA_TAG} cloud/ruoyi-java
docker build -t ${params.HARBOR_HOST}/${params.NAMESPACE_NAME}/${env.JAVA_REPOSITORY_NAME}:${params .JAVA_TAG} cloud/ruoyi-java
docker build -t ${env.HARBOR_HOST}/${env.NAMESPACE_NAME}/${env.NODE_REPOSITORY_NAME}:${env .NODE_TAG} cloud/ruoyi-nginx
docker build -t ${params.HARBOR_HOST}/${params.NAMESPACE_NAME}/${env.NODE_REPOSITORY_NAME}:${params .NODE_TAG} cloud/ruoyi-nginx
docker login ${env .HARBOR_HOST} --username ${env.USERNAME} --password ${env.PASSWORD}
docker login ${params .HARBOR_HOST} --username ${env.USERNAME} --password ${env.PASSWORD}
docker push ${env.HARBOR_HOST}/${env.NAMESPACE_NAME}/${env.JAVA_REPOSITORY_NAME}:${env .JAVA_TAG}
docker push ${params.HARBOR_HOST}/${params.NAMESPACE_NAME}/${env.JAVA_REPOSITORY_NAME}:${params .JAVA_TAG}
docker push ${env.HARBOR_HOST}/${env.NAMESPACE_NAME}/${env.NODE_REPOSITORY_NAME}:${env .NODE_TAG}
docker push ${params.HARBOR_HOST}/${params.NAMESPACE_NAME}/${env.NODE_REPOSITORY_NAME}:${params .NODE_TAG}
"""
"""
}
}
}
}
@ -210,15 +216,15 @@ pipeline {
dingtalk (
dingtalk (
robot: '66dc8be5-85f4-46dd-89f5-d4efb3abab12',
robot: '66dc8be5-85f4-46dd-89f5-d4efb3abab12',
type: 'MARKDOWN',
type: 'MARKDOWN',
title: '镜像构建成功 ${PROJECT_NAME}' ,
title: "镜像构建成功 ${params.PROJECT_NAME}" ,
text: [
text: [
'### 项目 ${PROJECT_NAME}' ,
"### 项目 ${params.PROJECT_NAME}" ,
'---' ,
"---" ,
'- 状态:<font color=#00FF00 >成功</font>' ,
"- 状态:<font color=#00FF00 >成功</font>" ,
'- 阶段:构建镜像' ,
"- 阶段:构建镜像" ,
'- 版本:${BUILD_NUMBER}' ,
"- 版本:${BUILD_NUMBER}" ,
'- [查看部署详情](${STATUS_URL})' ,
"- [查看部署详情](${STATUS_URL})" ,
'- [查看日志Console](${CONSOLE_URL})'
"- [查看日志Console](${CONSOLE_URL})"
],
],
at: ['17694993496']
at: ['17694993496']
)
)
@ -228,15 +234,15 @@ pipeline {
dingtalk (
dingtalk (
robot: '66dc8be5-85f4-46dd-89f5-d4efb3abab12',
robot: '66dc8be5-85f4-46dd-89f5-d4efb3abab12',
type: 'MARKDOWN',
type: 'MARKDOWN',
title: '镜像构建失败 ${PROJECT_NAME}' ,
title: "镜像构建失败 ${params.PROJECT_NAME}" ,
text: [
text: [
'### 项目 ${PROJECT_NAME}' ,
"### 项目 ${params.PROJECT_NAME}" ,
'---' ,
"---" ,
'- 状态:<font color=#FF0000 >失败</font>' ,
"- 状态:<font color=#FF0000 >失败</font>" ,
'- 阶段:构建镜像' ,
"- 阶段:构建镜像" ,
'- 版本:${BUILD_NUMBER}' ,
"- 版本:${BUILD_NUMBER}" ,
'- [查看部署详情](${STATUS_URL})' ,
"- [查看部署详情](${STATUS_URL})" ,
'- [查看日志Console](${CONSOLE_URL})'
"- [查看日志Console](${CONSOLE_URL})"
],
],
at: ['17694993496']
at: ['17694993496']
)
)
@ -248,8 +254,8 @@ pipeline {
steps {
steps {
container('kubectl') {
container('kubectl') {
sh """
sh """
sed -i "s/REPLICAS/${env.JAVA_REPLICAS}/;s/HARBOR_HOST/${env.HARBOR_HOST}/;s/NAMESPACE_NAME/${env.NAMESPACE_NAME}/;s/REPOSITORY_NAME/${env.JAVA_REPOSITORY_NAME}/;s/TAG/${env .JAVA_TAG}/" ${env.YAML_PATH}${env.JAVA_DEPLOYMENT_NAME}
sed -i "s/REPLICAS/${params.JAVA_REPLICAS}/;s/HARBOR_HOST/${params.HARBOR_HOST}/;s/NAMESPACE_NAME/${params.NAMESPACE_NAME}/;s/REPOSITORY_NAME/${env.JAVA_REPOSITORY_NAME}/;s/TAG/${params .JAVA_TAG}/" ${env.YAML_PATH}${env.JAVA_DEPLOYMENT_NAME}
sed -i "s/REPLICAS/${env.NODE_REPLICAS}/;s/HARBOR_HOST/${env.HARBOR_HOST}/;s/NAMESPACE_NAME/${env.NAMESPACE_NAME}/;s/REPOSITORY_NAME/${env.NODE_REPOSITORY_NAME}/;s/TAG/${env .NODE_TAG}/" ${env.YAML_PATH}${env.NODE_DEPLOYMENT_NAME}
sed -i "s/REPLICAS/${params.NODE_REPLICAS}/;s/HARBOR_HOST/${params.HARBOR_HOST}/;s/NAMESPACE_NAME/${params.NAMESPACE_NAME}/;s/REPOSITORY_NAME/${env.NODE_REPOSITORY_NAME}/;s/TAG/${params .NODE_TAG}/" ${env.YAML_PATH}${env.NODE_DEPLOYMENT_NAME}
kubectl apply -f ${env.YAML_PATH} --record
kubectl apply -f ${env.YAML_PATH} --record
"""
"""
}
}
@ -260,15 +266,15 @@ pipeline {
dingtalk (
dingtalk (
robot: '66dc8be5-85f4-46dd-89f5-d4efb3abab12',
robot: '66dc8be5-85f4-46dd-89f5-d4efb3abab12',
type: 'MARKDOWN',
type: 'MARKDOWN',
title: 'k8s部署成功 ${PROJECT_NAME}' ,
title: "k8s部署成功 ${params.PROJECT_NAME}" ,
text: [
text: [
'### 项目 ${PROJECT_NAME}' ,
"### 项目 ${params.PROJECT_NAME}" ,
'---' ,
"---" ,
'- 状态:<font color=#00FF00 >成功</font>' ,
"- 状态:<font color=#00FF00 >成功</font>" ,
'- 阶段:部署yaml' ,
"- 阶段:部署yaml" ,
'- 版本:${BUILD_NUMBER}' ,
"- 版本:${BUILD_NUMBER}" ,
'- [查看部署详情](${STATUS_URL})' ,
"- [查看部署详情](${STATUS_URL})" ,
'- [查看日志Console](${CONSOLE_URL})'
"- [查看日志Console](${CONSOLE_URL})"
],
],
at: ['17694993496']
at: ['17694993496']
)
)
@ -278,15 +284,15 @@ pipeline {
dingtalk (
dingtalk (
robot: '66dc8be5-85f4-46dd-89f5-d4efb3abab12',
robot: '66dc8be5-85f4-46dd-89f5-d4efb3abab12',
type: 'MARKDOWN',
type: 'MARKDOWN',
title: 'k8s部署失败 ${PROJECT_NAME}' ,
title: "k8s部署失败 ${params.PROJECT_NAME}" ,
text: [
text: [
'### 项目 ${PROJECT_NAME}' ,
"### 项目 ${params.PROJECT_NAME}" ,
'---' ,
"---" ,
'- 状态:<font color=#FF0000 >失败</font>' ,
"- 状态:<font color=#FF0000 >失败</font>" ,
'- 阶段:部署yaml' ,
"- 阶段:部署yaml" ,
'- 版本:${BUILD_NUMBER}' ,
"- 版本:${BUILD_NUMBER}" ,
'- [查看部署详情](${STATUS_URL})' ,
"- [查看部署详情](${STATUS_URL})" ,
'- [查看日志Console](${CONSOLE_URL})'
"- [查看日志Console](${CONSOLE_URL})"
],
],
at: ['17694993496']
at: ['17694993496']
)
)