自定义Jenkins电子邮件

jgwigjjp  于 2023-03-17  发布在  Jenkins
关注(0)|答案(1)|浏览(288)

我正在尝试更改环境变量MSG_INFO的值并将其传递到post:failstage

#!/usr/bin/env groovy
def msg_info = ''
pipeline {
  agent {
        
    }
    
    environment
    {
        MSG_INFO = ""
    }
    
    stages{
            stage('Fetch_sonar_analysis') {
                    steps {
                        container('tools') {
                             
                            sh '''
                            ls -l
                            csv_file_path=$(find . -name *.csv)
                            if [ -z "$csv_file_path" ]; 
                            then
                            env.MSG_INFO=$(echo "${file} doesn't exist")
                                exit 1
                            fi
                            '''
                    }
                }
            }
    }
    post {
        success {
                    emailext attachmentsPattern: '**/*.csv' , body: "Hi,\nPASS : ${env.BUILD_URL}.\nPlease find the csv attached to this email.\nThank you.", 
                    subject: "Sonar_Report_csv :: Pipeline Build JOB :  ${env.BUILD_NUMBER}-- Status: SUCCESS", 
                    mimeType: 'text/plain',to: "mail.com"
                    
                }
        failure {
                    mail to: 'mail.com',
                    subject: "Sonar_Report_csv :: Pipeline Build JOB :  ${env.BUILD_NUMBER}-- Status: FAILED",
                    body: "Hi,\n ${env.MSG_INFO} \nSomething is wrong Here : ${env.BUILD_URL}.\nThank you."
                }

        }
}

我想自定义电子邮件消息,但当我打印env.MSG_INFO时,打印失败,而不是“${file}不存在”

vtwuwzda

vtwuwzda1#

你不能在Shell块中设置环境变量。这里有一个方法可以做到这一点。

def msg_info = ''
pipeline {
  agent {
        
    }
    stages{
            stage('Fetch_sonar_analysis') {
                    steps {
                        container('tools') {
                            try {
                              sh '''
                              ls -l
                              csv_file_path=$(find . -name *.csv)
                              if [ -z "$csv_file_path" ]; 
                              then
                              env.MSG_INFO=$(echo "${file} doesn't exist")
                                  exit 1
                              fi
                              '''  
                            } catch(e) {
                              msg_info = "${file} doesn't exist"
                              error "Error occured while reading files"
                            }
                            
                    }
                }
            }
    }
    post {
        success {
                    emailext attachmentsPattern: '**/*.csv' , body: "Hi,\nPASS : ${env.BUILD_URL}.\nPlease find the csv attached to this email.\nThank you.", 
                    subject: "Sonar_Report_csv :: Pipeline Build JOB :  ${env.BUILD_NUMBER}-- Status: SUCCESS", 
                    mimeType: 'text/plain',to: "mail.com"
                    
                }
        failure {
                    mail to: 'mail.com',
                    subject: "Sonar_Report_csv :: Pipeline Build JOB :  ${env.BUILD_NUMBER}-- Status: FAILED",
                    body: "Hi,\n ${msg_info} \nSomething is wrong Here : ${env.BUILD_URL}.\nThank you."
                }

        }
}

或者,您可以将消息写入文件,然后在故障后处理步骤中读取该消息。

相关问题