slack-plugin icon indicating copy to clipboard operation
slack-plugin copied to clipboard

Ability to have multiple Slack-Notifications as Post-Build-Actions

Open richardpringle opened this issue 7 years ago • 7 comments

Feature request:

Right now, it appears as though I can add "Slack-Notifications" once for a job, but I would love to be able to more granularly add notifications, ie have separate channels for success and failure.

Any time I see a message in the failure channel, I drop everything that I'm doing and go check it out, but I would also like to receive messages about successes.

richardpringle avatar Sep 22 '17 15:09 richardpringle

Please advise if there's a better place to put this feature request.

richardpringle avatar Sep 22 '17 15:09 richardpringle

i have 2 slack steps as post-build steps. one as regular post-build and one in flexible publish. The job config file has both however, their setting are identical and equal to whichever one is first in the sequence.

If I edit manually the job config file, will the different sets of paramters stay put?

rginga avatar Sep 26 '17 20:09 rginga

Hi @richardpringle,

You can achieve that by using Jenkins Declarative Pipeline OR Scripted Pipeline.

A job example (Jenkinsfile);

pipeline {

  agent { label 'master' }

  options {

    disableConcurrentBuilds()
    timeout(time: 10, unit: 'MINUTES')
    buildDiscarder(logRotator(numToKeepStr: '10'))

  } // options

  parameters {

    string(name: 'SLACK_CHANNEL_1',
           description: 'Default Slack channel to send messages to',
           defaultValue: '#my_channel_1')

    string(name: 'SLACK_CHANNEL_2',
           description: 'Default Slack channel to send messages to',
           defaultValue: '#my_channel_2')           

  } // parameters

  environment {

    // Slack configuration
    SLACK_COLOR_DANGER  = '#E01563'
    SLACK_COLOR_INFO    = '#6ECADC'
    SLACK_COLOR_WARNING = '#FFC300'
    SLACK_COLOR_GOOD    = '#3EB991'

  } // environment

  stages {

    stage("Check requirements") {
      steps {

        // get user that has started the build
        wrap([$class: 'BuildUser']) { script { env.USER_ID = "${BUILD_USER_ID}" } }

        // first of all, notify the team
        slackSend (color: "${env.SLACK_COLOR_INFO}",
                   channel: "${params.SLACK_CHANNEL_1}",
                   message: "*STARTED:* Job ${env.JOB_NAME} build ${env.BUILD_NUMBER} by ${env.USER_ID}\n More info at: ${env.BUILD_URL}")
        // now, check the requirements
        script {
          // binaries - host machine
          echo "Checking binaries"
          sh "ssh -V"
        } // script
      } // steps
    } // stage

    stage("Clean Workspace") {
      steps {
        echo "Cleaning-up job workspace"
        deleteDir()
      } // steps
    } // stage

  } // stages

  post {

    aborted {

      echo "Sending message to Slack"
      slackSend (color: "${env.SLACK_COLOR_WARNING}",
                 channel: "${params.SLACK_CHANNEL_2}",
                 message: "*ABORTED:* Job ${env.JOB_NAME} build ${env.BUILD_NUMBER} by ${env.USER_ID}\n More info at: ${env.BUILD_URL}")
    } // aborted

    failure {

      echo "Sending message to Slack"
      slackSend (color: "${env.SLACK_COLOR_DANGER}",
                 channel: "${params.SLACK_CHANNEL_2}",
                 message: "*FAILED:* Job ${env.JOB_NAME} build ${env.BUILD_NUMBER} by ${env.USER_ID}\n More info at: ${env.BUILD_URL}")
    } // failure

    success {
      echo "Sending message to Slack"
      slackSend (color: "${env.SLACK_COLOR_GOOD}",
                 channel: "${params.SLACK_CHANNEL_1}",
                 message: "*SUCCESS:* Job ${env.JOB_NAME} build ${env.BUILD_NUMBER} by ${env.USER_ID}\n More info at: ${env.BUILD_URL}")
    } // success

  } // post
} // pipeline

I understand that you were trying to achieve it through Jenkins UI and it seems that it is not possible at the moment, thus, I think that the feature request should still open to have this option for those who prefer to stick to the graphical mode. This is just a workaround.

If you need, you can find more information about the declarative pipeline here.

Cheers!

ivan-pinatti avatar Jan 03 '18 21:01 ivan-pinatti

👍 thanks @ivan-pinatti

richardpringle avatar Jan 03 '18 21:01 richardpringle

Thanks @ivan-pinatti

ksmahesh avatar Feb 21 '18 22:02 ksmahesh

What's the status of above? Is it on the roadmap? Is it a slack plugin or Jenkins related issue?

mikolajzieba avatar Aug 24 '18 06:08 mikolajzieba

Unlikely to happen, unless someone contribute it, but it's not an easy feature to do. Easily achieved with pipeline

timja avatar Dec 16 '18 22:12 timja