当我从构建页面单击“重建”时,jenkins将重建并运行新作业-具有新jenkins内部编号的新作业。
如何获取执行重建的作业的内部版本号?
我不是在谈论以前的内部版本号。
对构建10说我,我进入构建5,然后单击“重建”。我如何像在env.BUILD_NUMBER
中那样从管道内部那个构建编号(5)?
答案 0 :(得分:2)
我假设您正在使用Groovy Pipeline,并且已经知道全局变量(请参阅全局变量参考)。
currentBuild
变量具有字段rawBuild
,该字段返回hudson.model.Run对象rawBuildObject#getCauses()
或rawBuildObject#getCauses()
并返回一些Cause对象。以下脚本:
node {
stage('test advance script') {
echo "current build number: ${currentBuild.number}"
echo "previous build number: ${currentBuild.previousBuild.getNumber()}"
def causes = currentBuild.rawBuild.getCauses()
echo "causes: ${causes}"
def rebuildCause0 = currentBuild.rawBuild.getCause(com.sonyericsson.rebuild.RebuildCause)
echo "rebuildCause0: ${rebuildCause0}"
echo "rebuild up number: ${rebuildCause0.getUpstreamBuild()}"
}
}
但是正如我们在聊天中讨论的那样,Rebuilder插件以错误的方式使用CauseAction
。如果将其固定为this,则控制台输出应为:
current build number: 72
previous build number: 71
causes: [hudson.model.Cause$UserIdCause@679c1066, job/DMP/job/test-pipeline/63[hudson.model.Cause$UserIdCause@679c1066]]
rebuildCause0: job/DMP/job/test-pipeline/63[hudson.model.Cause$UserIdCause@679c1066]
rebuild up number: 63
看到以下错误时,请记住
scriptApproval
:
Scripts not permitted to use method hudson.model.Run getCauses. Administrators can decide whether to approve or reject this signature.
答案 1 :(得分:0)
假设您可以卷曲自己的 jenkins 构建:
def getOriginalRebuildNum(String jobUrl = "${JOB_URL}"){
def urlJsonPath
def buildNumber
def result
ansiColor('xterm') {
try {
buildNumber = "${BUILD_NUMBER}"
urlJsonPath = "${JOB_URL}/${buildNumber}/api/json"
while (!buildNumber.isEmpty()) {
result = buildNumber
buildNumber = sh(
script:
"""
curl -s $urlJsonPath \
| jq -r '.actions[]
| select(."_class" == "hudson.model.CauseAction") .causes[]
| select(.upstreamBuild != null).upstreamBuild'
""",
returnStdout: true).trim()
urlJsonPath = "${JOB_URL}/${buildNumber}/api/json"
}
echo "Original Build: ${JOB_URL}/${result}/"
}
catch (err) {
"Error: Could not retrieve original Build Number from ${urlJsonPath} - $err"
result = "${BUILD_NUMBER}"
}
}
return result
}