Can I use a Closure to define a stage in a Jenkins Declarative Pipeline?

后端 未结 2 1916
死守一世寂寞
死守一世寂寞 2020-12-10 08:11

I\'m trying to do something like this:

def makeStage = {
  stage(\'a\') {
    steps {
      echo \'Hello World\'
    }
  }
} 
pipeline {
  agent none
  stage         


        
2条回答
  •  忘掉有多难
    2020-12-10 08:49

    Super late, but in case anyone runs into this issue, a possible solution would be to wrap your generated stage around a script declarative and invoke .call on the generated stage.

    So for you:

    def makeStage = {
      return {
        stage('a') {
          echo 'Hello World'
        }
      }
    }
    
    pipeline {
      agent none
      stages {
        stage ('hello world') {
          steps {
            script {
              makeStage().call()
            }      
          }
        }
      }
    }

    Whoops. edited, I had "steps" inside on my stage('a') in the makeStage declaration. "steps" is a declarative pipeline directive so it was throwing an error inside the script block.

提交回复
热议问题