Home > Software engineering >  groovy current scope error in jenkins pipeline
groovy current scope error in jenkins pipeline

Time:11-13

i neeed your help please

i'm working on groovy script to list all scm polling jobs. the script is working fine on jenkins scripting console but when i integrate it in jenkinsfile and run it in pipeline i get this error :

12:51:21  WorkflowScript: 10: The current scope already contains a variable of the name it
12:51:21   @ line 10, column 25.
12:51:21               def logSpec = { it, getTrigger -> String spec = getTrigger(it)?.getSpec();  if (spec ) println ("job_name  "   it.name   "   job_path  "     it.getFullName()   " with spec "   spec )}
12:51:21                             ^
12:51:21  
12:51:21  1 error
12:51:21  
12:51:21    at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
12:51:21    at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:958)

Here is the jenkinsfile :

#!/usr/bin/env groovy
import hudson.triggers.*
import hudson.maven.MavenModuleSet
import org.jenkinsci.plugins.workflow.job.*
pipeline {
  agent any  
  stages {
    stage('list jobs with scm polling') {
      steps {
          def logSpec = { it, getTrigger -> String spec = getTrigger(it)?.getSpec();  if (spec ) println ("job_name  "   it.name   "   job_path  "     it.getFullName()   " with spec "   spec )}
 
          println("--- SCM Frequent Polling for Pipeline jobs ---")
          Jenkins.getInstance().getAllItems(WorkflowJob.class).each() { logSpec(it, {it.getSCMTrigger()}) }
           
          println("\n--- SCM Frequent Polling for FreeStyle jobs ---")
          Jenkins.getInstance().getAllItems(FreeStyleProject.class).each() { logSpec(it, {it.getSCMTrigger()}) }
           
          println("\n--- SCM Frequent Polling for Maven jobs ---");
          Jenkins.getInstance().getAllItems(MavenModuleSet.class).each() { logSpec(it, {it.getTrigger(SCMTrigger.class)}) }
           
          println("--- SCM Frequent Polling for Abstract jobs---")
          Jenkins.getInstance().getAllItems(AbstractProject.class).each() { logSpec(it, {it.getTrigger(SCMTrigger.class)}) }
          
          
          println '\nDone.'

 }} }}

Does anyone can help ? thanksss

CodePudding user response:

it is an implicit variable that is provided in closures, when the closure doesn't have an explicitly declared parameter. So when you declare a parameter, make sure it is not called it to avoid conflicts with parent scopes that already define it (in your case the closure of .each()).

Also, to integrate a script section in a pipeline, either use the script step or define a function that you could call like a built-in step.

Lastly, .each() doesn't work well in pipeline code, due to the restrictions imposed by the CPS transformations applied by Jenkins to the pipeline code (unless tagged @NonCPS - which has other restrictions). So .each() should be replaced by a for loop.

pipeline {
  agent any  
  stages {
    stage('list jobs with scm polling') {
      steps {
          script {
              def logSpec = { job, getTrigger -> String spec = getTrigger(job)?.getSpec();  if (spec ) println ("job_name  "   job.name   "   job_path  "     job.getFullName()   " with spec "   spec )}
     
              println("--- SCM Frequent Polling for Pipeline jobs ---")
              for( item in Jenkins.getInstance().getAllItems(WorkflowJob.class) ) { 
                 logSpec( item, {item.getSCMTrigger()}) 
              }
               
              // ... other code ...
               
              
              println '\nDone.'
          }
 }} }}

Variant with separate function:

pipeline {
  agent any  
  stages {
    stage('list jobs with scm polling') {
      steps {
         doStuff()
 }} }}

void doStuff() {
    def logSpec = { job, getTrigger -> String spec = getTrigger(job)?.getSpec();  if (spec ) println ("job_name  "   job.name   "   job_path  "     job.getFullName()   " with spec "   spec )}

    println("--- SCM Frequent Polling for Pipeline jobs ---")
    for( item in Jenkins.getInstance().getAllItems(WorkflowJob.class) ) { 
       logSpec( item, {item.getSCMTrigger()}) 
    }

    // ... other code ...


    println '\nDone.'
}
  • Related