Jenkins:无论结果如何,在作业结束时执行 shell 脚本

Jenkins: Execute shell script at end of job regardless of result

我想 运行 在 Jenkins 作业结束时发出 HTTP POST 请求,无论它是失败还是提前中止。我计划使用简单的 Curl 命令来执行此操作。

我知道 Post build task plugin 可以做到这一点。 但是,我相信,它会将整个控制台保存到 RAM 以进行文本比较,这不仅我不需要,而且很可能会导致我们的 master 出现 OOM 异常。

此外,我在 UI 中没有看到任何有关 plubin 的文档来尝试仅使用 Jenkins 脚本对此进行测试。 有人有任何替代方法或方法可以在 Jenkins 文件中调用 postbuild-task 吗?

您可以使用 post 构建步骤。添加此 post 部分

pipeline {
  agent any
  stages {
    stage(""){
      steps {
        script {
    
        }
      }
    }
  }
  post { #---------------This block
    always {
      script {
        your stuff which you want execute no matter what
      }
    }
  }
 }

由于我的原始代码没有使用 jenkins 管道,因此我最终使用一个简单的 try finally 块完成了此操作

您可以在阶段后添加一个 post 块

post {
    success {
      //Success block
    }

    unstable {
      //unstable block
    }

    failure {
      //Failure block
    }

    always {
      //This block will always be executed regardless of the build outcome
      echo "Build completed with status: ${currentBuild.result}"
    }
}

标准流程示例:

pipeline {
    agent any
    stages {
        stage("1") {
            }
        stage("2") {
            }
         ....
       }
  post {
    always {
           //This block will always be executed
           }
       }
}