def trial = 'jobName'
slackSend color: "good", message: "${trial} build successful"
The above code gives me the below Slack message-
jobName build successful
How do I timestamp the message so that I get the below message-
jobName build successful on dd/mm/yyyy
Thanks for the help.
You could use the below piece of code-
def trial = 'jobName'
def date = new Date().format('dd/MM/yyyy')
slackSend color: "good", message: "${trial} build successful on " + date
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: '' )
What I want to do is send email to users after each build with a link to Sonarqube server for that project/branch scan.
def sendEmailAfterSonarScan() {
soanrUrl = "http://lcoalhost:8080"
{ steps, domain, config ->
steps.emailext(to: "email#mydomain.com", body: '$PROJECT_NAME - Build # $BUILD_NUMBER - $BUILD_STATUS - $BUILD_URL ' + 'Please click the following link to view Sonar Report $sonarUrl', mimeType: 'text/html', subject: 'Sonar Report')
}
}
but the method fails in the pipeline with error $sonarUrl not recognised. What am I doing wrong ?
I don't know groovy, but suspect is it something to do with double or triple quotes ?
I am working currently on integration between Jenkins to Slack,
I want to fully control Jenkins from slack, basically, I want to trigger jobs, and I want to answer input if it exists.
for e.g
pipeline{
agent any
stages{
stage('Test Notification success stage'){
steps{
script{
env.createofflinepkg = input message: 'User input required',
ok: 'Submit',
parameters: [choice(name: 'Create Offline Package', choices: "Create\nSkip", description: 'Create Offline Package or Skip')]
}
slackSend (channel: 'input-response',color: '#ffff00', message: "Yellow at general : Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
}
}
}
I want this to be sent to slack and then I could answer this from slack, there is a way to do this?
Thanks in Advance.
I don't know if this is exactly what you are after. If you are sending ${env.BUILD_URL}/input URL to slack, that should redirect you directly to the approval page from Slack to Jenkins approval page.
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})")