Jenkins,如何向文件添加可变内容

时间:2018-08-24 13:10:18

标签: jenkins-pipeline

如何在文件中添加可变内容?

sh "ssh root@${host} 'echo '$vari' > text.txt'"

这将提供空文件

没有变量,它可以工作:

sh "ssh root@${host} 'echo some text > text.txt'"

1 个答案:

答案 0 :(得分:0)

有两种方法可以将变量的内容写入文件:

pipeline {
    agent any
    environment {
        VAR = "hello world!"
    }

    stages {
        stage('Write to file'){
            steps{
                sh 'echo "${VAR}" > test.txt'
                sh "echo ${VAR} >> test.txt"
                sh 'cat test.txt'
            }
        }
    }
}

输出:

[test] Running shell script
+ echo hello world!
[Pipeline] sh
[test] Running shell script
+ echo hello world!
[Pipeline] sh
[test] Running shell script
+ cat test.txt
hello world!
hello world!