Search code examples
loopsgithubgroovyjenkins-pipelinereadfile

Jenkinsfile read file and use in a loop


I have a scripted Jenkins pipeline located in Jenkinsfile in Github repo. I need to read some data and use it for my script, for this I have this piece of code:

def mydata = [‘val1’, ‘val2’]
mydata.each() {
…
}

Now I need to place the data in the .txt file in the same Github repository and read data from that file. The format in the file is:

val1

val2

I tried this way:

def tmpval = readFile file: ‘values.txt' 
        env.Mydata = tmpval
        Mydata.each() {
        ......
        }

but it doesn’t work as expected, I received “Caused: java.io.NotSerializableException: java.util.ArrayList$Itr”


Solution

  • Resolved:

    String[] mydata = new File("${WORKSPACE}/values.txt")
    mydata.each {