簡體   English   中英

基於 Jenkins 管道中構建狀態的條件值

[英]Conditional value base on build status in Jenkins pipeline

所以我有這個pipeline

pipeline {

    agent {
        node {
            label 'bla bla'
            customWorkspace 'D:\\'
        }
    }

    environment {
        EMAIL = 'some_emails'
        HEADER_COLOR = 'teal'
    }

    stages {
        stage('Pull git changes') {
            steps {
                ....
                }
            }
        }     

        stage('Run tests') {
            steps {
                echo "Run tests with tag: ${TESTS_RUN_TAG}"
                }
            }
        }
    }

    post {
        always {
            robot enableCache: false, logFileName: 'log.html', outputFileName: 'output.xml', outputPath: '', reportFileName: 'report.html'
            emailext attachmentsPattern: 'log.html', subject: "${JOB_NAME} build #${BUILD_NUMBER} automation results", body: """BY_BODY""", to: "${EMAIL}" 
        }
    }
}

所以在我的帖子中,我發送了帶有HTML正文的電子郵件(此處省略了正文),我想根據${currentBuild.result}值更改一些HTML顏色。 所以這就是我所嘗試的:

if (${currentBuild.result} == "SUCCESS") {                                          
    HEADER_COLOR = 'crimson'
} else {                                   
    HEADER_COLOR = 'teal'
}

在我的HTML正文中,我使用這種方式:

bgcolor="${HEADER_COLOR}"

所以我嘗試將它放在我的后期階段,但它在WorkflowScript失敗了,我想也許我有一些語法錯誤,或者我需要把它放在其他地方(或兩者兼而有之)

有任何想法嗎 ?

我不擅長 groovy 腳本,所以我會提出以下建議:

post {
  always {
    put something here 
  }
  failure {
    mail to: team@example.com, subject: 'The Pipeline failed :('
  }
  success {
    mail to: team@example.com, subject: 'The Pipeline succeeded :)'
  }
}

問題是你沒有在腳本中包含那些if block ,所以它應該是這樣的

post {
  always {
    script {
      
      if (${currentBuild.currentResult} == "SUCCESS") {                                          
        HEADER_COLOR = 'crimson'
      } else {                                   
        HEADER_COLOR = 'teal'
      }
     
      robot enableCache: false, logFileName: 'log.html', outputFileName: 'output.xml', outputPath: '', reportFileName: 'report.html'
      emailext attachmentsPattern: 'log.html', subject: "${JOB_NAME} build #${BUILD_NUMBER} automation results", body: """BY_BODY""", to: "${EMAIL}" 
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM