According to the main Jenkins-Slack documentation you have to configure the Jenkins app for slack and there you specify a single channel where the plugin can post. Now in my case, I need to post to multiple channels and users from a Jenkins server and it is very cumbersome to manage multiple auth tokens. Is there a way to have a single token that would work with all the channels and users? Are there other approaches? I see that the Jenkins plugin has a bot checkbox, but there is almost no documentation on how to make this work.
You can add channel: '#CHANNEL_NAME:
More info: https://jenkins.io/doc/pipeline/steps/slack/
post {
success {
slackSend (channel: '#ch1', color: '#00FF00', message: "SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
failure {
slackSend (channel: '#ch1', color: '#FF0000', message: "FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
}
I just implemented the solution for my project channels.
There is an Advanced button available in the Post build actions -> Slack notification of a Job.
You can give the base URL and token along with the channel name you want to send the notifications to.
If one has enough permissions to their organization's slack account, one can create an app at https://api.slack.com/apps?new_app=1 which acts as a bot.
Once this bot is installed to workspace [ https://api.slack.com/apps/<org-id>/install-on-team? ] and was given permission to post to targeted channels
( just send a message /invite #<bot-name> in the channel and it will have permissions to post to that channel]. Not sure how to do it for users, though.
Apart from that, slackSend supports sending to multiple channels in a single go.
Multiple channels may be provided as a comma, semicolon, or space delimited string.
https://jenkins.io/doc/pipeline/steps/slack/
For ex., any one of the following are valid:
channel: "#ch-1,#ch-2,#ch-3"
(OR)
channel: "#ch-1;#ch-2;#ch-3"
(OR)
channel: "#ch-1 #ch-2 #ch-3"
Full command may be as:
slackSend (channel: '#ch1 #ch2 #ch3', color: '#00FF00', message: "SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
Related
So I'm trying to use slackSend in my Jenkinsfile to post build status on a channel and I'm trying to explicitly define everything like this- (for some reason I have to define it like this)
WithCredentials([string(credentialsId: 'theSlackToken', variable: 'slackCredentials')]) {
slackSend (channel: '#johnsmith', message: 'hello there', color: '#3eb991', failOnError: true, teamDomain: 'myteamsubdomain', token: slackCredentials)
}
But when I use these above lines in my Jenkinsfile I get error saying no such DSL method string.
Please help I will highly appreciate any suggestions.
There's a few issues here:
First for your command to work, you shouldn't use 'withCredentials', only thing you need is the credentialsId, and then set it as tokenCredentialId, like this:
slackSend (channel: '#johnsmith', message: 'hello there', color: '#3eb991', tokenCredentialId: theSlackToken)
Importent: The credentials should be Secret Text!!!
There is no need to use tokenCredentialId in every slackSend command, you can set it as the general slack token for this Jenkins. over Jenkins -> manage Jenkins -> Configure System, then look for Slack, and enter the right credentials for the token you want to be used. Then you can use:
slackSend (channel: '#johnsmith', message: 'hello there', color: '#3eb991'
Using tokenCredentialId in the slack command allows to overwrite the current global token, and like that you can switch between slack apps inside Jenkins.
For more info:
https://plugins.jenkins.io/slack/
Jenkins slackSend question: How to perform an alert notification to a channel that has received a message that needs to be addressed by either any member #here or a specific person ex #Jane.Doe?
Reviewed Jenkins Slack notifications: https://www.jenkins.io/doc/pipeline/steps/slack/
Tried adding #here to the beginning of slackSend message:
failure {
script {
slackSend(
color: "#FF0000",
channel: "${SLACK_CHANNEL}",
message: "#home FAILED"
)
}
}
However, adding #here to slackSend message does not initiate alert notification for that channel.
Suggestions?
Note: Received a comment from a coworker, I am investigating this path:
thought I remember needing to do a look up on users to get an ID and then some special syntax it parses into a username. been a while though.
First you must find the ID of the user and then mention it in the the message, like this example:
def userId = slackUserIdFromEmail('spengler#ghostbusters.example.com')
slackSend(color: "good", message: "<#$userId> Message from Jenkins Pipeline")
Reference: https://plugins.jenkins.io/slack/
Edit:
If you want a put a special or a group mention, you only need follow the format guideline what can you see here : https://api.slack.com/reference/surfaces/formatting#special-mentions
Try it
def emailWithoutDomain = env.BUILD_USER_EMAIL.replaceAll("#.*","").trim()
slackSend ( message: 'I want to mention <#${emailWithoutDomain}> this #user as part of message', teamDomain: '', token: '', channel: '' )
I use the Jenkins Office 365 Connector and it sends messages of the build status to MS Teams as expected.
Now I want to add the value of a Jenkins job parameter to the message.
My usecase: I use a single job to deploy several services. I want to know in the message which service was deployed.
Notification from Dev_Deploy
Latest status of build #43
Status
Build Success
Remarks
Started by user XXX
Service
service-abc
I've seen in the Advanced configuration that there are Macros and Fact Definitions. Unfortunately there is no documentation in the plugin docs. Perhaps this configuration could help?
There is no option to customize the message in the jenkins GUI.
But a custom message can be specified in the pipeline script:
steps {
// some instructions here
office365ConnectorSend webhookUrl: 'https://outlook.office.com/webhook/123456...',
message: 'Application has been [deployed](https://uat.green.biz)',
status: 'Success',
color: '#0000FF'
}
Hint: The status color is not automatically set. So you have to set the color depending on the status.
Official documentation
In order to get the repository data, you can follow instructions to create a checkout snippet through the Jenkins UI in the configuration.
Once you input the correct URL, browser, and so on, you may invoke the office365ConnectorSend plugin. You may adapt the card sent by passing the factDefinitions attribute an array of [name,template] objects as outlined in Jenkins Docs. You can find an example in the open-source code readme.
there are some defaulted add-ons, but this should set you on the correct path.
Here is an example of my office365ConnectorSend:
office365ConnectorSend (
webhookUrl: "${webhookURL}",
color: "${currentBuild.currentResult} == 'SUCCESS' ? '00ff00' : 'ff0000'",
factDefinitions:[
[ name: "Commit Message", template: "${commit_message}"],
[ name: "Pipeline Duration", template: "${currentBuild.durationString.minus(' and counting')}"]
]
)
What i did:
I managed to send Notifications with Status of every Build to my private Slack Channel. So i configured my Jenkins as well as my Slack app.
What i want to do:
Sending messages to other users private Channels.
What i have tried:
I added channels to my Jenkinsfile and checked them in the Slack App of Jenkins and it wasn't successful. I think i have to create sth like a Bot, which is in the specific Channel i want to send a message to. Very sure that Jenkins can't see the channel because its a private channel of another person(obviously) and is not able to find it. Couldn't find a solution for this Problem.
Thanks a lot for your help, i think i wasted way to much time trying to find an answer for that.
I setup our build pipeline to send Slack notifications to commit authors. The biggest challenge is mapping to the Slack username. I had every developer on the team change their git user.name to match their Slack Display name. This is the most straight-forward way I know to make this work.
git config --global user.name "Mona Lisa" where "Mona Lisa" is the Slack display name of the user.
In the Jenkins pipeline, I used env.GIT_COMMIT_AUTHOR to get this value back.
Note, that the environment variable provides the commit author at the HEAD of whatever was checked out. For pull requests in a multi-branch pipeline, if the PR is not a fast forward, the commit is what the merge would produce, resulting in an author of 'Jenkins'. So, in that case, you would need the author from HEAD~1
In my experience sending Slack messages to a private channel requires OAuth. I have only succeeded sending to private channels via slackSend() when Jenkins is added to Slack as a bot and invited to the channel.
Here's an example pipeline that works:
#!/usr/bin/env groovy
pipeline {
agent {
node {
label "master"
}
}
stages {
stage('Send Notification') {
steps {
script {
def color = "${params.MESSAGE_STATUS}" == "GOOD"? "good" : "warning"
slackSend(color: "${color}", message: "${params.MESSAGE}", channel: "${params.CHANNEL}")
}
}
}
}
parameters {
string(name: 'MESSAGE', defaultValue: 'Hello')
string(name: 'CHANNEL', defaultValue: '#test_private')
choice(name: 'MESSAGE_STATUS', choices: ['GOOD', 'WARNING'], description: '')
}
}
Sending via bots etc. hasn't been successful as Slack reports the channel as non-existant.
I can find tons of information in the internet about integrating HipChat in a scripted Jenkins pipeline. So that works fine for me. But how can I post a status message to HipChat that contains an element to trigger an action back in Jenkins?
Currently I have input steps in Jenkins to approve a deployment to the next stage, e.g. PROD.
I also send a HipChat message if approval is needed which contains a link to JENKINS. That looks like this:
hipchatSend color: "${color}", notify: true, room: "Jenkins Team FooBar", message: "${env.JOB_NAME}#${env.BUILD_NUMBER}: ${message}", textFormat: true
/************** PROD **************/
stage ('Approval for PROD') {
try {
timeout(time: approvalTime, unit: approvalTimeUnit) {
input 'Do you approve the deployment to PROD?'
}
} catch (Exception ex) {
finishBuild = true
}
}
// Stop build if flag was set
if (finishBuild)
return
How can I define actions in that hipChat message? Is there a way that I can approve the next build step within HipChat?
I have not found a tutorial or documentation on how to define other kinds of hipchat messages with this plugin.
I could send a POST request to JENKINS if the message would contain standard HTML. Any ideas on how to do that?
How would it work with cards?
Thanks in advance.