发送电子邮件给所有阶段 – Jenkins

huangapple go评论69阅读模式
英文:

Sending an email for all stages - jenkins

问题

我有一个问题,我正在使用Jenkins流水线构建三个测试。我想要运行它们,以便如果第二个示例失败,将执行下一个阶段。
另外,我在获取所有三个阶段执行状态的信息方面遇到了问题。我想要发送一封电子邮件,其中将返回信息,例如:

测试1 - 成功
测试2 - 失败
测试3 - 成功

我有类似以下的内容:

流水线 {
    代理 none
    参数 {
	    ...  
    }
    环境 {
	    ...
	    def Test1Status
	    def Test2Status
	    def Test3Status
    }
    选项 {
	    ansiColor('xterm')
    }
    阶段 {
        阶段('仅刷新Jenkins设置') {
             {
                表达式 {
                    返回 params.RefreshJenkinsSettings == true
                }
            }
            步骤 {
                回声 "这只是用于刷新参数和步骤的更新运行"
                回声 "系统: ${SYSTEM_NAME}"
            }
        }
        阶段('运行测试1') {
            代理 {
                标签 '...'
            }
             {
                表达式 {
                    返回 params.RefreshJenkinsSettings == false
                }
            }
            步骤 {
                捕获错误(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                    sh 'python3 scripts/Test1.py -version "${Version}" ' 
                }
            }
        }
        阶段('测试2') {
            代理 {
                标签 '...'
            }
             {
                表达式 {
                    返回 params.RefreshJenkinsSettings == false
                }
            }
            步骤 {
                捕获错误(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                    sh 'python Scripts\\\\Test2.py -env "${Env}" -s "${Schema}"'
                } 
            }
        }	
        阶段('测试3') {
            代理 {
                标签 '...'
            }
             {
                表达式 {
                    返回 params.RefreshJenkinsSettings == false
                }
            }
            步骤 {
                bat 'D:\\Pentaho\\Kitchen.bat /rep:git1 /dir:TestCase/Test3 /job:Job "/param:ENV=%Env%" ...'
            }
        }
    }
     {
        总是 {
            脚本 {
                def emailBody = "测试邮件\n\n"

                // 检查每个阶段的状态并相应更新电子邮件正文
                if (Test1Status == 'SUCCESS') {
                    emailBody += "测试1: 成功\n"
                } else {
                    emailBody += "测试1: 失败\n"
                }

                if (Test2Status == 'SUCCESS') {
                    emailBody += "测试2: 成功\n"
                } else {
                    emailBody += "测试2: 失败\n"
                }

                if (Test3Status == 'SUCCESS') {
                    emailBody += "测试3: 成功\n"
                } else {
                    emailBody += "测试3: 失败\n"
                }

                // 发送带有更新内容的电子邮件
                emailext to: "xxx@yyy.com",
                    subject: "测试邮件",
                    body: emailBody,
                    mimeType: 'text/html'
            }
        }
    }
}

这是您提供的Jenkins流水线代码的翻译部分。如果需要进一步的帮助,请随时提出。

英文:

I have a problem, I am building in jenkins pipeline with three tests. I would like to run them so that if the second example fails, the next stage will be performed.
In addition, I have a problem with getting information about the execution status of all three stages. I would like to send one email that will return information, e.g.

Test 1 - success
Test2 - failure
Test3- success

I have something like this:

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'
}
}
}
}

答案1

得分: 0

你可以这样做:

import groovy.transform.Field

@Field // 使变量具有全局范围
Map FAILED_STAGES = [:]

def runTest(String stageName, Closure body) {
  catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
    try {
      body() // 执行体,例如 sh 'smth'
      FAILED_STAGES["${stageName}"] = "SUCCESS"
    } catch (e) {
      FAILED_STAGES["${stageName}"] = "FAILED"
      throw e // 传播错误
    }

  }
}

pipeline {
  agent none
  parameters {}
  environment {}

  options {
    ansiColor('xterm')
  }
  stages {
    stage('仅刷新 Jenkins 设置') {
      when {
        expression {
          return params.RefreshJenkinsSettings == true
        }
      }
      steps {
        echo "这是仅更新运行以刷新参数和步骤"
        echo "System: ${SYSTEM_NAME}"
      }
    }
    stage('运行测试1') {
      agent {
        label '...'
      }
      when {
        expression {
          return params.RefreshJenkinsSettings == false
        }
      }
      steps {
        runTest(env.STAGE_NAME) {
          sh 'python3 scripts/Test1.py -version "${Version}" '
        }
      }
    }
    stage('测试2') {
      agent {
        label '...'
      }
      when {
        expression {
          return params.RefreshJenkinsSettings == false
        }
      }
      steps {
        runTest(env.STAGE_NAME) {
          sh 'python Scripts\\\\Test2.py -env "${Env}" -s "${Schema}" '
        }
      }
    }
    stage('测试3') {
      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 = "测试邮件\n\n"

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

        // 以更新的内容发送电子邮件
        emailext to: "xxx@yyy.com",
          subject: "测试邮件",
          body: emailBody,
          mimeType: 'text/html'
      }
    }
  }
}

FAILED_STAGES - 存储命令名称和状态

英文:

you can do something like:

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 - stores command name and status

huangapple
  • 本文由 发表于 2023年7月28日 05:27:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76783499.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定