语法 | https://www.jenkins.io/doc/book/pipeline/syntax/
pipeline { agent {} triggers {} options {} stages {} post {} }
agent {} | 选择构建节点
根据分支来选择构建节点
DeepSeek / jenkins 根据分支选择构建节点
实现思路大同小异:(1)根据信息,为变量赋值,(2)在 agent {} 中,引用改数值;
agent { label "${env.BRANCH_NAME.contains('test')?'test':env.BRANCH_NAME.contains('master')?'master':'default'}" }
options {} | 控制构建的选项
禁用并行构建 | disableConcurrentBuilds()
options { disableConcurrentBuilds() }
控制构建超时 | timeout()
timeout(time: 1, unit: ‘HOURS’)
TimeUnit | https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/TimeUnit.html
丢弃旧的构建 | buildDiscarder()
How do I set discard old builds for a Multi Branch Pipeline Job?
options { buildDiscarder(logRotator(numToKeepStr: '30', artifactNumToKeepStr: '30')) }
daysToKeepStr: history is only kept up to this days.
numToKeepStr: only this number of build logs are kept.
artifactDaysToKeepStr: artifacts are only kept up to this days.
artifactNumToKeepStr: only this number of builds have their artifacts kept.
重试当前构建 | retry()
https://www.jenkins.io/doc/pipeline/steps/workflow-basic-steps/#retry-retry-the-body-up-to-n-times
options { retry(3) }
triggers {} | 控制构建触发
cron – How do I schedule jobs in Jenkins? – Stack Overflow
triggers { cron('*/5 * * * *') // every 5 minutes } // https://stackoverflow.com/questions/39207924/jenkins-how-to-get-and-use-upstream-info-in-downstream triggers { upstream(upstreamProjects: 'Jenkins-Managed-Jobs/0000150/latest', threshold: hudson.model.Result.FAILURE) }
hudson.model.Result | https://javadoc.jenkins-ci.org/hudson/model/Result.html
parameters {} | 构建参数
文档:https://www.jenkins.io/doc/book/pipeline/syntax/#parameters
选择构建参数
parameters { // A choice parameter, 'three'], description: '') }. The first value is the default. choice(name: 'serviceFolder', choices: ['F','A','B'], description: 'Service to Build') // A boolean parameter booleanParam(name: 'DEBUG_BUILD', defaultValue: true, description: '') }
post {} | 当构建结束时,开始执行某些步骤
https://www.jenkins.io/doc/book/pipeline/syntax/#post
always
pipeline { agent any stages { ... } post { always { echo 'I will always say Hello again!' } } }
cleanup
针对 post 的其他步骤,当其执行结束之后,将执行 cleanup 步骤。