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})")
}
Related
I have scenario where i need job to run only if the previous build was success .
if previous build had failed i need user to wait for admin apporval.
if previous build had success user can run the job.
can anyone help me how to go about it .
Below is the pipeline script which will check the previous build and ask for the approval if the pervious build is failed or sucess
pipeline{
agent any
stages{
stage('Previous Status check'){
steps{
echo "Checking"
sleep 10
}
}
stage('Deploy approval'){
when {
expression {
// When last build has failed
!hudson.model.Result.SUCCESS.equals(currentBuild.rawBuild.getPreviousBuild()?.getResult()) == true
}
}
steps {
input(message: 'last build was failed please check target group and approve', ok: 'Release!' , submitter: "ritesh.mahajan")
}
}
stage('Building code on server'){
steps{
script {
def inputConfig
def inputTest
// Get the input
def userInput = input(
id: 'userInput', message: 'Enter the branch to build',
parameters: [
string(defaultValue: 'Master',
description: 'Enter the branch',
name: 'Branch'),
])
inputbranch=userInput
echo "${inputbranch}"
echo "here we can execute script in remote machine by default it will build master ..we can also accept parameter"
}
}
}
}
}
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()
}
}```
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!
Can I use slackSend command in jenkins flow dsl if i have Jenkins ver. 1.656.
I have enabled Slack Notification Plugin and it works fine in most cases, but i wish to display message when build starts.
You can set up script in the pipeline, should be something like this:
def notify(status) {
slackSend channel: "#jenkins",
color: '#d71f85',
message: "${status}",
tokenCredentialId: 'yourtoken'
}
pipeline{
....
stages{
stage('Buildstart) {
steps {
notify("Build Started")
}
}
....
}
}
I have a Jenkinsfile that is running a shell command and I want to send the output of that sh method as the message: of the slackSend method.
I have the following so far, but the message to the slack channel is empty. I'm not certain how to capture the output in a way that I can reference it in the message section:
node {
checkout scm
stage 'run shell command'
def shell_command = sh "ls -l"
def shell_output = apply_cluster
stage 'notify slack-notification'
slackSend channel: '#slack-notifications', color: 'good', message: shell_output, teamDomain: 'company', token: env.SLACK_TOKEN
}
After a little more research, I discovered a couple of things I did not initially understand: The first is that even with defining a function in a Jenkinsfile, if it's a DSL method like sh, it will still get executed, so the second function I defined isn't necessary. Second, if you want to return stdout, you have to specify that as part of the command. The new code looks like this:
node {
checkout scm
stage 'run shell command'
def shell_command = sh script: "ls -l", returnStdout: true
stage 'notify slack-notification'
slackSend channel: '#slack-notifications', color: 'good', message: shell_command, teamDomain: 'company', token: env.SLACK_TOKEN
}