Set build number for Jenkins workflow (pipeline) builds

寵の児 提交于 2020-01-22 08:52:10

问题


I am migrating jenkins-workflow job to new template based workflow job. Because the build number is used as part of the version of build artifacts the workflow produces I have to start build number of the new workflow with a number greater than the old workflow. Unfortunately 'Next Build Number' plugin does not work with workflow pipeline.

Anybody knows a good way do this?


回答1:


Try running below script in Jenkins Script Console.. Change "workFlow" to your Jobname

def job = Jenkins.instance.getItem("workFlow")
job.nextBuildNumber = 10
job.saveNextBuildNumber()



回答2:


Or, you could add a snippet like this to your Pipeline/Workflow job DSL script (aka Jenkinsfile):

offset = 5
currentBuild.displayName = "#" + (currentBuild.number + offset)



回答3:


Unfortunately the methods listed above didn't work for me when using folders. I resorted to the following in the Jenkins script console:

job = Jenkins.getInstance().getItemByFullName("BSKAzureConversion-Jobs/master", Job.class)
job.nextBuildNumber = 92
job.save()



回答4:


A few notes:

  • Jenkins Jobs and Folders are not serializable. Need to use @NonCPS
  • Disable Groovy Sandbox, as globals like Jenkins.instance are not accessible otherwise
  • Jenkins.instance.getItem() won't work well with folders. Use Jenkins.instance.getItemByFullName against the env.JOB_NAME
  • Setting job.nextBuildNumber will take effect subsequent build, so go ahead and kick off the next one
  • Make sure you return out of your pipeline so the initiating build doesn't continue with bad build number

Code:

@NonCPS
def updateBuildNumber(build_number) {
  def job = Jenkins.instance.getItemByFullName(env.JOB_NAME, Job.class)
  job.nextBuildNumber = build_number
  job.saveNextBuildNumber()
  build env.JOB_NAME
  return true
}



回答5:


I also found another way of doing this by using 'jenkins-cli'. It works for locked down installations where access to script console is not available (Cloudbees Enterprise Jenkins)

java -jar jenkins-cli.jar   -s https://instanceURL/ \
          set-next-build-number workflow-name  33


来源:https://stackoverflow.com/questions/34951689/set-build-number-for-jenkins-workflow-pipeline-builds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!