如何发送python错误消息给Jenkins office 365 connector webbook中的团队?

rsaldnfx  于 2023-03-01  发布在  Jenkins
关注(0)|答案(1)|浏览(134)

我正尝试在teams alerts通道中发送python错误消息,例如:

File "Myfile/......../test.py". line 8 n = len(arr_)e SyntaxError: invalid syntax

我使用不同的exitcode,这取决于代码是否抛出异常或失败,因此我希望每次exitcode不为零时都能收到警报,管道使用以下代码:

def runPythonScript(){
    def command =  "Myfile/......../test.py
    def output = sh(
        script: command,
        returnStatus: true,
  
       
        )
    if (output!= 0){
        error "exit code ${output}"
   
       
    }
   
   
       }

pipeline {
    agent any

    stages {
        stage('Run test') {
            steps {
                script{
                    try{
                        def pythonOutput = runPythonScript()
               
                   
                    }
                    catch(Exception e){
                       
                        office365ConnectorSend webhookUrl: 'https://Mywebhook........',
                        message:"started ${env.JOB_NAME} ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)",
                        status: 'FAILURE',
                        color: '#00ff00'",
                        factDefinitions:[
                        [ name: "Commit Message", template: "${e}"],
                        [ name: "Pipeline Duration", template: "time example"],
                        [ name: "Current build result", template: "${currentBuild.currentResult}"]
                        ]

                        throw e
                       
                       
                    }
                   
                }
               
               
               
           
            }
           
        }
    }
}

该代码的工作原理,并发送警报到正确的通道与所需的退出代码,我只是想包括错误消息,如上所述,如果你能推荐任何相关的文档将是伟大的,到目前为止,我只找到了非常简单的例子。

col17t5w

col17t5w1#

一种选择是将错误从Python程序写入一个文件,然后在catch块中读取并添加到消息中。

相关问题