wsn
wsn

Reputation: 49

Is it possible to pass variable from steps to script in jenkinsfile

I have one pipeline and I want to pass one Arraylist that I get from a groovy method into the script that is running in Master Jenkins.

stages {
        stage('Get Tests Parameter') {
            steps {
                code = returnList()
                script {
                    properties([
                            parameters([
                                    [$class              : 'CascadeChoiceParameter',
                                     choiceType          : 'PT_CHECKBOX',
                                     description         : 'Select a choice',
                                     defaultValue        : '',
                                     filterLength        : 1,
                                     filterable          : false,
                                     name                : 'Tests',
                                     referencedParameters: 'role',
                                     script              : [$class        : 'GroovyScript',
                                                            fallbackScript: [
                                                                    classpath: [],
                                                                    sandbox  : true,
                                                                    script   : 'return ["ERROR"]'
                                                            ],
                                                            script        : [
                                                                    classpath: [],
                                                                    sandbox  : false,
                                                                    script   : code
                                                            ]
                                     ]
                                    ]
                            ])
                    ])
                }
            }
        }
}
...

def returnList() {
    def stringList = []
    def fileContent = readFile "/var/jenkins_home/example.txt"
    for (line in fileContent.readLines()) {
        stringList.add(line.split(",")[0] + ":selected");
    }
    return stringList
}

That stages are running in a slave, so I couldn't execute that method returnList() inside the script because the script is running in Master. So I'm trying to get returnList ArrayList to a variable and use that variable in the script part. Is that possible?

Upvotes: 0

Views: 637

Answers (1)

ycr
ycr

Reputation: 14574

If you want to execute a specific step in a specific node then you can specify the agent within the stage block. So what you can do is execute the file reading logic on the master in the initial stage and then use it in consecutive stages. Check the example below.

def code

pipeline {
    agent none
    stages {
        stage('LoadParameters') {
            agent { label 'master' }
            steps {
                 scipt {
                    code = returnList()
                  }
            }
        }
        stage('Get Tests Parameter') {
            steps {
                script {
                    properties([
                            parameters([
                                    [$class              : 'CascadeChoiceParameter',
                                     choiceType          : 'PT_CHECKBOX',
                                     description         : 'Select a choice',
                                     defaultValue        : '',
                                     filterLength        : 1,
                                     filterable          : false,
                                     name                : 'Tests',
                                     referencedParameters: 'role',
                                     script              : [$class        : 'GroovyScript',
                                                            fallbackScript: [
                                                                    classpath: [],
                                                                    sandbox  : true,
                                                                    script   : 'return ["ERROR"]'
                                                            ],
                                                            script        : [
                                                                    classpath: [],
                                                                    sandbox  : false,
                                                                    script   : code
                                                            ]
                                     ]
                                    ]
                            ])
                    ])
                }
            }
        }
    }
}

def returnList() {
    def stringList = []
    def fileContent = readFile "/var/jenkins_home/example.txt"
    for (line in fileContent.readLines()) {
        stringList.add(line.split(",")[0] + ":selected");
    }
    return stringList
}

Upvotes: 1

Related Questions