Can we construct a more complex slackSend notification?

889 views
Skip to first unread message

Idan Adar

unread,
Jun 27, 2017, 1:13:35 PM6/27/17
to Jenkins Users
In a Node.js app, one can use the slack-notify package to send something like this:


slack
.send({
     
"username": "myBotName",
     
"icon_emoji": ":robot_face:",
     
"mrkdwn": true,
     
"attachments": [
         
{
           
"mrkdwn_in": ['text','pretext'],
           
"color": "#199515",
           
"text": message,
           
"fallback": "*my*: _markdown_ message."
         
}
     
]  
   
});

In a Jenkinsfile, one can do this:
slackSend (
       color
: '#F01717',
       message
: "$JOB_NAME: <$BUILD_URL|Build #$BUILD_NUMBER>, my message."
)

I'm looking for a way to accomplish the Node implementation, but using slackSend so that the message dispatched from the Jenkinsfile will be on-par with the Node.js-sent message.

Idan Adar

unread,
Jun 30, 2017, 10:11:05 AM6/30/17
to Jenkins Users
Anyone? :)

Michael Pailloncy

unread,
Jun 30, 2017, 10:41:38 AM6/30/17
to jenkins...@googlegroups.com
Alternatively, you can create your own Pipeline method, a bit like this one : https://github.com/jenkinsci/pipeline-examples/blob/master/pipeline-examples/slacknotify/slackNotify.groovy  
Since it's a simple curl, you can then directly use all the existing options/parameters offered by the Slack REST API.

Note that it's better to centralized this method inside a Global Shared Library (just in case you are not already using it :))


--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/deaa0550-128a-4513-8972-0cf991d49bbd%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Idan Adar

unread,
Jun 30, 2017, 11:23:18 AM6/30/17
to Jenkins Users
Ohhh, nice!
Thanks for the example Michael.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

Idan Adar

unread,
Jun 30, 2017, 11:44:37 AM6/30/17
to Jenkins Users
Do you happen to know why does the following line shows an error?
"mrkdwn_in": ['text','pretext'], -- expecting '}', found ':'

def payload = JsonOutput.toJson([
     
"username": "Production Deployer",

     
"icon_emoji": ":robot_face:",
     
"mrkdwn": true,
     
"attachments": [
         
{
           
"mrkdwn_in": ['text','pretext'],
           
"color": "#199515",

           
"text": "*$JOB_NAME:* <$BUILD_URL|Build #$BUILD_NUMBER>, _microservice_ in _${clusterName}_ successfully updated.",
           
"fallback": "*Production Deployer*: operation succeeded."
         
}
     
]
   
])

Michael Pailloncy

unread,
Jul 2, 2017, 9:41:09 AM7/2/17
to jenkins...@googlegroups.com
It's related to Groovy syntax : the { ... } is not interpreted as you want to here.

I think this example should work in your case :


    def payload = JsonOutput.toJson([
      "username": "Production Deployer",
      "icon_emoji": ":robot_face:",
      "mrkdwn": true,
      "attachments": [
         [
            "mrkdwn_in": ['text','pretext'],
            "color": "#199515",
            "text": "*$JOB_NAME:* <$BUILD_URL|Build #$BUILD_NUMBER>, _microservice_ in _${clusterName}_ successfully updated.",
            "fallback": "*Production Deployer*: operation succeeded."
        ]
      ]
   ])


To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/b4ac2560-b636-49da-a7e6-4d90e677a383%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages