DSL Jenkins Slack Notifiers

DSL Config

DSL jobs are used to automate and have control over the Jenkins processes. To manipulate DSL jobs, we need basic groovy knowledge.

  • Follow the previous post before proceeding this post
  • Install Job DSL Jenkins plugin if not installed by default. Go to Manage Jenkins > Manage Plugins > Available

  • Create a freestyle Jenkins job for DSL, and configure your SCM git repo in it

  • Add DSL config. Go to Configure > Build > Add build step > Process Job DSLs

  • Point to all the groovy files in GitHub repo

  • DSL groovy snippet for slack notifiers in publishers section
  configure { project ->
    project / publishers << 'jenkins.plugins.slack.SlackNotifier' {
    baseUrl("https://hooks.slack.com/services/K3YH3REOK/")
    room("#tests")
    notifyAborted(false)
    notifyFailure(true)
    notifyNotBuilt(false)
    notifyUnstable(false)
    notifyBackToNormal(false)
    notifySuccess(true)
    notifyRepeatedFailure(false)
    startNotification(true)
    includeTestSummary(false)
    includeCustomMessage(true)
    customMessage("@here Excuse me! I've something for you")
    sendAs(null)
    commitInfoChoice("NONE")
    teamDomain("your_domain_name")
    authToken("TOK3N97EA/tOKenHeRepLeasE")
    }
  }

[Text in red has to be customized specific to the project]

  • Example .groovy formatted file for a Jenkins multiphase-job
  multiJob('job_name') {
    description 'Multijob Project to start tests in parallel'

    steps {
         phase('job_name') {
            phaseJob('job_1')
            phaseJob('job_2')
            phaseJob('job_3')
        }
    }

    configure { project ->
     project / publishers << 'jenkins.plugins.slack.SlackNotifier' {
     baseUrl("https://hooks.slack.com/services/K3YH3REOK/")
     room("#tests")
     notifyAborted(false)
     notifyFailure(true)
     notifyNotBuilt(false)
     notifyUnstable(false)
     notifyBackToNormal(false)
     notifySuccess(true)
     notifyRepeatedFailure(false)
     startNotification(true)
     includeTestSummary(false)
     includeCustomMessage(true)
     customMessage("@here Excuse me! I've something for you")
     sendAs(null)
     commitInfoChoice("NONE")
     teamDomain("your_domain_name")
     authToken("TOK3N97EA/tOKenHeRepLeasE")
     }
    }
  } 

  • Now, run the DSL job
  • You’re all set now with the slack notifiers

 

NOTE:

Every time you make changes in the DSL groovy file and update from GitHub, it actually throws an error. Apparently, you’ve to approve the groovy files before executing DSL jobs.

  • Go to Manage Jenkins > In-Process Script Approval

  • Manually approve the updated scripts as in here,

  • The manual approval can be automated too; go to Manage Jenkins > Configure Global Security > CSRF Protection
  • Disable Enable script security for Job DSL scripts

 

 

Advertisements

Jenkins Slack Notifiers

Manual Config

  • Login slack from the web browser and navigate to incoming webhooks
https://my.slack.com/apps/A0F7XDUAZ-incoming-webhooks
  • Now, add configuration on clicking the Add Configuration button

  • Select the channel to be coupled with Jenkins and click on Add Incoming WebHooks integration

  • Now, a token will be generated as shown below

  • Install Slack Notification Jenkins plugin. Go to Manage Jenkins > Manage Plugins > Available

  • Open Jenkins job, Configure > Add post-build action > Slack Notifications

  • Fill-up the Slack notifications form with details obtained earlier

  • You’re all set now and start receiving notifications