跳转至

错误处理和通知

1. 利用 try/catch 块来处理可能的错误,以及使用 post 指令来发送构建通知。
pipeline {
    agent any
    stages {
        stage('Example') {
            steps {
                script {
                    try {
                        // 尝试运行可能出错的命令
                    } catch (ex) {
                        echo 'An error occurred: ' + ex.toString()
                    }
                }
            }
        }
    }
    post {
        succes {
            script{
                currentBuild.description += "\n 构建成功!"    // 全局变量.构建描述
            }
        }

        failure {
            script{
                currentBuild.description += "\n 构建失败!"
            }
        }

        aborted {
            script{
                currentBuild.description += "\n 构建取消!"
            }
        }
    }
}

参数:

  • always:
  • changed:只有当流水线或阶段完成状态与之前不一样时。
  • failure:
  • success:
  • unstable:
  • aborted:只有当流水线或阶段完成状态为 aborted 。例如手动取消