如何在Jenkins管道构建失败后发送Slack通知?

kiv*_*ant 31 groovy jenkins slack jenkins-pipeline

我在Jenkins v2.19中有一个管道groovy脚本.我还有一个
"Slack Notification Plugin"v2.0.1和"Groovy Postbuild Plugin".

我已成功发送消息"build started"和"build finished"(如果有的话).

当一些构建步骤失败时 - 我如何向Slack通道发送消息"Build failed"?

Fah*_*ign 35

你可以做这样的事情并使用try catch块.

以下是一些示例代码:

node {
    try {
        notifyBuild('STARTED')

        stage('Prepare code') {
            echo 'do checkout stuff'
        }

        stage('Testing') {
            echo 'Testing'
            echo 'Testing - publish coverage results'
        }

        stage('Staging') {
            echo 'Deploy Stage'
        }

        stage('Deploy') {
            echo 'Deploy - Backend'
            echo 'Deploy - Frontend'
        }

  } catch (e) {
    // If there was an exception thrown, the build failed
    currentBuild.result = "FAILED"
    throw e
  } finally {
    // Success or failure, always send notifications
    notifyBuild(currentBuild.result)
  }
}

def notifyBuild(String buildStatus = 'STARTED') {
  // build status of null means successful
  buildStatus =  buildStatus ?: 'SUCCESSFUL'

  // Default values
  def colorName = 'RED'
  def colorCode = '#FF0000'
  def subject = "${buildStatus}: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'"
  def summary = "${subject} (${env.BUILD_URL})"

  // Override default values based on build status
  if (buildStatus == 'STARTED') {
    color = 'YELLOW'
    colorCode = '#FFFF00'
  } else if (buildStatus == 'SUCCESSFUL') {
    color = 'GREEN'
    colorCode = '#00FF00'
  } else {
    color = 'RED'
    colorCode = '#FF0000'
  }

  // Send notifications
  slackSend (color: colorCode, message: summary)
}
Run Code Online (Sandbox Code Playgroud)

完整的片段可以在这里找到Jenkinsfile模板

  • 构建状态应该称为“SUCCESS”,而不是“SUCCESSFUL”(至少在最近的 Jenkins 中,当放入always/post 块时)。 (2认同)

bea*_*u13 26

根据Liam Newman的博客文章,仅在脚本管道中查看这个已清理的Slack片段(声明性管道用户向下滚动).它使用原始的Jenkins结果,消息格式,更好的颜色(基于EclEmma),以及一些Groovy功能,如默认参数:

def notifySlack(String buildStatus = 'STARTED') {
    // Build status of null means success.
    buildStatus = buildStatus ?: 'SUCCESS'

    def color

    if (buildStatus == 'STARTED') {
        color = '#D4DADF'
    } else if (buildStatus == 'SUCCESS') {
        color = '#BDFFC3'
    } else if (buildStatus == 'UNSTABLE') {
        color = '#FFFE89'
    } else {
        color = '#FF9FA1'
    }

    def msg = "${buildStatus}: `${env.JOB_NAME}` #${env.BUILD_NUMBER}:\n${env.BUILD_URL}"

    slackSend(color: color, message: msg)
}

node {
    try {
        notifySlack()

        // Existing build steps.
    } catch (e) {
        currentBuild.result = 'FAILURE'
        throw e
    } finally {
        notifySlack(currentBuild.result)
    }
}
Run Code Online (Sandbox Code Playgroud)

输出将是这样的(在这里使用不同的格式样式):

在此输入图像描述

也许env.JOB_NAME包含%2F可以修复的编码斜杠()replaceAll("%2F", "/").看看这个要点,看看如何通知HipChat.

如果您有声明性管道,请查看关于"清理和通知"的Jenkins文档或Liam Newman的后续帖子"声明性管道:通知和共享库".


Rak*_*akk 16

以防万一在声明式语法中,

现在,詹金斯提供post.您可以在管道末尾检查结果.

https://jenkins.io/doc/book/pipeline/syntax/#post-example

使用像:

pipeline {
    stages { ... }
    post {
       // only triggered when blue or green sign
       success {
           slackSend ...
       }
       // triggered when red sign
       failure {
           slackSend ...
       }
       // trigger every-works
       always {
           slackSend ...
       }
    }
}
Run Code Online (Sandbox Code Playgroud)

它也将用于每一个stage.请参阅文档链接.

  • 值得注意的是,OP使用Groovy(AKA脚本)管道,而`post`只能用于声明性管道.你不能混合两者. (2认同)