Keep build numbers when re-installing jenkins 98/138898/1
authorhyokeun <hyokeun.jeon@samsung.com>
Fri, 14 Jul 2017 07:18:57 +0000 (16:18 +0900)
committerhyokeun <hyokeun.jeon@samsung.com>
Fri, 14 Jul 2017 07:18:57 +0000 (16:18 +0900)
Change-Id: I3659333f59082e33770d0a363c06f045b91a0a84

groovy_init_scripts/init.groovy

index 4ac9de8..7fd625c 100644 (file)
@@ -19,6 +19,26 @@ def mx = { filepath ->
   this.metaClass.mixin script
 }
 
+// New container have their all jobs nextBuildNumber=1 if it is restarted prior to deploy.
+// Below will keep current build numbers.
+def setNextBuildNumber = {
+  jobs = Jenkins.instance.projects.collect { it.name }
+  jobs.each() { job ->
+    set_num = 0
+    new File(env['JENKINS_HOME'] + '/jobs/' + job + '/builds').eachDir() { dir ->
+      def thisdir = dir.getName().split("\\.")[0]
+      if (thisdir.isNumber()) {
+        if (thisdir.toInteger() > set_num) {
+          set_num = thisdir.toInteger()
+        }
+      }
+    }
+    item = Jenkins.instance.getItemByFullName(job)
+    item.updateNextBuildNumber(set_num + 1)
+    item.save()
+  }
+}
+
 Properties properties = new Properties()
 try {
   properties.load(new FileInputStream(env['JENKINS_HOME'] + '/.groovy_init_required'))
@@ -35,6 +55,11 @@ if (properties.CONTAINER_INIT_REQUIRED == null || properties.CONTAINER_INIT_REQU
 new File(env['JENKINS_HOME'] + '/.groovy_init_required').delete()
 
 ///////////////////////////////////////////////////////////////////
+///////////  Update proper build numbers for each job  ////////////
+///////////////////////////////////////////////////////////////////
+setNextBuildNumber()
+
+///////////////////////////////////////////////////////////////////
 ///////////////////  E-mail Notification  /////////////////////////
 ///////////////////////////////////////////////////////////////////
 if (properties.SMTP_HOST != null && properties.SMTP_HOST != '') {