在 Jenkinsfile 中混合和匹配 shell 变量与 groovy 变量

Mix and match shell variables with groovy variables in Jenkinsfile

我尝试了以下代码的各种变体,none 似乎有效

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 & 因此,''' 不能替换为 """

参考资料: