Saturday, September 28, 2019

Slack Notifications in Jenkins Pipelines.

pipeline{
    agent{
        label 'slave label name'
    }
        environment{
            SLACK_TOKEN = 'slack_credential_id'
            SLACK_TEAM_DOMAIN = 'teamdomainname'
            SLACK_CHANNEL = '#jenkins-slack-channel'
            SLACK_BASE_URL = 'https://teamdomainname.slack.com/services/hooks/jenkins-ci/'
        }
        stages{
            stage("Sending Job Start Notification via Slack"){
                steps{
                    slackSend (color: '#000000', message: "Started Pileine: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'. Details: (<${env.BUILD_URL} | here >)", baseUrl: "${env.SLACK_BASE_URL}", teamDomain: "${env.SLACK_TEAM_DOMAIN}", channel: "${env.SLACK_CHANNEL}", tokenCredentialId: "${env.SLACK_TOKEN}")
                }
            }
        }
    }
    post{
        success{
            slackSend (color: '#000000', message: "Success Pileine: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'. Details: (<${env.BUILD_URL} | here >)", baseUrl: "${env.SLACK_BASE_URL}", teamDomain: "${env.SLACK_TEAM_DOMAIN}", channel: "${env.SLACK_CHANNEL}", tokenCredentialId: "${env.SLACK_TOKEN}")
        }
        failure{
            slackSend (color: '#000000', message: "Failed Pileine: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'. Details: (<${env.BUILD_URL} | here >)", baseUrl: "${env.SLACK_BASE_URL}", teamDomain: "${env.SLACK_TEAM_DOMAIN}", channel: "${env.SLACK_CHANNEL}", tokenCredentialId: "${env.SLACK_TOKEN}")
        }
        cleanup{
            deleteDir()
        }
    }
}

No comments:

Post a Comment