文章目录
- 多选参数
- `定时构建`
一、语法
timestamps {
properties ([
parameters ([
string(name: 'NEW_BRANCH', defaultValue: '' , description:'', trim: true),
choice(choices: ['mysql', 'oracle'], description: '''选择数据库类型''', name: 'DATABASE', trim: true),
])
])
node('cs') {
//定义变量
def db_ip = '192.168.1.2'
try {
step([$class: 'WsCleanup']) //清理工作空间
stage('git pull') {
gitClone(branch)
}
//并行构建
parallel(
'npm web': {
stage('npm') {
sh """
shell命令
"""
}
},
'mvn': {
stage('mvn') {
sh """
shell命令
"""
}
}
)
} catch (e) {
throw e
} finally {
}
}
}
//定义函数
def gitClone(branch) {
sh "git config --global http.sslVerify false"
for (gitProductName in gitProductList) {
sh "git clone --depth=1 -b ${branch} git地址"
}
}
二、定义变量
在if语句里面定义的变量,在函数中可以直接调用
dbDriver = "com.mysql.jdbc.Driver" //可以直接在函数中使用
def dbDriver = "com.mysql.jdbc.Driver" //函数中不能直接使用,需要当作实参传入给函数
三、properties属性
参数化构建
properties ([
parameters ([
string(name: 'NEW_BRANCH', defaultValue: '' , description:'', trim: true),
text(name: 'DOCKER_HOST',defaultValue: this.DOCKER_HOST, description: '''填写Docker主机的IP地址,多个地址用逗号分隔。
如:192.168.1.2,192.168.1.3''',trim: true),
choice(choices: ['mysql', 'oracle'], description: '''选择数据库类型''', name: 'DATABASE', trim: true),
booleanParam(defaultValue: true, description: '默认升级数据库', name: 'DATABASE'),
listGitBranches(branchFilter: '.*/feature.*||.*/dev', credentialsId: 'a2b624d4-567d-4ff2',
defaultValue: '', name: 'BRANCH', quickFilterEnabled: true, remoteURL: 'https://192.168.1.2/devops.git', selectedValue: 'NONE',
sortMode: 'DESCENDING_SMART', tagFilter: '*', type: 'PT_BRANCH') //列出git分支
])
])
多选参数
需安装Extended Choice Parameter plugin插件
参考文章:Jenkins参数化构建如何设置多选框
extendedChoice(description: '跳过指定的git工程', multiSelectDelimiter: ',', name: 'SKIP_GIT_PRODUCT', quoteValue: false, saveJSONParameterToFile: false,type: 'PT_CHECKBOX', value: 'a,b,c',visibleItemCount: 3),
定时构建
properties ([
pipelineTriggers([
cron('H/30 * * * *')
])
])
并行执行
parallel (
'master': {node('master') { //可以指定节点
sh "echo '在master执行'"
}},
'java': { //不指定就用全局node指定的节点
stage('mvn') {
sh 'mvn'
}
},
'web': {
stage('npm') {
sh 'npm'
}
}
)
sshCommand到远程主机执行命令
def mysqlRemote = getMysqlRemote(mysqlIp, mysqlSshUser, mysqlSshPassword)
sshCommand remote: mysqlRemote, failOnError: false, command: """
shell命令
"""
def getMysqlRemote(mysqlIp, mysqlSshUser, mysqlSshPassword) {
def remote = [:]
remote.name = 'mysqlServer'
remote.host = mysqlIp
remote.allowAnyHosts = true
remote.user = mysqlSshUser
remote.password = mysqlSshPassword
return remote
}
触发其它job
wait:是否等待下游job构建完成。默认为true
propagate:继承下游job的构建状态,如果下游job报错,则自己也报错
build(job: '下游job名', parameters: [string(name: 'VERSION', value: env.VERSION)], propagate: true, wait: true)