How to use Log Parser Plugin in a Jenkins Workflow script - jenkins

The Log Parser Plugin is now compatible with Workflow but how do I use it?

Easiest is to use Snippet Generator to get an example step statement using it.

Snippet Generator is a good idea; however, it is not generating the complete line to make it work. It is necessary to add at the end of the code line ", projectRulePath: ''".
Final line will be in the way, for example:
logParser failBuildOnError: true, parsingRulePath: '/myRulesPath', showGraphs:true, unstableOnWarning: true, useProjectRule: false, projectRulePath: ''

Related

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

Jenkins file can we use the IF statement

in Jenkins file one of the variable is having the comma separated values like below.
infra_services=[abc,def,xyz]
when I write the below code it was throwing an error.
if ("{$Infra_Services}".contains("xyz"))
then
echo "$Infra_Services"
fi
yes you can do if statements in a Jenkinsfile. However if you are using declarative pipeline you need to brace it with the step script.
Your issue comes from the fact you did not put any double quotes around "abc" and all the elements of your array
infra_services=[abc,def,xyz]
​
A second error will raise after you fix this. If infra_services is an array, to manipulate it you should not try to cast it as string. It should throw when you do "{$Infra_Services}"
here is a working example
​def Infra_Services = ["abc","def","xyz"]
if (Infra_Services.contains("xyz")) {
println "found"
}​​
My advice is to test your groovy before running it on jenkins, you will gain precious time. Here is a good online groovy console I use to test my code. running the groovy console from terminal is an alternative
https://groovyconsole.appspot.com/

Jenkins declarative pipeline : How to configure the klocwork result display on the job page

I am creating a pipeline using the declarative pipeline flavour, with clockwork steps enclosed within a klockwork wrapper where I can define the klocwork setup :
klocworkWrapper(installConfig: 'My Klocwork', ltoken: "${HOME}/.klocwork/ltoken", serverConfig: 'Klocwork#XYZ', serverProject: 'S3cr3TPr0j3ct') {
klocworkBuildSpecGeneration([additionalOpts: '', buildCommand: 'make', ignoreErrors: true, output: 'kwinject.out', tool: 'kwinject'])
klocworkIntegrationStep1([additionalOpts: '', buildSpec: 'kwinject.out', disableKwdeploy: false, ignoreCompileErrors: true, importConfig: '', incrementalAnalysis: false, tablesDir: 'kwtables'])
klocworkIntegrationStep2([additionalOpts: '', buildName: "${JOB_BASE_NAME}_${BUILD_NUMBER}", tablesDir: 'kwtables'])
}
Ok, analysis is launched, and I can see the results on the Klocwork server web interface.
But I cannot find a way to retrieve resulting diagrams on the Jenkins web interface, even when using the pipeline script generator.
Unless I am totally wrong, I think that I should use klocworkQualityGateway, but the generated script snippet is not correct.
Once copied within the wrapper, it fails lacking for some enableXYGateway or gatewayXYConfig property.
For example this line :
klocworkQualityGateway([enableCiGateway: false, enableServerGateway: true, gatewayServerConfigs: [[conditionName: 'Issues', jobResult: 'failure', query: 'state:+Status,Fix', threshold: '1']]])
fails with an error message :
WorkflowScript: 92: Missing required parameter: "gatewayCiConfig" # line 92, column 1.
klocworkQualityGateway([enableCiGateway: false, enableServerGateway: true, gatewayServerConfigs: [[conditionName: 'Issues', jobResult: 'failure', query: 'state:+Status,Fix', threshold: '1']]])
I really cannot find a way to make it work, and I guess I can take a wrong turn... so any help would be appreciate.
Thanks for your help and best regards
J-L
Well, after a fruitful discussion with the plugin maintainer (M. Baron) it appears that there is currently no simple and direct solution to display Klocwork result on a pipeline job page.
He said :
This step doesn't have a native pipeline interface and a few people
have tried, but haven't had much success with workarounds to use this
in a pipeline.
The simplest thing to do seems to trigger a freestyle job that will only do that.
As far as I have understood, a new plugin version with full pipeline support will replace the current one.
So, I think this discussion can be closed.

Email-Ext objects in groovy script as file

My question is kind of a follow up of this:
How to place Email-Ext groovy script on the jenkins file system
Here is my situation:
I have a groovy script which constructs the email which will be send.
This works fine as long as I have the script directly written (the code) in:
Pre-send Script
If I take this script, place it in the Jenkins filesystem(...jenkins\email-templates) as: email-presend.groovy and I try to call it with:
<presendScript>${SCRIPT, script="email-presend"}</presendScript>
I get the error message, that I can't access the message object:
Script1.groovy: 1: expecting EOF, found 'or' # line 1, column 17.
Error in script or template: groovy.lang.MissingPropertyException: No such property: msg for class: Script1
Code on the line:
msg.addHeader("X-Priority", "1 (Highest)");
msg.addHeader("Importance", "High");
Am I missing something obvious, since I can't find any code snippet which did this?
I want to use the javax.mail.Message msg which is available if the code is directly in jenkins.
Unfortunately it seems like it's not possible according to this post:
Email-ext comment
Too bad :(

JSLint : how to use it?

I've understood that JSLint is a great tool for JavaScript development but I've some dark points in my global comprehension of this.
How to use it inside my development environment ? In other words how and when do you run JSLint against your code ? I've seen the Aptana integration but it seems that it doesn't take into account statements like :
/* jslint nomen: false */
How to work correctly in a client side development environment ? I want JSLint to feel good when parsing calls including objects like "console", "$" or "JQuery".
How to use it on a whole project with multiple files (with no import statement inside of them) ?
I've read to much statement suggesting to copy paste on jslint to sleep correctly, so any answer consisting of configuring the online JSLint form would be considered as irrelevant.
I'm sorry to write this answer which is not really one. The best solution I've found is to use JSHint which is a concurrent to JSLint with some nice extra features :
Installation is made easy through NPM with command like (also works for JSLint), NPM is required :
npm install -g jshint
Execution is made easy against a lot of file (doesn't work for JSLint) :
jshint mycodedirectory
Configuration is possible through the --config options, config files look like :
{
"curly":true,
"eqeqeq": true,
"immed": true,
"bitwise": true,
"newcap": true,
"noempty": true,
"unused": true,
"camelcase":true,
"undef": true,
"strict": true,
"trailing": true,
"maxparams": 7,
"maxdepth": 5,
"maxstatements": 50,
"maxcomplexity": 13
}
This solution works for both browser and server code, it's IDE and OS independent, it can be easily integrated in continuous integration process.
Some one came up with a solution to run it automatically on your project using node.js:
Automating JSLint Validation
I use (a slightly modified) JSLINT for WSH on Emacs/WinXP. It highlights problematic code right while I am typing:
This is of great help not only to enforce the configured coding style, but also to find many JavaScript syntax errors before executing code.
It does respect statements such as /* jslint nomen: false */.
As you are asking concerning linting code that runs in a browser environment with jQuery, simply use standard JSLint options:
/*jslint browser: true */
/*global $ */
Just be sure to replace the included JSLint code with the latest version.

Resources