Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-43092

Jenkins stages executions hangs when creating stages dynamically

XMLWordPrintable

      i have created a groovy closure inside a jenkinsfile like:

      job {
      stage1 = sh "something"
      stage2 = sh "something2"
      }
      

      Using Jenkins global libraries i read the closure and create different stages by dynamically counting the number of times 'stage' word was used

      For each stage i use the "sh" command to run a script.

      The problem i am facing is, once a stage is created and the shell script runs inside the stage, even after the shell script exits jenkins does not move on to creating the next stage instead it hangs on the same stage. 
      job.groovy

       #!groovy def call(body) { 
      def args = [:]   
      body.resolveStrategy = Closure.DELEGATE_FIRST   
      body.delegate = args   
      body() 
      
      // Loading jenkins jenkinsLibrary   
      def lib = new utils.JenkinsLibrary() 
      node(args.label)   
      {      
          def total_stages = lib.countStages(args)     for(int iter = 0; iter<total_stages; iter++)     
          {       
            lib.prepareStages(iter,args)
           }
         }
       }

      JenkinsLibrary.groovy

      package utils;
      
      import java.nio.charset.StandardCharsets
      
      @NonCPS
      def countStages(def args)
      {
         def count = 0
         def li = stageList(args)
         count = li.size()
         return count;
      }
      
      @NonCPS
      def stageList(def args)
      {
         def list = []
         for(emp in args) {
            if (emp.toString().contains('stage'))
              {
                list.add("$emp")
              }
          }
         return list;
      }
      
      
      @NonCPS
      def prepareStages(def iter, def args)
      {
         def li = stageList(args)
         createStages(iter,li[iter])
      }
      
      @NonCPS
      def createStages(def number, def valueString)
      {
         def (fullstage, command) = valueString.split('=')
         def (key, stage_name) = fullstage.split('_')
         stage ("$stage_name")
         {
            sh ("$command")
         }
      }
      return this;
      ;
      
      

       

      I see an obvious difference in the normal workflow for pipeline jobs, which is defining stages on run time. But i am not sure how jenkins hang on once i run the shell command.

      Also to note is that if i run an echo command inside a stage, it proceeds perfectly fine.... but in case of running a shell command in the stage it simply does not exit out of the first stage

            svanoort Sam Van Oort
            fahdarshad123 Fahd Arshad
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: