Declarative Pipeline语法
pipeline{
agentany
options{
timeout(time: 1,unit:'HOURS')//超时设置 timeout(10)//设置超时,单位:分钟
retry(2)//重试次数
buildDiscarder(logRotator(numToKeepStr:'3'))//保持构建的最大个数
}
tools{//工具名称必须在Jenkins 管理Jenkins → 全局工具配置中预配置。如果agent none,这将被忽略
maven'Maven3.5'
jdk'java8'
}
stages{
stage('download code') {
agent{
label"docker-manager-26"
}
steps{
dir('/home/pipline/build/aliyun'){
sh'/home/maven/apache-maven-3.5.0/bin/mvn install'
}
}
post{//定义Pipeline或stage运行结束时的操作
always{
dir('/home/pipline/build/sharebuy'){
sh'/home/maven/apache-maven-3.5.0/bin/mvn install'
}
}
}
}
stage('mvn-code'){
agent{
label"docker-manager-26"
}
steps{
script{//在Declarative Pipeline中执行script Pipeline代码
dir('/home/pipline/build/aliyun'){
defmvnHome = tool'Maven3.5'
env.PATH ="${mvnHome}/bin:${env.PATH}"
defJAVA_HOME = tool'java8'
env.PATH ="${JAVA_HOME}/bin:${env.PATH}"
sh'/home/maven/apache-maven-3.5.0/bin/mvn install'
}
}
}
}
}
post{
always{
echo'I will always say Hello again!' # 一般用于执行步骤后发送通知
}
}
}
补充:pipeline加入钉钉通知(首先需要在插件管理界面搜索钉钉插件安装,安装完成后重启生效)
post{//发送构建失败通知
failure{
dingTalk accessToken: 'xxxxxx',
imageUrl: 'http://xxx/failure.png', jenkinsUrl: 'http://xxxx/', message: '下载代码失败', notifyPeople: 'phone'
}
}
post{
//发送构建成功通知
success{
dingTalk accessToken: 'xxxx',
imageUrl: 'http://xxxxx/success.png', jenkinsUrl: 'http://xxxxx/', message: '构建成功', notifyPeople: 'phone'
}
}