Set build number for Jenkins workflow (pipeline) builds

后端 未结 5 1244
野趣味
野趣味 2021-02-05 09:57

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

5条回答
  •  野的像风
    2021-02-05 10:38

    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
    }
    

提交回复
热议问题