如何在jenkins脚本管道中将echo输出写入文本文件

zazmityj  于 2023-08-03  发布在  Jenkins
关注(0)|答案(2)|浏览(165)

我试图写回输出到一个文本文件内的命令本身。我运行这个脚本在Jenkins内置控制器本身是windows机器。这是我的代码
管道{代理任意

stages {
    stage('Hello') {
        steps {
            echo 'Hello World'
        }
    }
}

字符串
}
在这段代码中,我试图从同一个命令将echo输出写入一个文本文件。例如,echo 'Hello World' > Helloworld.txt
这可能吗?

kupeojn6

kupeojn61#

你可以使用bat(sh - linux/bat - windows)命令

pipeline {
  agent any

  stages {
    stage('Hello') {
      steps {
        // Run the 'echo' command using the 'bat' step and redirect the output to the file
        bat 'echo Hello World > Helloworld.txt'
      }
    }
  }
}

字符串
但更好的方法是writeFile链接

slhcrj9b

slhcrj9b2#

我不知道你能不能只执行一个命令。您可能必须使用一个脚本和输出,并使用另一个脚本和输出进行编写。
使用[writeFile][1]步骤。这是管道插件的一个基本步骤。

script {
// first set a var
text = "Hello world"
// print it
echo '$text'
// write the file
writeFile(
    'file': 'Helloworld.txt',
    'text': '$text'
)
}

字符串

相关问题