Timeout the build if it takes too long and trigger release if it builds

Change-Id: I82e8c18363306f899e876c36cb5affafe256645e
diff --git a/Jenkinsfile b/Jenkinsfile
index 29887de..2ad2820 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -1,101 +1,114 @@
-node ('build') {
-   stage 'Checkout cord repo'
-   checkout changelog: false, poll: false, scm: [$class: 'RepoScm', currentBranch: true, manifestRepositoryUrl: 'https://gerrit.opencord.org/manifest', quiet: true]
-   dir('build') {
-        stage 'Redeploy head node and Build Vagrant box'
-        try {
-            parallel(
-                maasOps: {
-                    sh "maas login maas http://10.90.0.2/MAAS/api/2.0 ${apiKey}"
-                    sh "maas maas machine release ${systemId}"
+def filename = 'manifest-' + env.BRANCH_NAME + '.xml'
 
-                    timeout(time: 15) {
-                        waitUntil {
-                           try {
-                                sh "maas maas machine read ${systemId} | grep Ready"
-                                return true
-                            } catch (exception) {
-                                return false
+timeout (time: 240) {
+    node ('build') {
+       stage 'Checkout cord repo'
+       checkout changelog: false, poll: false, scm: [$class: 'RepoScm', manifestBranch: env.BRANCH_NAME, manifestRepositoryUrl: 'https://gerrit.opencord.org/manifest', quiet: true]
+
+       stage 'Generate and Copy Manifest file'
+       sh returnStdout: true, script: 'repo manifest -r -o ' + filename 
+       sh returnStdout: true, script: 'cp ' + filename + ' ' + env.JENKINS_HOME + '/tmp'
+
+       dir('build') {
+            stage 'Redeploy head node and Build Vagrant box'
+            try {
+                parallel(
+                    maasOps: {
+                        sh "maas login maas http://10.90.0.2/MAAS/api/2.0 ${apiKey}"
+                        sh "maas maas machine release ${systemId}"
+
+                        timeout(time: 15) {
+                            waitUntil {
+                               try {
+                                    sh "maas maas machine read ${systemId} | grep Ready"
+                                    return true
+                                } catch (exception) {
+                                    return false
+                                }
                             }
                         }
-                    }
 
-                    sh 'maas maas machines allocate'
-                    sh "maas maas machine deploy ${systemId}"
+                        sh 'maas maas machines allocate'
+                        sh "maas maas machine deploy ${systemId}"
 
-                    timeout(time: 30) {
-                        waitUntil {
-                           try {
-                                sh "maas maas machine read ${systemId} | grep Deployed"
-                                return true
-                            } catch (exception) {
-                                return false
+                        timeout(time: 30) {
+                            waitUntil {
+                               try {
+                                    sh "maas maas machine read ${systemId} | grep Deployed"
+                                    return true
+                                } catch (exception) {
+                                    return false
+                                }
                             }
                         }
-                    }
 
-                }, vagrantOps: {
-                    sh 'vagrant up corddev'
-                }, failFast : true
-            )
+                    }, vagrantOps: {
+                        sh 'vagrant up corddev'
+                    }, failFast : true
+                )
 
-            stage 'Fetch CORD packages'
-            sh 'vagrant ssh -c "cd /cord/build; ./gradlew fetch" corddev'
+                stage 'Fetch CORD packages'
+                sh 'vagrant ssh -c "cd /cord/build; ./gradlew fetch" corddev'
 
-            stage 'Build CORD Images'
-            sh 'vagrant ssh -c "cd /cord/build; ./gradlew buildImages" corddev'
+                stage 'Build CORD Images'
+                sh 'vagrant ssh -c "cd /cord/build; ./gradlew buildImages" corddev'
 
-            stage 'Publish to headnode'
-            sh 'vagrant ssh -c "cd /cord/build; ./gradlew -PtargetReg=10.90.0.251:5000 -PdeployConfig=config/onlab_develop_pod.yml publish" corddev'
+                stage 'Publish to headnode'
+                sh 'vagrant ssh -c "cd /cord/build; ./gradlew -PtargetReg=10.90.0.251:5000 -PdeployConfig=config/onlab_develop_pod.yml publish" corddev'
 
-            stage 'Deploy'
-            sh 'vagrant ssh -c "cd /cord/build; ./gradlew -PtargetReg=10.90.0.251:5000 -PdeployConfig=config/onlab_develop_pod.yml deploy" corddev'
+                stage 'Deploy'
+                sh 'vagrant ssh -c "cd /cord/build; ./gradlew -PtargetReg=10.90.0.251:5000 -PdeployConfig=config/onlab_develop_pod.yml deploy" corddev'
 
-            stage 'Power cycle compute nodes'
-            parallel(
-                compute_1: {
-                    sh 'ipmitool -U admin -P admin -H 10.90.0.10 power cycle'
-                }, compute_2: {
-                    sh 'ipmitool -U admin -P admin -H 10.90.0.11 power cycle'
-                }, failFast : true
-            )
+                stage 'Power cycle compute nodes'
+                parallel(
+                    compute_1: {
+                        sh 'ipmitool -U admin -P admin -H 10.90.0.10 power cycle'
+                    }, compute_2: {
+                        sh 'ipmitool -U admin -P admin -H 10.90.0.11 power cycle'
+                    }, failFast : true
+                )
 
-            stage 'Wait for compute nodes to get deployed'
-            sh 'ssh-keygen -f "/home/ubuntu/.ssh/known_hosts" -R 10.90.0.251'
-            def cordapikey = sh(returnStdout: true, script: "sshpass -p ${headnodepass} ssh -oStrictHostKeyChecking=no -l ${headnodeuser} 10.90.0.251 sudo maas-region-admin apikey --username cord")
-            sh "sshpass -p ${headnodepass} ssh -oStrictHostKeyChecking=no -l ${headnodeuser} 10.90.0.251 maas login pod-maas http://10.90.0.251/MAAS/api/1.0 $cordapikey"
-            timeout(time: 45) {
-                waitUntil {
-                    try {
-                        num = sh(returnStdout: true, script: "sshpass -p ${headnodepass} ssh -l ${headnodeuser} 10.90.0.251  maas pod-maas nodes list | grep Deployed | wc -l").trim()
-                        return num == '2'
-                    } catch (exception) {
-                        return false
+                stage 'Wait for compute nodes to get deployed'
+                sh 'ssh-keygen -f "/home/ubuntu/.ssh/known_hosts" -R 10.90.0.251'
+                def cordapikey = sh(returnStdout: true, script: "sshpass -p ${headnodepass} ssh -oStrictHostKeyChecking=no -l ${headnodeuser} 10.90.0.251 sudo maas-region-admin apikey --username cord")
+                sh "sshpass -p ${headnodepass} ssh -oStrictHostKeyChecking=no -l ${headnodeuser} 10.90.0.251 maas login pod-maas http://10.90.0.251/MAAS/api/1.0 $cordapikey"
+                timeout(time: 45) {
+                    waitUntil {
+                        try {
+                            num = sh(returnStdout: true, script: "sshpass -p ${headnodepass} ssh -l ${headnodeuser} 10.90.0.251  maas pod-maas nodes list | grep Deployed | wc -l").trim()
+                            return num == '2'
+                        } catch (exception) {
+                            return false
+                        }
                     }
                 }
-            }
 
-            stage 'Wait for computes nodes to be provisioned'
-            ip = sh (returnStdout: true, script:"sshpass -p ${headnodepass} ssh -oStrictHostKeyChecking=no -l ${headnodeuser} 10.90.0.251 docker inspect --format '{{.NetworkSettings.Networks.maas_default.IPAddress}}'  provisioner").trim()
-            timeout(time:45) {
-                waitUntil {
-                    try {
-                        out = sh (returnStdout: true, script:"sshpass -p ${headnodepass} ssh -oStrictHostKeyChecking=no -l ${headnodeuser} 10.90.0.251 curl -sS http://$ip:4243/provision/ | jq -c '.[] | select(.status | contains(2))'").trim()
-                        return out != ""
-                    } catch (exception) {
-                        return false
+                stage 'Wait for computes nodes to be provisioned'
+                ip = sh (returnStdout: true, script:"sshpass -p ${headnodepass} ssh -oStrictHostKeyChecking=no -l ${headnodeuser} 10.90.0.251 docker inspect --format '{{.NetworkSettings.Networks.maas_default.IPAddress}}'  provisioner").trim()
+                timeout(time:45) {
+                    waitUntil {
+                        try {
+                            out = sh (returnStdout: true, script:"sshpass -p ${headnodepass} ssh -oStrictHostKeyChecking=no -l ${headnodeuser} 10.90.0.251 curl -sS http://$ip:4243/provision/ | jq -c '.[] | select(.status | contains(2))'").trim()
+                            return out != ""
+                        } catch (exception) {
+                            return false
+                        }
                     }
                 }
+
+                stage 'Trigger Build'
+                url = 'https://jenkins.opencord.org/job/release-build/job/' + env.BRANCH_NAME + '/build'
+                httpRequest authentication: 'auto-release', httpMode: 'POST', url: url, validResponseCodes: '201'
+
+                currentBuild.result = 'SUCCESS'
+            } catch (err) {
+                currentBuild.result = 'FAILURE'
+                step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: 'cord-dev@opencord.org', sendToIndividuals: false])
+            } finally {
+                sh 'vagrant destroy -f corddev'
             }
+            echo "RESULT: ${currentBuild.result}"
+       }
 
-            currentBuild.result = 'SUCCESS'
-        } catch (err) {
-            currentBuild.result = 'FAILURE'
-            step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: 'cord-dev@opencord.org', sendToIndividuals: false])
-        } finally {
-            sh 'vagrant destroy -f corddev'
-        }
-        echo "RESULT: ${currentBuild.result}"
-   }
-
+    }
 }