使用 slackSend 的 Jenkins 管道评估步骤

Jenkins Pipeline Evaluation Step with slackSend

I'm trying to do an evaluation inside a configFileProvider block, inside a step block, inside a pipeline, Here is the full code.

.

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

The block I'm interested in is:

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

Basically I want to evaluate the success of the script based on return status and send a slack message based on that. I've tried a few different ways, like trying to set the script run as the evaluation like:

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

That Didn't work either, Any Suggestions would be greatly appreciated.

差不多吧。 (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'
            }

        }
    }

试一试,看看效果如何。