Jenkins: attach multiple files in same email using emailext attachmentsPattern - jenkins

We are trying to use Jenkins pipeline and using the following function:
emailext attachmentsPattern: "${env.LOG_FILE}" , "${env.PASSED_SOURCES_LOG}", "${env.FAILED_SOURCES_LOG}",
body: "Hi,\nThe nightly integration tests run is completed. Please find the log attached to this email.",
subject: "NIGHTLY INTEGRATION TESTS RUN - ${BUILD_NUMBER}",
to: "${env.EMAIL}"
Unfortunatelly I could not find a way to put these multiple files in same email. It outputs error.
Would someone know how to declare it properly?
Cheers,

The attachmentsPattern takes an Ant file pattern so perhaps something like:
**/*.log
would work. Or you could collect the files by reading them and writing them to a single file and then attach that.

Related

is there a way to see all the available methods of a plugin in Jenkins?

I am learning Jenkins on my own and I am trying to learn about plugins. I have a stage to send an email with the cppcheck results with a template I found here the template instantiate the CppcheckBuildAction and access its methods, what I would like to know if is possible to check what methods are avaialable for that instance and if possible how / where I can see them.
Also how could I for example echo / println one of them. For instance in the template provided in the link above it acces the total number of errors with ${cppcheckResult.report.getNumberTotal()} but if I echo it I get an error groovy.lang.MissingPropertyException: No such property: cppcheckResult for class: groovy.lang.Binding, this is what I tried
stage('Email') {
steps {
script{
publishCppcheck pattern:'cppcheck.xml'
emailext( subject: 'foo', to: 'mail#mail.net', body: '${JELLY_SCRIPT, template="custom"}')
}
echo "${cppcheckResult.report.getNumberTotal()}"
}
}
my final goal actually is to send the email just when the report find a new error so I was thinking to save the total number of errors in an external file and compare it with each build and if the number is bigger send the email, is there any native / easier way to do this?
Most plugins should have a javadoc link. At bottom of plugin,should see javadoc
And then there's the Extension Index for core and plugins.

Jenkins Log Parser

I was using https://plugins.jenkins.io/log-parser/ plugin with freestyle Jenkins Jobs. But since moving to Jenkins Pipeline, I have not been able to integrate the log parser into the Declaratinve Pipeline syntax.
How can this be done? I also didn't find info in their docs. Also, what would be a good log parsing rule and where to specify it? In the Jenkinsfile also? Could you give an example? Thanks.
I don't user log-parser, but a quick glance at the issues suggests it is not presently compatible;
JENKINS-27208: Make Log Parser Plugin compatible with Workflow
JENKINS-32866: Log Parser Plugin does not parse Pipeline console outputs
Update:
This old response by Jesse Glick (Cloudbees; Jenkins sponsor) to similar question suggests it does in fact work now and suggests how to generate syntax, but OP complains DSL and documentation is weak.
gdemengin wrote pipeline-logparser to work around another issue JENKINS-54304
Build Failure Analyzer may also be of use to you.
YMMV
You can try something like the following:
stage('check') {
steps {
echo 'checking logs from previous stages...'
logParser failBuildOnError: true, parsingRulesPath: '/path/to/rules', useProjectRule: false, projectRulePath: ''
}
}
The pipeline syntax section in Jenkins allows you to get snippets for your Jenkinsfile

Publishing Static Code Metric reports in Jenkins using declarative pipeline(Jenkinsfile)

I am building a simple CI pipeline for my python code in Jenkins using Jenkinsfile, which basically does the following things:
Creating test environment and installing dependencies.
Running static code metrics:
various raw metrics : SLOC, comment lines, blank lines, Cyclomatic Complexity, the Maintainability Index etc.
tests coverage reports using coverage
errors and style check using pylint
Testing pulled source code (Unit testing)
So lets say i have this stage for example, for "Static code metrics":
...
stage('Static code metrics') {
steps {
echo "Raw metrics"
sh ''' radon raw --json my_python_repo/ > raw_report.json
radon cc --json my_python_repo/ > cc_report.json
radon mi --json my_python_repo/ > mi_report.json
//TODO: add conversion and HTML publisher step
'''
}
}
...
As you see above, the reports are saved in .json format. I need to figure out a way to publish these reports in a pretty visual way on Jenkins dashboard.
One of the steps is to convert the .json files to HTML and then use HTML publisher plugin to publish reports but i don't know what tool to use.
If there is way to solve this or any other way to publish these reports on jenkins dashboard, please provide the solution.
Lets say content of mi_report.json is:
{"my_python_repo/code.py": {"mi": 16.42950884051172, "rank": "B"}, "my_python_repo/test.py": {"mi": 33.532817596089814, "rank": "A"}}
There is no Jenkins-ready tool to publish custom JSON as HTML.
But you can use warnings plugin to publish your metrics in the Jenkins way.
Here small instruction on how to configure Jenkins to parse radon reports.

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.

jenkins nightwatch email report

Has anyone created a simple email report that Jenkins sends out when nightwatch builds fail? or event converted junit reports to email reports?
I would like the following information in a concise report:
Failures
List of code changes
Who was the last people to commit
I don't have a public version, but let me try to offer some assistance how you can combine nodemailer with nightwatch-html-reporter.
I have a working nightwatch located at https://github.com/shane-reaume/nightwatch-bones
You should be able to see where the nightwatch-html-reporter code lives in the globals.js as...
var reporter = new HtmlReporter({
openBrowser: true,
reportsDirectory: __dirname + '/reports/',
themeName: 'default',
reportFilename: os.split(' ').join('-') + '-report.html'
});
Here you are creating an html file. With that you can extract the html for your nodemailer (see https://community.nodemailer.com/).
It seems to be relatively easy if you follow the directions on each, but let me know if you have trouble and I can try to throw something together.
i suggest you to go with the email-extension plugin.
follow the documentation https://wiki.jenkins.io/display/JENKINS/Email-ext+plugin
you can mail the reports as attachements to respective emails sepperating by commas

Resources