【问题标题】:unable to understand what `sh !` does inside a Jenkins pipeline code无法理解 `sh !` 在 Jenkins 管道代码中的作用
【发布时间】:2020-08-31 08:25:01
【问题描述】:

我有一个 Jenkins 流水线代码

stages {
    stage('Monitoring logs') {
       steps {
          script {
              sh '! grep "wow" output.log
                 }
              }
          }
      }

谁能解释这部分代码的作用->sh '! grep "wow" output.log' 以及如果在output.log 中找到“wow”字符串,它将对执行或管道执行状态产生什么影响?

我知道grep "wow" output.log 会在output.log 中搜索“wow”字符串,但我不知道sh '! grep "wow" output.log' 会做什么?

【问题讨论】:

  • 我不是 Jenkins 的专家,但通常否定选项 ! 用于省略/否定输出。所以这里的意思是,打印grep命令的相反状态,所以如果grep找到结果wow,它将是0,但是在否定它之后它将变成1这里(因为命令的成功状态是0).
  • sh 是 shell 的可执行文件,通常可以在 /bin 中找到。 '! grep "wow" output.log" 是要在 shell 中运行的命令。
  • @AnsFourtyTwo 在这种情况下是sh is a Groovy function
  • 如果在output.log中发现字符串“wow”,sh '! grep "wow" output.log'对管道的执行或状态有什么影响? @AnsFourtyTwo @Biffen @RavinderSingh13

标签: shell unix jenkins jenkins-declarative-pipeline


【解决方案1】:
  1. 如果output.log 包含wowgrep "wow" output.log 成功(退出 0),否则失败(退出非 0)。

  2. ! negates the exit code(0 变为 1;非 0 变为 0)。

  3. Jenkins’ sh function 运行 shell 脚本并在非 0 退出代码上引发异常,这将使管道失败。*

如果output.log 包含wow,那么管道将失败。

* 通过使用-e 选项。

【讨论】:

    猜你喜欢
    • 2018-08-10
    • 1970-01-01
    • 2019-03-30
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    • 2018-07-24
    • 2018-04-16
    • 1970-01-01
    相关资源
    最近更新 更多