简体   繁体   中英

How to read the txt file line by line and assign a variable to each line in jenkinsfile

**I am passing the file as mytextfile.txt

test

yatin

deep

shubham

ankit

rohan

sachin

The pipeline fails with below error

java.lang.IndexOutOfBoundsException: Index: 11, Size: 11

at java.util.ArrayList.rangeCheck(ArrayList.java:657)

at java.util.ArrayList.get(ArrayList.java:433)

at sun.reflect.GeneratedMethodAccessor1401.invoke(Unknown Source)**

  #!groovy

    pipeline {
      libraries {
         lib("library@master")
         }
        options {
          timeout(time: 1, unit: 'HOURS')
            } 
          agent any 
        stages {
           stage('testing') {
            steps {
           script {
          checkout scm
          env.changefile = readTrusted('mytextfile.txt')
          lines = env.changefile.readLines()

          Detail = lines.get(1)
          Name = lines.get(3)
          Notes = lines.get(5)
          State = lines.get(7)
          Class = lines.get(9)
          Extras = lines.get(11)
          echo "detail used for this deployment is: ${Detail}"
          echo "name for this deployment is: ${Name}"
          echo "notes are: ${Notes}"
          echo "Class is: ${Class}"
          echo "extras is: ${Extras}"
          echo "State is: ${State}"

             }
           } 
         }
       }
     }

arrays in java/groovy have zero-based indexes.

so, if you have 11 elements in lines array

then to get the first one use lines.get(0) or lines[0]

to get the last one use lines.get(10) or lines[10] or line[-1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM