Send shell variable to slack notification in jenkins job - jenkins

I have a shell script which does the deployment. There is a variable set in my shell script and I want to pass that variable in slack notification custom message, but its not working as expected.
I have used envInject plugin to set the shell variable as environment variable and its not working as expected
${CRQ} is completed in staging. Please validate
Console url is : ${BUILD_URL}console
staging_daily_dose - #114 Failure after 4.4 sec (Open)
Console url is : https://jenkins-host/job/staging_daily_dose/114/console```
I want ${CRQ} with the actual value. Can someone help me with the best approach

From various posts it is clear that to add an environment variable that can be used by Slack Notification Plugin needs to be inserted using Inject Environment Variable, So
In The Build step create a properties file and write the required variables, and use the file in the Inject Environment Variables Plugin
Next use the variable in the Slack Notification Plugin
This will result in a slack message like below.

You can refer the variables as $VARIABLE along with the messages in the Post-build Actions -> Slack Notifications -> Include Custom Message section as shown in the image below.
Slack notification configuration:

Related

How to send additional data elements in Jenkins Notification Plugin?

I use Jenkins Pipeline Jobs and invoke build using its remote API's.
I also use Build Notification plugin to invoke my API once the build is complete for further downstream automation. As per the plugin documentation, it provides a fixed set of data elements from the build. However as part of the build, the Job has generated some data elements which I need to provide it back to my API which gets invoked by Notification plugin (As part of the JSON Payload). Can someone help me how do pass additional data elements through this plugin? or any better ways of doing it?
For example,
1. When the pipeline job is configured with notification endpoint, the jenkins config XML has the following entry
<com.tikal.hudson.plugins.notification.HudsonNotificationProperty plugin="notification#1.11">
<endpoints>
<com.tikal.hudson.plugins.notification.Endpoint>
<protocol>HTTP</protocol>
<format>JSON</format>
<url>http://localhost/api/postStatus</url>
<event>finalized</event>
<timeout>30000</timeout>
<loglines>20</loglines>
</com.tikal.hudson.plugins.notification.Endpoint>
</endpoints>
</com.tikal.hudson.plugins.notification.HudsonNotificationProperty>
A pipeline script just builds an image and the image ID has to be sent in the notification
I did not find a perfect solution in the existing Jenkins Notification Plugin. However the solution that I used it to pass the data as part of the Log Text and parse the information in the other side.

Not able to access custom parameters passed to the jenkins build script

I want to add some parameters from the web hooks into the mail sent from the Jenkins. I tried solutions provided in StackOverflow and elsewhere. No success yet.
I have the following in place:
Checked This is a parameterized build
The build script looks like http://JENKINS_URL/job/android02/buildWithParameters?token=<My Token>&PARAM=<My Custom Params>
In the mail content, I try to access the custom parameters by ${PARAM}, $PARAM.
In the mail, however, I'm not getting the values I'm setting. If I set default values for PARAM, it is correctly displayed in the mail I receive. I tried http://JENKINS_URL/job/android02/build?token=<My Token>&PARAM=<My Custom Params> URL as well thinking just in case if it works. The mail is sent as Editable email Notification config.
Basically, everything is working except that I'm not able to access the custom parameters I have passed via Trigger builds remotely option.
Edit1:
If I keep a default value, say, test for the parameter, PARAM, in the mail received, I can see test displayed. But I need to get the value I'm passing in the build script.
From the "tool tip" that you get when you click on the question mark next to "":
${ENV,var="VARIABLENAME"}
Expands to an environment variable (specified here as VARIABLENAME) from the build environment. Note that this does not include any variables set by the build scripts themselves, only those set by Jenkins and other plugins.
So, use ${ENV,var="PARAM"}.

Setting Jenkins to email a build notification to the BitBucket user who pushed a branch

