Search code examples
jenkinsbuildbuild-numbers

How to retain the build number while migrating a Jenkins job?


We have a Jenkins job running on a Jenkins server instance A. The current build number for this job is say 58.

We are migrating this Jenkins job to a new Jenkins server - B. However, there is a need to retain the build number - 58 from the previous server in this new Jenkins instance B.

Is this possible? If yes, how?

Thank you


Solution

  • If you only intend to keep the build number intact for job the in the new Jenkins Server, you could achieve it simply by writing a script that will populate the nextBuildNumber file in $JENKINS_HOME/jobs/<job_name>/ with the appropriate #buildnumber that you wish to have.

    Something like this (script.sh) :-

    #!bin/bash -x
    JENKINS_HOME=/var/lib/jenkins
    mkdir -p $JENKINS_HOME/jobs/<new_job> && cp $JENKINS_HOME/jobs/<old_job>/* $JENKINS_HOME/jobs/<new_job>/
    OLD_BUILD_NO=`cat $JENKINS_HOME/jobs/seed/nextBuildNumber`
    NEW_BUILD_NO=`expr $OLD_BUILD_NO - 1`
    echo $NEW_BUILD_NO > $JENKINS_HOME/jobs/<new_job>/nextBuildNumber
    chown -R jenkins:jenkins $JENKINS_HOME/jobs/temp/
    

    Now run this script as:-

    sudo bash script.sh
    

    Although it creates the required job in the same jenkins server instance, the basic idea is same ..to populate the nextBuildNumber file.