2

I want to wrap steps and post in a function.

This works fine:

pipeline {
    agent any
    stages {
        stage('Test') {
            steps {
                whateverFunction()
            }
            post {
                sh 'echo test'
            }
        }
    }
}

void whateverFunction() {
    sh 'ls /'
}

But as soon as I pack steps and post in my function it does not work. (Fail's with the error: steps in a stage must be in a ‘steps’ block.)

pipeline {
    agent any
    stages {
        stage('Test') {
            whateverFunction()
        }
    }
}

void whateverFunction() {
    steps {
        sh 'echo test'  
    }
    post {
        sh 'echo test'
    }
}

What I also tried is to have steps and then call my function in that step with a steps inside. Basically warping steps in steps which leads to the behavior that no step is executed. (But apparently it would be a valid Jenkins-file)

Is it possible to have a function which contains steps and post inside a stage. Or is there a way to achieve a similar functionality?

Sir l33tname
  • 4,026
  • 6
  • 38
  • 49
  • Duplicate of https://stackoverflow.com/questions/42837066/can-i-create-dynamically-stages-in-a-jenkins-pipeline – Markus Jan 09 '19 at 19:13

1 Answers1

0

Seems to be that you cannot create similar functionality with post inside a method in declarative pipeline. To achieve this you can try to use scripted pipelines.

For declarative pipelines you can use post section only inside stage (example) or after stages block (example).

biruk1230
  • 3,042
  • 4
  • 16
  • 29