Showing posts with label Pipelines. Show all posts
Showing posts with label Pipelines. Show all posts

Friday, December 13, 2019

Send Email Notifications in Declarative Pipeline

#!/usr/bin/env groovy

pipeline{

    agent {
            label 'slave'           
        }
    }
    stages {       
        stage ('Test Email') {
            steps {
                emailext (
                        to: 'e-mail@mail.com',
                        subject: 'subject',
                        body: 'details',
                        recipientProviders: [[$class: 'RequesterRecipientProvider']]
                        )
            }
        }
    }
    post {
    failure {
        mail to: 'e-mail@mail.com',
             subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
             body: "Something is wrong with ${env.BUILD_URL}"
        }
    }
}

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()
        }
    }
}