在 Jenkins/Groovy 中,为什么控制台输出中没有打印 echo > file 中的重定向?

In Jenkins/Groovy, why is the redirection in echo > file not printed in the console output?

在我的 Groovy Jenkins 管道中,我有以下内容:

sh(script: """#!/bin/bash
    set -ex
    echo 'sometext' > test1.txt
    """)

但在 Jenkins 日志中,它看起来像这样:

[Pipeline] script
[Pipeline] {
[Pipeline] sh
11:43:28  + echo sometext

也就是说,它似乎完全忽略了“>”运算符。我在转义方面做错了什么吗?

管道声明

pipeline {
    agent any

    stages {
        stage('echo to file (short)') {
            steps {
                sh """
                    #!/bin/bash
                    set -ex
                    echo 'sometext' > test1.txt
                    ls -al
                    cat test1.txt
                   """
            }
        }
        stage('echo to file (less short)') {
            steps {
                sh script: """
                    #!/bin/bash
                    set -ex
                    echo 'sometext' > test1.txt
                    ls -al
                    cat test1.txt
                    """
            }
        }    
        stage('echo to file (lesser short)') {
            steps {
                sh( script: """
                    #!/bin/bash
                    set -ex
                    echo 'sometext' > test1.txt
                    ls -al
                    cat test1.txt
                    """)
            }
        }    
    }
}

控制台输出

[Pipeline] stage
[Pipeline] { (echo to file (short))
[Pipeline] sh
+ set -ex
+ echo sometext
+ ls -al
total 13
drwxr-xr-x  2 jenkins Administratoren     0 Aug 17 21:01 .
drwxr-xr-x 29 jenkins Administratoren 12288 Aug 17 21:01 ..
-rw-r--r--  1 jenkins Administratoren     9 Aug 17 21:18 test1.txt
+ cat test1.txt
sometext
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (echo to file (less short))
[Pipeline] sh
+ set -ex
+ echo sometext
+ ls -al
total 13
drwxr-xr-x  2 jenkins Administratoren     0 Aug 17 21:01 .
drwxr-xr-x 29 jenkins Administratoren 12288 Aug 17 21:01 ..
-rw-r--r--  1 jenkins Administratoren     9 Aug 17 21:18 test1.txt
+ cat test1.txt
sometext
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (echo to file (lesser short))
[Pipeline] sh
+ set -ex
+ echo sometext
+ ls -al
total 13
drwxr-xr-x  2 jenkins Administratoren     0 Aug 17 21:01 .
drwxr-xr-x 29 jenkins Administratoren 12288 Aug 17 21:01 ..
-rw-r--r--  1 jenkins Administratoren     9 Aug 17 21:18 test1.txt
+ cat test1.txt
sometext
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

我认为这就是 -x 将命令打印到输出中的方式

尝试

set -ev

而不是

set -ex

https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html

-v Print shell input lines as they are read.