简体   繁体   English

如何逐行读取txt文件并为jenkinsfile中的每一行分配一个变量

[英]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 **我将文件作为 mytextfile.txt 传递

test测试

yatin亚丁

deep深的

shubham舒巴姆

ankit工具箱

rohan罗汉

sachin萨钦

The pipeline fails with below error管道失败并出现以下错误

java.lang.IndexOutOfBoundsException: Index: 11, Size: 11 java.lang.IndexOutOfBoundsException:索引:11,大小:11

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

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

at sun.reflect.GeneratedMethodAccessor1401.invoke(Unknown Source)**在 sun.reflect.GeneratedMethodAccessor1401.invoke(未知来源)**

  #!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. java/groovy 中的 arrays 具有从零开始的索引。

so, if you have 11 elements in lines array所以,如果lines数组中有11元素

then to get the first one use lines.get(0) or lines[0]然后获取第一个使用lines.get(0)lines[0]

to get the last one use lines.get(10) or lines[10] or line[-1]获取最后一个使用lines.get(10)lines[10]line[-1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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