Customise slack message with if block in Jenkins - jenkins

I have a requirement to send out a slack message via jenkins post build step based on certain condition. I have put the below code snippet under "Custom Message" of Slack Notification of my jenkins build. Below prints the entire content of get_schedule_response.txt.
Here's the result for the schedule execution --------> ${FILE,path="$WORKSPACE/get_schedule_response.txt"}
I want to send out this slack notification only if the get_schedule_response.txt file content contains text SUCCESS in it. Is it possible to do?

by using grep command u can find out the word SUCESSS in the file get_schedule_response.txt file
if [(grep -o "SUCCESS" "$WORKSPACE/get_schedule_response.txt"}) = "SUCCESS"];
then
#include your slack notification for success
else
#include your slack notification for success
# -o : Print only the matched parts of a matching line,
to refer all options for grep command follow this link
https://www.geeksforgeeks.org/grep-command-in-unixlinux/

Related

Python string as build parameter to Jenkins Trigger mail doesn't read the entire string. Instead cuts half of the string while reading

This is inside my trigger email
SQLScripts=["Query1","Query2","Query3","Query4","Query5","Query6"]
What gets read in string parameter for Jenkins build is following
This line of code executes
echo %SQLScripts%
Prints
echo ["Query1","Query2","Query3",
Initially I thought this could be some problem with how I wrote variable name, I tried $SQLScripts and "$SQLScripts". But problem is with reading the variable from email.
As I have manually added value inside jenkins build configuration and echo printed the entire value.
Please any help is appreciated.

How to pass pipeline variables to post build gerrit message?

I have a Pylint running in a Jenkins pipeline. To implement it, I used Gerrit trigger plugin and Next Generation Warnings plugin. Everything is working as expected - Jenkins is joining the review, checks change with pylint and generates report.
Now, I'd like to post pylint score in a custom "Build successful" message. I wanted to pass the pylint score to a environment variable and use it in dedicated window for Gerrit plugin message.
Unfortunately no matter what I try, I cannot pass any "new" variable to the message. Passing parameters embedded in pipeline works (e.g. patchset number).
I created new environment variable in Configure Jenkins menu, tried exporting to shell, writing to it (via $VAR and env. syntax) but nothing works - that is, build message displays raw string like $VAR instead of what variable contains.
What should I do to pass local pylint score (distinct for every pipeline occurence) to the custom build message for Gerrit?
I don't think the custom message can be used for this. This is just supposed to be a static message.
They way I do this is to use the SSH command to perform the review. You can also achieve the same using the REST API.
First I run my linting and white space checking script that will generate a json file with the information I would like to pass to Gerrit. Next I send it to Gerrit using SSH. See below my pipeline script and an example json file.
As a bonus I have added the robot comments. This will now show up in your review as a remark from Jenkins that line 8 of my Jenkins file has a trailing white space. You can easily replace this with your lint result of you like or just ignore it and only put the message. It is easier to use a json file as it will make it easier to create multi line messages
node('master') {
sh """
cat lint_change.json | ssh -p ${env.GERRIT_PORT} ${env.GERRIT_HOST} gerrit review ${env.GERRIT_PATCHSET_REVISION} --json
"""
}
Example json file:
{
"labels": {
"Code-Style": "-1"
},
"message": "Lint Bot Review\nLint Results:\n Errors: 0\n Warnings: 0\n\nWhitespace results:\n Errors: 1",
"robot_comments": {
"Jenkinsfile": [
{
"robot_id": "lint-bot",
"line": "8",
"message": "trailing whitespace."
}
]
}
}
Alternatively, you may want to look at a new gerrit-code-review-plugin that should make this things even easier. However, I have not tried this yet.

How to send an email with pass/fail tests on jenkins using editable email notificaiton

I have a jenkins job that builds an iOS app and runs a test script which posts the results into an xml file using xcpretty.
In the post build actions I publish the results in xml format. Is it possible to get some of the contents of these results and put them into the email notification to send i.e.
Tests Passed: {Number of passed tests}
Tests Failed: {Number of failed tests}
Total Tests: {Total tests}
In your "Execute Shell"
echo "test log" > /some/file/path/logFile.txt
Then in your "Editable Email Notification-Default Content"
${FILE,path="/some/file/path/logFile.txt"}
You can use attach the XML file to the e-mail and try to use HTML block and add the file name inside. I used it for jpeg file and it worked great.
It's also possible to use a script to examine the file and assign an environment variable, that you'd later use in the content of the email.

OSSEC Slack Integration

I want all OSSEC notifications to be routed to a Slack room instead of email. 2.9.Beta5 has a ossec-slack.sh active response script. The relevant parts of my ossec.conf are:
<command>
<name>ossec-slack</name>
<executable>ossec-slack.sh</executable>
<expect>srcip</expect>
<timeout_allowed>no</timeout_allowed>
</command>
<active-response>
<command>ossec-slack</command>
<location>local</location>
<level>1</level>
</active-response>
This works for SSH logins (failed and successful), but as far as I can tell doesn't trigger anything else. What am I doing wrong/how are others doing this? Is this just beta software being beta software?
First make sure your ossec-slack.sh file has the correct information in the top:
# FILE: /var/ossec/active-response/bin/ossec-slack.sh
SLACKUSER="ossec"
CHANNEL="#slack_chanel" # include the hash "#"
SITE="https://hooks.slack.com/services/TOKEN"
SOURCE="ossec2slack"
Your "SLACKUSER" is the same as the "Customize Name" field that you set in your Slack WebHook Integrations page.
Now that your ossec-slack.sh file is set up you can test your Slack integration manually:
/var/ossec/active-response/bin/ossec-slack.sh
Running the script manually will post recent entries from your alerts log file:
/var/ossec/logs/alerts/alerts.log
When this script is triggered as an active-response, it will only post the information for the current alert, rather than posting from your log file.
When you have verified that you can post Slack messages manually, add the following XML blocks to your ossec.conf file:
<!-- FILE: /var/ossec/etc/ossec.conf -->
<ossec_config>
<command>
<name>ossec-slack</name>
<executable>ossec-slack.sh</executable>
<expect></expect> <!-- no expect args required -->
<timeout_allowed>no</timeout_allowed>
</command>
<active-response>
<command>ossec-slack</command>
<location>local</location>
<level>3</level>
</active-response>
</ossec_config>
The settings above will post to your Slack channel whenever a level 3 or above alert is triggered.
Note: no arguments are required within the <expect> tag. But the <expect> tag itself, is required. See OSSEC's active-response documentation for more information.
To test this integration, restart your ossec server:
/var/ossec/bin/ossec-control restart
You should see the "OSSEC Started" alert very quickly:
If you do not see the alert, check your logs for any misconfigurations:
tail /var/ossec/etc/logs/ossec.log
tail /var/ossec/logs/active-responses.log
Not a full answer, but adding on here. To ensure this works, make sure you don't have this set in /var/ossec/etc/ossec.conf. If it's there, just remove.
<active-response>
<disabled>yes</disabled>
</active-response>

Jenkins: How to pass strings to the body of the email notifications

Inside the Execute Shell, a LIST=$(ls -1 dir_name/*rpm) is done. How can I pass the contents of LIST so that they're displayed in the Default Content or whatever is the body of the Editable Email Notification?
You can use the InjectEnv plugin with some settings like below:
In the Jenkins build step (or post step), you can use the $MY_LIST variable:
+ echo build-failure-analyzer.xml buildtriggerbadge.xml cloudbees-plugin-gateway.xml com.cloudbees.dockerpublish.DockerBuilder.xml com.orctom.jenkins.plugin.globalpostscript.GlobalPostScript.xml com.tikal.jenkins.plugins.multijob.PhaseJobsConfig.xml config_project_creation.xml config.xml config.xml.old Connection Activity monitoring to slaves.log credentials.xml Download metadata.log envinject-plugin-configuration.xml envInject.xml Fingerprint cleanup.log hudson.maven.MavenModuleSet.xml hudson.model.UpdateCenter.xml hudson.plugins.copyartifact.TriggeredBuildSelector.xml hudson.plugins.git.GitSCM.xml hudson.plugins.git.GitTool.xml hudson.plugins.groovy.Groovy.xml hudson.plugins.logfilesizechecker.LogfilesizecheckerWrapper.xml

Resources