Janeth Fernando
Janeth Fernando

Reputation: 104

Passing variables to sequential stages in jenkins pipeline

I have a Jenkins sequential staged pipeline which looks like below. I want to pass a map(something like def return_map = [:]) from the 1st sequential stage to the other sequential stage(1 to 2) enter image description here This is the code which i am using.

def stepsToRun = [:]

pipeline {
    agent none

    stages {
        stage ("Prepare Stages"){
            steps {
                script {
                    build_script = load '/home/ubuntu/Documents/build-image.groovy'
                    for (int i = 1; i < 5; i++) {
                        stepsToRun["Step${i}"] = prepareStage("Step${i}")
                    }   
                    parallel stepsToRun
                }
            }
        }
    }
}

def prepareStage(def name) {
    return {
        stage (name) {
            stage("1") {
               def return_map = build_script.image_build_handler(var1,var2,var3)
            }
            stage("2") {
                build_script.push_images(return_map)
            }
        }
    }
}

I want to pass the return_map to the next stage(stage2) I tried like this. But it didn't work.

Upvotes: 0

Views: 487

Answers (1)

Sourav
Sourav

Reputation: 3392

Can you try like this:

def prepareStage(def name) {
return {
    stage (name) {
        stage("1") {
           def return_var = build_script.image_build_handler(var1,var2,var3)
           env.return_var = return_var
        }
        stage("2") {
            build_script.push_images(env.return_var)
        }
    }
  }
}

Upvotes: 1

Related Questions