使用slackSend的Jenkins管道评估步骤

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用slackSend的Jenkins管道评估步骤相关的知识,希望对你有一定的参考价值。

我正在尝试在一个步骤块内,在一个管道内的configFileProvider块内进行评估,这是完整的代码。

.

pipeline {
agent any
environment {
    GIT_CREDS = credentials('GIT')
    }

stages {
    stage ('Load Repos') {
    steps {
        cleanWs notFailBuild: true
        sh """
        env
        git clone "https://$GIT_CREDS@$GIT_URL
        git clone "https://$GIT_CREDS@$CONTENT_URL_SHORT"
        """
    }
    }

    stage ('Run Content Build Script') {
    steps {
        slackSend channel: 'notif', message: "Started Content Build and Validate for ${env.CONTENT_GIT_REPO} for Repository ${env.CONTENT_GIT_URL} by ${env.JOB_NAME} (<${env.BUILD_URL}|Open>)", tokenCredentialId: '<Token>'
        configFileProvider(
        [configFile(fileId: '605dae22-d459-4c9a-8d3c-f88d305195aa', variable: 'script')]) {
            sh(returnStatus: true, script: """python3.6 $script""")
            if (returnStatus != 0) {
                currentBuild.result = 'FAILED'
                slackSend channel: SLACK_CHANNEL, message: "Content Build and Validate FAILED for ${env.CONTENT_GIT_REPO} for Repository ${env.CONTENT_GIT_URL} by ${env.JOB_NAME} (<${env.BUILD_URL}|Open>)", color: 'warning', tokenCredentialId: 'slack-token'
            }
            else {
                slackSend channel: SLACK_CHANNEL, message: "Content Build and Validate Succeded for ${env.CONTENT_GIT_REPO} for Repository ${env.CONTENT_GIT_URL} by ${env.JOB_NAME} (<${env.BUILD_URL}|Open>)", color: 'success', tokenCredentialId: 'slack-token'
            }
        }
    }
    }

我感兴趣的块是:

configFileProvider(
        [configFile(fileId: '605dae22-d459-4c9a-8d3c-f88d305195aa', variable: 'script')]) {
            sh(returnStatus: true, script: """python3.6 $script""")
            if (returnStatus != 0) {
                currentBuild.result = 'FAILED'
                slackSend channel: SLACK_CHANNEL, message: "Content Build and Validate FAILED for ${env.CONTENT_GIT_REPO} for Repository ${env.CONTENT_GIT_URL} by ${env.JOB_NAME} (<${env.BUILD_URL}|Open>)", color: 'warning', tokenCredentialId: 'slack-token'
            }
            else {
                slackSend channel: SLACK_CHANNEL, message: "Content Build and Validate Succeded for ${env.CONTENT_GIT_REPO} for Repository ${env.CONTENT_GIT_URL} by ${env.JOB_NAME} (<${env.BUILD_URL}|Open>)", color: 'success', tokenCredentialId: 'slack-token'
            }
        }

基本上我想根据返回状态评估脚本的成功,并根据它发送松弛消息。我尝试了几种不同的方法,比如尝试将脚本设置为评估,例如:

if(python3.6“$ script){......

这也不起作用,任何建议将不胜感激。

答案

几乎是正确的。 (1)你需要将if逻辑放在脚本步骤中。 (2)你需要保存sh步的结果并在if中使用它。

configFileProvider(
    [configFile(fileId: '605dae22-d459-4c9a-8d3c-f88d305195aa', variable: 'script')]) {
        script {
            def int buildStatus = sh(returnStatus: true, script: """python3.6 $script""")
            if (buildStatus != 0) {
                currentBuild.result = 'FAILED'
                slackSend channel: SLACK_CHANNEL, message: "Content Build and Validate FAILED for ${env.CONTENT_GIT_REPO} for Repository ${env.CONTENT_GIT_URL} by ${env.JOB_NAME} (<${env.BUILD_URL}|Open>)", color: 'warning', tokenCredentialId: 'slack-token'
            }
            else {
                slackSend channel: SLACK_CHANNEL, message: "Content Build and Validate Succeded for ${env.CONTENT_GIT_REPO} for Repository ${env.CONTENT_GIT_URL} by ${env.JOB_NAME} (<${env.BUILD_URL}|Open>)", color: 'success', tokenCredentialId: 'slack-token'
            }

        }
    }

试试这个,看看它是如何工作的。

以上是关于使用slackSend的Jenkins管道评估步骤的主要内容,如果未能解决你的问题,请参考以下文章

Jenkins groovy 管道 - 为另一个管道调用构建步骤

Jenkins声明性管道:如何从输入步骤中读取选择?

如何在詹金斯中获取管道作业的 URL

将Jenkins布尔管道迁移到Teamcity构建链的问题

Jenkins管道最佳实践Top 10

Jenkins 管道无法使用凭据推送到 gerrit