A project repository has been successfully connected to a Jenkins server using the BitBucket plugin, and a project set up such that:
Each push to a branch in BitBucket will trigger a webhook sent to the Jenkins server
When the Jenkins server receives the webhook it will build the changed branch (by specifying branch name as ** in the config)
After the build is complete a notification is sent back to BitBucket of the build status using the BitBucket notifier
Each of these has been easy to set up with just the instructions in the plugin and a few quick Googles. However I've now run into a problem which is maybe more a matter of wanting to run in an unconventional manner than anything else.
Using the normal emailer plugin or the Email-ext plugin it's possible to set emails to send to people involved in the creation of a build. For example the Email-ext plugin allows choice of:
Requester
Developers (all people who have commits in the build based off its last version)
Recipient list (a pre-set list)
Various "blame" settings for broken builds
The development process being followed involves each project being worked on by one developer in a named branch, e.g. userA/projectB. Obviously other developers could check that out and push to make changes but that's frowned upon. Even in that instance, the user who pushes the change to BitBucket should be notified.
None of the current settings support this. Requester is the closest, but that only works for manual builds. It seems a very simple requirement that the push to SCM that triggered a build should notify the user who pushed, but this is not documented anywhere that is easy to find.
After a lot of searching it seems the only way to accomplish this is by using a Pre-send script. This is added to the Advanced setting of the Email-ext post-build step, and takes the form of code written in Groovy which is a Java extension.
The script can take advantage of Environment variables, but is hard to test as there's no way to run the script with these in place. You can test simple Groovy scripts from Home -> Manage Jenkins -> Script console.
One important "gotcha" with the environment variables is that they are "included" in the script, rather than variables or constants. E.g. before the script compiles and runs, the content of the variable is pasted in place of its $NAME. In the example below the multi-line string syntax is used to include the BitBicket payload, whereas it might be expected that def payload = $BITBUCKET_PAYLOAD would simply work.
import javax.mail.Message.RecipientType
import javax.mail.Address
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeMessage
import groovy.json.JsonSlurper
def jsonSlurper = new JsonSlurper()
def bitbucket = jsonSlurper.parseText('''
$BITBUCKET_PAYLOAD'''
)
switch (bitbucket.actor.username){
case "userA":
msg.setRecipients(MimeMessage.RecipientType.TO, InternetAddress.parse("user.a#domain.com"));
break;
case "userB":
msg.setRecipients(MimeMessage.RecipientType.TO, InternetAddress.parse("user.b#domain.com"));
break;
}
The setRecipients command overwrites any existing recipient. Thus the recipient list or other email configuration should be set as a fallback for if the user is not recognised. Additionally, if there is nobody selected to send the email to, the script won't run at all. As added debugging, including the username in the body might help.
If the script fails, stack traces should be printed to the console log output of the test, and the build pass/fail shouldn't be affected, but the normal email address setup will be used instead. In stack traces look for lines with Script() in them, as that's the container which evaluates the Groovy script.

Stash Pull Request Builder plugin for Jenkins custom variables in comments

I have the Stash pull request builder plugin working great in Jenkins, it see's a pull request and kicks off the Jenkins job which runs some tests and puts a generic comment on the pull request saying pass or fail.
The problem I'm having is when the plugin comments back to the pull request I'd like to use a variable I create in the post build section, I've tried creating the variable as an environment variable but it seems the comments part of the plugin can only see the built in Jenkins variables like ${BUILD_NUMBER} anything else just prints out the name directly.
I've had a look at envinject, but I'm not sure how to set a variable from it in the post build section, or even if the variables it creates would be seen by the Pull Request Builder Plugin.
I did a quick test with my Jenkins/Stash platform.
I have a job using this custom variable:
I've added a post-build step to publish a custom comment:
We can see that my SCM_REVISION environment variable is displayed in Stash:
I hope it helped :)

To get build status through environment variable

I am using jenkins for continous integration.
For my build purpose, i triggering email using Ant task. I am not able to find an environment variable to pass ant for sending email build status(success/failure/stable).
i want to know how can i get environment variable for build status?..if not available, what is the alternative option for build status?
Thanks in Advance
varghese
Why use ANT to send emails from Jenkins when you have two great plugins that does just that?
The default mail notification is quite good, and if you would like to have more control
I suggest using the Email-ext plugin which is very comprehensive.
If still wish to use ANT to sent your mail-notifications including the status
you will have to break your process into two steps,
where the first part runs the build and the second one runs the ANT script to report the status.
In this case, you will need to trigger the second job from the first job via the Parameterized Build plugin -
see my answer here:
trigger other configuration and send current build status with Jenkins
The build status is not set until the job has finished running, so there is no easy way to push build status to a process triggered within the build itself. You could pull build status via the API, but this would have to be an externally triggered process due to the constraint mentioned above. Any reason you aren't using the built in email support or one of the excellent email extension plugins such as this one?

Resources