Search code examples
jenkinsgroovyjenkins-pipelinejenkins-plugins

Jenkins active choice React reference parameter Groovy script to get array content from file


Parameter Code Block

I'm trying to display list for rollback parameter in Jenkins from a file (where file has roll back numbers in each line) I'm reading that content and storing it as array and returned this array in the code block but parameter has no drop-down values.

I'm not getting any error - Not sure what am I doing wrong here.

if(ENV == "INT") {
def filePath = "C:\\\\Users\\\\Lenovo\\\\OneDrive\\\\Documents\\\\Jenkins-Test\\\\test.txt"
                def fileContent = readFile file: filePath
                def modifiedList = fileContent.readLines()
               def values = modifiedList.collect { "\"${it}\"" }.join(',')
                return values
}
else if(ENV == "VAL") {
return ["Test", "Hello"]
}
else {
return ["1", "2"]
}

ENV is the previous parameter I'm using it as referenced parameter here.

Param UI

Can anyone please suggest solution for this !

I'm expecting below:

Test.txt (File content is not static and each value will be in new line) has file content:

R1v112
R1v122
R1v123

I want to display if ENV selected is INT then rollback drop-down should display values R1v112, R1v122, R1v123 for selection.


Solution

  • You are not writing a scripted pipeline here, it's a pure Groovy script, so Jenkins steps like readFile are not defined. Error reporting for Active Choices sucks, you have to resort to hacks like this to see what actually failed

    try {
    ... your code
    } catch(e) {
        return [e.toString()]
    }
    

    In your case the Groovy code could be

    if(ENV == "INT") {
        return new File("C:\\\\Users\\\\Lenovo\\\\OneDrive\\\\Documents\\\\Jenkins-Test\\\\test.txt").readLines()
    }