Sending slack message in jenkins with ${currentBuild.number} - jenkins

Hey i've been trying to send message with my number of build in a slack message in my jenkins pipeline using slacksend like this
slackSend channel: 'developer-team', message: 'The unit tests have been successful for Build N°: ${currentBuild.number}'
but the message i get from slack is this way :
The unit tests have been successful for Build N°: ${currentBuild.number}
i don't know how to fix to get the appropriate message

Try like this:
slackSend channel: '#notification', color: 'good', message: "The unit tests have been successful for Build N°: ${env.BUILD_NUMBER}", teamDomain: 'testnotifgroup', tokenCredentialId: 'slack-token'
Use double quotes " " to wrap your message

Related

Scripted jenkins pipeline slackSend is not displaying vertical color line

In Jenkins Scripted (groovy) pipeline, I have below code line
slackSend channel: '#Regression-Testing-Result',
color: (currentBuild.result.equals("SUCCESS")) ? "good" : "danger",
message: (currentBuild.result.equals("SUCCESS")) ? "Tests passed" : "Tests failed"
Issue :
Tests passed or Tests failed message is printed in slack channel (Regression-Testing-Result) but the colored vertical line is not printed (vertical green line for success, vertical red line for failure)
Jenkins version : 2.319.3
Slack Upload Plugin version used : 1.7

Is there a way to stop repeated Jenkins failure notifications being sent to slack or email?

There are a few flaky automation tests in our project and at times they fail due to either timeout or some other issues but I have noticed that it returns back to normal on its own, so in the Jenkinsfile script is there a way so that the slack channel doesn't keep getting the build failures notifications after maybe 2-3 failures and then just send the notification when the build returns to normal/SUCCESS state?
I was thinking of including a different stage for success and failure and iterate over them? Not sure if that would work or if it's the correct approach for it.
try {
stage('Run Smoke Tests') {
#Test Workers setup & called here
}
echo "BUILD SUCCESS"
stage('Publish Slack Notification') {
slackSend color: '#2ECC71', channel: slackChannel, message: " Production Smoke Tests - Passed! :awesome: \n" +
" List of Enabled Tests : ${Link} \n" +
" Jenkins Test Result : ${jenkinsTestReport}"
}
}
catch (err) {
echo "BUILD FAILURE"
slackSend color: '#FF0000', channel: slackChannel, message: " Production Smoke Tests - Failed! :sadpanda: \n" +
" List of Enabled Tests : ${Link} \n" +
" Jenkins Test Report : ${jenkinsTestReport}"
throw err
}
finally {
cleanWs()
}
}```

Error Slack Send Pipeline step configured values from global config

I have downloaded & installed Slack Notification Plugin in jenkins and using slackSend in the pipeline, it was working before but now getting an error as below: After this i downloaded Global Slack Notifier plugin, but still the same error,is there any setup required? Please advice
[Pipeline] slackSend
run slackstepsend, step null:false, desc null:false
Slack Send Pipeline step configured values from global config - baseUrl: true, teamDomain: true, token: true, channel: false, color: false
ERROR: Slack notification failed. See Jenkins logs for details.
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: FAILURE
Code is as below:
if (dstry) {
def status = sh(returnStatus: true, script: "set +e; terraform plan -destroy -var-file=my.tfvars -out=destroy.tfplan")
echo "Plan Status : ${status}"
def destroyExitCode = sh(returnStatus: true, script: "set +e; terraform destroy -auto-approve")
echo "Terraform Destroy Exit Code: ${destroyExitCode}"
if (destroyExitCode == "0") {
slackSend channel: '#ci', color: 'good', message: "Destroy Applied ${env.JOB_NAME} - ${env.BUILD_NUMBER} ()"
currentBuild.result = 'SUCCESSFUL'
} else {
slackSend channel: '#ci', color: 'danger', message: "Destroy Failed: ${env.JOB_NAME} - ${env.BUILD_NUMBER} ()"
currentBuild.result = 'FAILURE'
}
}
Did you add the slack Jenkins token for integration?
Go to this Jenkins CI url, search for your team domain, then add a new configuration. Copy the name of the token or the token itself. Then go to your Jenkins pipeline script and add to slackSend, the domain and the token credential ID or the token in plain text (not secured). Should look something like this:
slackSend channel: '#ci', color: 'good', message: "Destroy Applied ${env.JOB_NAME} - ${env.BUILD_NUMBER}", teamDomain: 'your_domain.slack.com', tokenCredentialId: 'your_id'
or if you want to use the token in plain text token:'your_token' instead of the tokenCredentialId
Hope this helps!

Groovy slacksend function doesn't work in jenkins pipeline

I have a groovy slacksend function to notify to slack channels.
slackSend(
channel: "#channel-name",
color: "warning",
message: "Could not confirm server started - ${env.BRANCH} (<${env.BUILD_URL}/console|Details> - <${SERVER_URL}|Open>)"
)
But the output is something like this
[Pipeline] slackSend
run slackstepsend, step null:false, desc :true
Slack Send Pipeline step configured values from global config - baseUrl:
true, teamDomain: true, token: true, channel: false, color: false
and there is not notification being sent. Any idea?
Those values in your pipeline output should be getting populated with the values from your jenkinsfile. eg, teamDomain: true should be teamDomain: <your_slack_team>. You can pass each of these as a parameter in the slacksend invocation in your Jenkinsfile just as you are with channel, color, and message. Also, your channel name doesn't require #, though i don't know if that would cause it to fail.
slackSend channel: '#yourchannelname', color: color, message: 'Starting branch', teamDomain: 'your_enterprise_team_domain', token: 'token_generated_from_jenkins_slack_integration'

How to change slack notification in jenkins?

I have 'Slack Notification Plugin' installed in Jenkins.
It configured to notify when Build fails or Build success.
For example with such a message:
jenkins BOT [9:00 PM]
----------------
web-services tests - #58 Success after 1 min 38 sec (</job/web-services%20tests/58/|Open>)
Is it possible to customize message? I want to have something like this:
jenkins BOT [9:00 PM]
----------------
web-services tests - #58 Success after 1 min 38 sec (USER_NAME)
thank you in advance.
If you use jenkins 2.0 you can change massage something like this:
slackSend color: 'good', message: 'Message from Jenkins Pipeline'
Or something like this in UI
You can use Jenkinsfile and every configuration will be in your vcs.
node {
try {
notifyStarted()
stage 'Checkout'
sh gg
stage 'Build'
sh xx
stage 'Deploy'
sh yy
notifySuccessful()
} catch(e) {
currentBuild.result = "FAILED"
notifyFailed()
}
}
def notifyStarted() {
slackSend (color: '#FFFF00', message: "STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
def notifySuccessful() {
slackSend (color: '#00FF00', message: "SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
def notifyFailed() {
slackSend (color: '#FF0000', message: "FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}

Resources