Home > Mobile >  Groovy code in script block to replace general build step step()
Groovy code in script block to replace general build step step()

Time:08-27

Among the possible steps one can use in a Jenkins pipeline, there is one with the name step, subtitled General Build Step. https://www.jenkins.io/doc/pipeline/steps/workflow-basic-steps/#step-general-build-step . I need to iterate on calling this step based on the contents of a file. I have created a groovy script to read the file and perform the iteration, but I am not sure how to create the equivalent of my step() in the groovy script. Here is the general format of the step I am trying to perform:

stage ('title') {
  steps {
    step([
       $class: 'UCDeployPublisher',
       siteName: 'literal string',
       deploy: [
          $class: 'com.urbancode.jenkins.plugins.ucdeploy.DeployHelper$DeployBlock',
          param1: 'another literal string',
          param2: 'yet another string'
       ]
    ])
  }
}

The script step I have developed looks like this:

steps {
  script {
     def content = readFile(file:'data.csv', encoding:'UTF-8');
     def lines = content.split('\n');
     for (line in lines) {
        // want to insert equivalent groovy code for the basic build step here
     }
  }
}

I'm expecting there is probably a trivial answer here. I'm just out of my element in the groovy/java world and I am not sure how to proceed. I have done extensive research, looked at source code for Jenkins, looked at plugins, etc. I am stuck!

CodePudding user response:

Check the following, simply move your UCDeployPublisher to a new function and call that from your loop.

steps {
  script {
     def content = readFile(file:'data.csv', encoding:'UTF-8');
     def lines = content.split('\n');
     for (line in lines) {
         runUCD(line)
     }
  }
}

// Groovy function 
def runUCD(def n) {
    stage ("title $n") {
      steps {
        step([
           $class: 'UCDeployPublisher',
           siteName: 'literal string',
           deploy: [
              $class: 'com.urbancode.jenkins.plugins.ucdeploy.DeployHelper$DeployBlock',
              param1: 'another literal string',
              param2: 'yet another string'
           ]
        ])
      }
    }
}

CodePudding user response:

This is showing the code related to my comment on the accepted answer

pipeline {
   stages {
      stage ('loop') {
         steps {
              script {
                  ... groovy to read/parse file and call runUCD
               }
         }
      }
   }
}
 
def runUCD(def param1, def param2) {
    stage ("title $param1") {
       step([
            ....
        ])
    }
}
  • Related