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

Jenkins stages executions hangs when creating stages dynamically

    Details

    • Similar Issues:

      Description

      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

        Attachments

          Activity

          Hide
          svanoort Sam Van Oort added a comment -

          Fahd Arshad  It is not supported to run pipeline steps inside NonCPS blocks – this is in the pipeline docs, but the explanation here (best practices, bullet point 3c) is probably the most concise explanation. 

          If you attempt to run steps inside a NonCPS block it is not guaranteed or even expected to work correctly, and hangs like this are merely one possibility.

          Remove the all the enclosing @NonCPS functions when dynamically defining stages and it should work (you can call out to NonCPS functions within that). 

          Show
          svanoort Sam Van Oort added a comment - Fahd Arshad   It is not supported to run pipeline steps inside NonCPS blocks – this is in the pipeline docs, but the explanation here ( best practices , bullet point 3c) is probably the most concise explanation.  If you attempt to run steps inside a NonCPS block it is not guaranteed or even expected to work correctly, and hangs like this are merely one possibility. Remove the all the enclosing @NonCPS functions when dynamically defining stages and it should work (you can call out to NonCPS functions within that). 
          Hide
          fahdarshad123 Fahd Arshad added a comment -

          Thanks Sam Van Oort I understand your point. I ll see if i can work without @NonCPS functions. Thanks for the prompt response though  appreciated. 

          Show
          fahdarshad123 Fahd Arshad added a comment - Thanks Sam Van Oort I understand your point. I ll see if i can work without @NonCPS functions. Thanks for the prompt response though  appreciated. 

            People

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

              Dates

              • Created:
                Updated:
                Resolved: