为所有阶段发送电子邮件- jenkins

woobm2wo  于 2023-08-03  发布在  Jenkins
关注(0)|答案(1)|浏览(99)

我有一个问题,我在Jenkins管道建设与三个测试。我想运行它们,以便如果第二个示例失败,将执行下一阶段。此外,我在获取所有三个阶段的执行状态信息方面遇到了问题。我想发送一封电子邮件,将返回信息,例如。

Test 1 - success
Test2 - failure
Test3- success

字符串
我有这样的东西:

pipeline {
    agent none
    parameters {
    ...  
    }
    environment {
    ...
    def Test1Status
    def Test2Status
    def Test3Status
    }
    options {
        ansiColor('xterm')
    }
    stages {
        stage('Refresh Jenkins Settings only') {
            when {
                expression {
                    return params.RefreshJenkinsSettings == true
                }
            }
            steps {
                echo "This is Update Only run to refresh parameters and steps"
                echo "System: ${SYSTEM_NAME}"
            }
        }
        stage('Run Test1') {
            agent {
                label '...'
            }
            when {
                expression {
                    return params.RefreshJenkinsSettings == false
                }
            }
            steps {
                catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                    sh 'python3 scripts/Test1.py  -version "${Version}" ' 
                }
            }
        }
        stage('Test2') {
        agent {
            label '...'
        }
        when {
            expression {
                return params.RefreshJenkinsSettings == false
            }
        }
        steps {
                catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                    sh 'python Scripts\\\\Test2.py -env "${Env}" -s "${Schema}"'
                } 
        }
    }   
        stage('Test3') {
            agent {
                label '...'
            }
            when {
                expression {
                    return params.RefreshJenkinsSettings == false
                }
            }
            steps {
        bat 'D:\\Pentaho\\Kitchen.bat /rep:git1 /dir:TestCase/Test3 /job:Job "/param:ENV=%Env%" ...'
            }
        }
    }
    
post {
        always {
            script {
                def emailBody = "Test Email\n\n"

                // Check the status of each stage and update the email body accordingly
                if (Test1Status == 'SUCCESS') {
                    emailBody += "Test1: SUCCESS\n"
                } else {
                    emailBody += "Test1: FAILED\n"
                }

                if (Test2Status == 'SUCCESS') {
                    emailBody += "Test2: SUCCESS\n"
                } else {
                    emailBody += "Test2: FAILED\n"
                }

                if (Test3Status == 'SUCCESS') {
                    emailBody += "Test3: SUCCESS\n"
                } else {
                    emailBody += "Test3: FAILED\n"
                }

                // Send the email with the updated content
                emailext to: "xxx@yyy.com",
                    subject: "Test Email",
                    body: emailBody,
                    mimeType: 'text/html'
            }
        }
    }
}

wz3gfoph

wz3gfoph1#

你可以这样做:

import groovy.transform.Field

@Field // make variable global scope
Map FAILED_STAGES = [:]

def runTest(String stageName, Closure body) {
  catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
    try {
      body() // execute body i.e sh 'smth'
      FAILED_STAGES["${stageName}"] = "SUCCESS"
    } catch (e) {
      FAILED_STAGES["${stageName}"] = "FAILED"
      throw e // propagate error
    }

  }
}

pipeline {
  agent none
  parameters {}
  environment {}

  options {
    ansiColor('xterm')
  }
  stages {
    stage('Refresh Jenkins Settings only') {
      when {
        expression {
          return params.RefreshJenkinsSettings == true
        }
      }
      steps {
        echo "This is Update Only run to refresh parameters and steps"
        echo "System: ${SYSTEM_NAME}"
      }
    }
    stage('Run Test1') {
      agent {
        label '...'
      }
      when {
        expression {
          return params.RefreshJenkinsSettings == false
        }
      }
      steps {
        runTest(env.STAGE_NAME) {
          sh 'python3 scripts/Test1.py  -version "${Version}" '
        }
      }
    }
    stage('Test2') {
    agent {
      label '...'
    }
    when {
      expression {
        return params.RefreshJenkinsSettings == false
      }
    }
    steps {
      runTest(env.STAGE_NAME) {
        sh 'python Scripts\\\\Test2.py -env "${Env}" -s "${Schema}"'
      }
    }
    stage('Test3') {
      agent {
        label '...'
      }
      when {
        expression {
          return params.RefreshJenkinsSettings == false
        }
      }
      steps {
        script {
          runTest(env.STAGE_NAME) {
            bat 'D:\\Pentaho\\Kitchen.bat /rep:git1 /dir:TestCase/Test3 /job:Job "/param:ENV=%Env%" ...'
          }
        }
      }
    }
  }
  post {
    always {
      script {
        def emailBody = "Test Email\n\n"

        FAILED_STAGES.each { String stage_name, String status ->
          emailBody += "${stage_name}: ${status}\n"
        }

        // Send the email with the updated content
        emailext to: "xxx@yyy.com",
          subject: "Test Email",
          body: emailBody,
          mimeType: 'text/html'
      }
    }
  }
}

字符串
FAILED_STAGES -存储命令名称和状态

相关问题