代码拉取完成,页面将自动刷新
pipeline {
// 指定集群 any 表示所有
agent any
// 工具
tools {
maven 'maven'
jdk 'jdk'
}
// 定义常量
environment {
// 钉钉机器人编号
rebootId = 'a3c07482-d031-47a6-8542-05ac56c5f17a'
// 开始logo
imageOfStart = 'https://www.easyicon.net/api/resizeApi.php?id=1229977&size=128'
// 成功logo
imageOfSuccess = 'https://www.easyicon.net/api/resizeApi.php?id=1194837&size=128'
// 失败logo
imageOfFailure = 'https://www.easyicon.net/api/resizeApi.php?id=1201052&size=128'
// 不稳定logo
imageOfUnstable = 'https://www.easyicon.net/api/resizeApi.php?id=1219854&size=128'
// 终止logo
imageOfAborted = 'https://www.easyicon.net/api/resizeApi.php?id=1183198&size=128'
// 认证Id
credentialsId = '98e9c197-f0ae-44c3-8f67-4ca0339028a8'
// 仓库地址
repositoryUrl = 'https://gitee.com/huangxunhui/jenkins_demo.git'
// 打包命令
mavenProd = 'mvn clean package -P prod -Dmaven.test.skip=true'
mavenTest = 'mvn clean package -P test -Dmaven.test.skip=true'
mavenDev = 'mvn clean package -P dev -Dmaven.test.skip=true'
// 服务器名称 - 案例测试-全部部署到dev环境
devServer = 'dev'
testServer = 'dev'
prodServer = 'dev'
// sshPublisher 配置
removePrefix = '/target'
remoteDirectory = '/home/project/jenkins_demo'
sourceFiles = '**/target/jenkins_demo.jar'
execCommandProd = 'cd /home/project && ./manage.sh jenkins_demo/ restart'
execCommandTest = 'cd /home/project && ./manage.sh jenkins_demo/ restart'
execCommandDev = 'cd /home/project && ./manage.sh jenkins_demo/ restart'
}
stages {
stage('开始构建通知'){
steps {
dingtalk (
robot: "${rebootId}",
type: 'LINK',
title: "${env.JOB_NAME}",
text: [
"开始构建-编号为#${BUILD_NUMBER}"
],
messageUrl: "${env.BUILD_URL}",
picUrl: "${imageOfStart}"
)
}
}
stage('拉取代码'){
steps {
echo "拉取 ${BRANCH_NAME} 分支的代码。"
checkout([$class: 'GitSCM', branches: [[name: "*/${BRANCH_NAME}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${credentialsId}", url: "${repositoryUrl}"]]])
}
}
stage('进行打包'){
steps {
script {
if (env.BRANCH_NAME == 'master') {
sh "${mavenProd}"
} else if (env.BRANCH_NAME == 'test') {
sh "${mavenTest}"
} else if (env.BRANCH_NAME == 'dev') {
sh "${mavenDev}"
} else {
sh "${mavenDev}"
}
}
}
}
stage('项目部署'){
steps {
script {
if (env.BRANCH_NAME == 'master') {
// 部署生产环境
sshPublisher(publishers: [sshPublisherDesc(configName: "${prodServer}", transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "${execCommandProd}", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: "${remoteDirectory}", remoteDirectorySDF: false, removePrefix: "${removePrefix}", sourceFiles: "${sourceFiles}")], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
} else if (env.BRANCH_NAME == 'test') {
// 部署测试环境
sshPublisher(publishers: [sshPublisherDesc(configName: "${testServer}", transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "${execCommandTest}", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: "${remoteDirectory}", remoteDirectorySDF: false, removePrefix: "${removePrefix}", sourceFiles: "${sourceFiles}")], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
} else if (env.BRANCH_NAME == 'dev') {
// 部署开发环境
sshPublisher(publishers: [sshPublisherDesc(configName: "${devServer}", transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "${execCommandDev}", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: "${remoteDirectory}", remoteDirectorySDF: false, removePrefix: "${removePrefix}", sourceFiles: "${sourceFiles}")], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
} else {
sshPublisher(publishers: [sshPublisherDesc(configName: "${devServer}", transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "${execCommandTest}", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: "${remoteDirectory}", remoteDirectorySDF: false, removePrefix: "${removePrefix}", sourceFiles: "${sourceFiles}")], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
}
}
}
// 流水线结束通知
post {
// 成功通知
success {
dingtalk (
robot: "${rebootId}",
type: 'LINK',
title: "${env.JOB_NAME}",
text: [
"构建成功-编号为#${BUILD_NUMBER}"
],
messageUrl: "${env.BUILD_URL}",
picUrl: "${imageOfSuccess}"
)
}
// 失败通知
failure {
dingtalk (
robot: "${rebootId}",
type: 'LINK',
title: "${env.JOB_NAME}",
text: [
"构建失败-编号为#${BUILD_NUMBER}"
],
messageUrl: "${env.BUILD_URL}",
picUrl: "${imageOfFailure}"
)
}
// 构建不稳定通知
unstable {
dingtalk (
robot: "${rebootId}",
type: 'LINK',
title: "${env.JOB_NAME}",
text: [
"构建不稳定-编号为#${BUILD_NUMBER}"
],
messageUrl: "${env.BUILD_URL}",
picUrl: "${imageOfUnstable}"
)
}
// 构建终止通知
aborted {
dingtalk (
robot: "${rebootId}",
type: 'LINK',
title: "${env.JOB_NAME}",
text: [
"构建终止-编号为#${BUILD_NUMBER}"
],
messageUrl: "${env.BUILD_URL}",
picUrl: "${imageOfAborted}"
)
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。