我尝试了以下代码的各种变体,但似乎都不起作用
def runScript(command){
sh '''#!/bin/bash
file="env.txt"
while IFS='=' read -r key value
do
export ${key}="${value}"
done < "$file"
pwd
"${command}"
'''
}
command
是我要在从 env.txt
重新创建环境变量后执行的动态 shell 命令
有什么想法吗?
最佳答案
使用这个
def runScript(command){
sh '''#!/bin/bash
file="env.txt"
while IFS='=' read -r key value
do
export ${key}="${value}"
done < "$file"
pwd
''' + "${command}"
}
单引号'''
将创建多行shell脚本。 "
引号将获取命令变量的值并将其与 shell 脚本连接起来。
请注意,有两个 shell 变量 key
和 value
,因此 '''
不能替换为 """
引用文献:
Access a Groovy variable from within shell step in Jenkins pipeline
关于shell - 将 shell 变量与 Jenkinsfile 中的常规变量混合并匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53402568/