Created
November 28, 2018 08:56
-
-
Save chanjarster/bb93f2536ea947d6f00077abd7dd2ad5 to your computer and use it in GitHub Desktop.
Maven+Docker的Jenkins pipeline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pipeline { | |
agent any | |
triggers { | |
pollSCM '*/5 * * * *' | |
} | |
options { | |
// 禁止因Multibranch pipeline index动作触发构建 | |
overrideIndexTriggers false | |
// 禁止并发构建 | |
disableConcurrentBuilds() | |
// 构建结果保留天数10天,最多保留10个 | |
buildDiscarder logRotator(daysToKeepStr: '30', numToKeepStr: '10') | |
// 控制台打出时间戳 | |
// timestamps() | |
// 构建超时设置:15分钟 | |
timeout(15) | |
} | |
stages { | |
stage('Build & Test') { | |
steps { | |
withMaven( | |
// Jenkins全局工具设置的Maven的名字 | |
maven: 'Maven3', | |
// Jenkins全局工具设置的JDK的名字,有JDK6,JDK7,JDK8可选 | |
jdk: 'JDK8', | |
// 指定maven本地仓库路径为项目working dir下的.local-m2-reop,可以避免因并发构建导致本地仓库混乱的问题 | |
mavenLocalRepo: '.local-m2-repo' | |
) { | |
sh 'mvn clean install' | |
} | |
} | |
} | |
stage('Push nexus') { | |
when { | |
// 只有当前分支是develop时才执行本Stage | |
branch 'develop' | |
beforeAgent true | |
} | |
steps { | |
withMaven( | |
maven: 'Maven3', | |
jdk: 'JDK8', | |
mavenLocalRepo: '.local-m2-repo', | |
// 只是Deploy,因此把所有options都关闭掉(默认是都开启的) | |
publisherStrategy: 'EXPLICIT' | |
) { | |
sh 'mvn deploy -DskipTests -DskipITs' | |
} | |
} | |
} | |
stage ('Push harbor') { | |
when { | |
branch 'develop' | |
beforeAgent true | |
} | |
steps { | |
withMaven( | |
maven: 'Maven3', | |
jdk: 'JDK8', | |
mavenLocalRepo: '.local-m2-repo', | |
publisherStrategy: 'EXPLICIT' | |
) { | |
sh 'mvn dockerfile:build dockerfile:push' | |
} | |
} | |
} | |
} | |
post { | |
always { | |
// 发送邮件到本次change所包含的committer | |
emailext recipientProviders: [developers()], | |
subject: "Pipeline [${currentBuild.fullDisplayName}] built ${currentBuild.currentResult}", | |
body: "More details: ${currentBuild.absoluteUrl}" | |
} | |
cleanup { | |
// 清空workspace,节省Jenkins服务器磁盘空间 | |
echo 'Cleanup workspace' | |
withMaven(maven: 'Maven3', jdk: 'JDK8', mavenLocalRepo: '.local-m2-repo') { | |
sh 'mvn clean' | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment