首页 > 解决方案 > Jenkinsfile sh 模块逐行读取文件

问题描述

我正在尝试建立一个 Jenkinsfile,其中一个步骤是逐行读取文本文件并将其分配给变量。但是 While 循环的输入不起作用。

代码片段:

dir(FilePath) {
     sh("""
          while read -r line; do 
              args+="--arg $line" 
          done < env
       """)

标签: while-loopjenkins-pipeline

解决方案


我会使用詹金斯的基本步骤。然后你可以将它传递给 shell 或做任何你需要的事情。

https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#readfile-read-file-from-workspace

dir(FilePath) {
    script {
         def file = readFile file:"file.txt"
         sh("do whatever ${file}")
    }
}

推荐阅读