Cannot access recordIssues in Jenkins Pipeline using Warnings Next Generation - jenkins

I have a simple Jenkinsfile with a recordIssues step. The relevant code looks like this:
step([
$class: 'recordIssues',
aggregatingResults: true,
enabledForFailure: true,
tools: [pyLint()]
])
I have installed the latest version of the Warnings Next Generation plugin (https://plugins.jenkins.io/warnings-ng) but I run into the following issue:
[Pipeline] step
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
java.lang.UnsupportedOperationException: no known implementation of interface jenkins.tasks.SimpleBuildStep is named recordIssues
at org.jenkinsci.plugins.structs.describable.DescribableModel.resolveClass(DescribableModel.java:478)
Is it somehow possible to check that the extension is installed correctly?

This is my working version for a Python project CI configuration in Jenkins to use JUnit, PEP8, Pylint and Coverage reports:
...
stage('Test: Run') {
steps {
// Run my project tests.
sh 'coverage run manage.py tests'
// Dump coverage metrics to XML.
sh 'coverage xml'
// Run Pylint.
sh 'pylint --rcfile=.pylintrc my_project > reports/pylint.report'
// Run Pycodestyle (PEP8 checks).
sh 'pycodestyle my_project > reports/pep8.report'
}
post {
always{
// Generate JUnit, PEP8, Pylint and Coverage reports.
junit 'reports/*junit.xml'
recordIssues(
tool: pep8(pattern: 'reports/pep8.report'),
unstableTotalAll: 200,
failedTotalAll: 220
)
recordIssues(
tool: pyLint(pattern: 'reports/pylint.report'),
unstableTotalAll: 20,
failedTotalAll: 30
)
cobertura coberturaReportFile: 'reports/coverage.xml'
}
}
}
...
It works with Cobertura plugin, JUnit plugin and Warnings Next Generation. Python packages I used are the traditional coverage and pylint and for PEP8 I used pycodestyle.
Hope this helps somebody else, as finding good examples of this Jenkinsfile stuff is not easy.

Just for the record:
Jenkins v2.204.2
Jenkins Warnings Next Generation Plugin v8.0.0
stage('Static Analysis') {
steps {
recordIssues(
tool: pyLint(pattern: '**/pylint.out'),
unstableTotalAll: 100,
)
}

This works for me (Jenkins ver. 2.164.1):
stage('Static Analysis') {
recordIssues(
tool: pyLint(pattern: '**/pylint.out'),
unstableTotalAll: '100',
)
recordIssues(
tool: pep8(pattern: '**/pep8.out'),
unstableTotalAll: '100',
)
}

Related

i am getting [0m[[0m[0minfo[0m] [0m[0m in jenkins console on building sbt jenkins pipeline project

i have an sbt project in jenkins, created the project as jenkins pipeline project, i have installed sbt in jenkins, i have checked the auto install checkbox and select the version number 1.2.8
here is my jenkins file
pipeline {
agent any
stages {
stage('Reload') {
steps {
echo "Reloading..."
//sh "sbt reload"
sh "${tool name: 'sbt1.2.8', type: 'org.jvnet.hudson.plugins.SbtPluginBuilder$SbtInstallation'}/bin/sbt compile"
}
}
}
}
and here is sbt settings in jenkins
here is jenkins console logs
+ /var/lib/jenkins/tools/org.jvnet.hudson.plugins.SbtPluginBuilder_SbtInstallation/sbt1.2.8/bin/sbt compile
[0m[[0m[0minfo[0m] [0m[0mLoading settings for project interpret-backend-project-jenkinsfile-build from plugins.sbt ...[0m
[0m[[0m[0minfo[0m] [0m[0mLoading project definition from /var/lib/jenkins/workspace/interpret-backend-project-jenkinsfile/project[0m
[0m[[0m[0minfo[0m] [0m[0mLoading settings for project interpret-backend-project-jenkinsfile from build.sbt ...[0m
[0m[[0m[0minfo[0m] [0m[0mSet current project to interpret (in build file:/var/lib/jenkins/workspace/interpret-backend-project-jenkinsfile/)[0m
[0m[[0m[0minfo[0m] [0m[0mExecuting in batch mode. For better performance use sbt's shell[0m
[0m[[0m[32msuccess[0m] [0m[0mTotal time: 4 s, completed Nov 25, 2020, 4:53:05 PM[0m
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
[Checks API] No suitable checks publisher found.
Finished: SUCCESS
[0m[[0m[0minfo[0m] [0m[0m why is this displaying how can i fix this ?
These are ANSI color codes. You can either embrace them to have colorful logs or disable them.
To enable colors in Jenkins you can modify your pipeline definition:
pipeline {
...
options {
ansiColor('xterm')
}
...
}
This is a reference to the AnsiColor plugin.
If you can't use this plugin and want to disable colors in sbt logs, you can do that by modifying sbt.color option. For example, by launching sbt with -Dsbt.color=false (I see that you can add this to in the UI) or by adding it to the SBT_OPTS environment variable:
pipeline{
...
environment {
SBT_OPTS = "${SBT_OPTS} -Dsbt.color=false"
}
...
}
Check out sbt docs and take a look at the sbt.ci option as well, it should be automatically set on Jenkins.

How do I fail a Jenkins pipeline with below a certain PHPUnit code coverage percentage?

I am using a Jenkins declarative pipeline to implement continuous integration with my code. The code is a PHP project that uses PHPUnit for unit testing.
Currently, the pipeline
builds the project
runs a code sniffer to find coding violations
runs PHPUnit tests, generating coverage reports and publishing them
If any PHPUnit test fails, the Jenkins job fails (which is good).
Now, I also want to fail the job if the code coverage percentage is below a certain amount. I can't find how to do so.
The current Jenkinsfile looks like this:
pipeline {
agent any
stages {
stage ('Build') {
steps {
sh '''
cd symfony && composer install --prefer-dist
./vendor/bin/pcov clobber
'''
}
}
stage('PHP CodeSniffer') {
steps {
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
sh './symfony/vendor/bin/phpcs-meq.sh ./symfony/src/AppBundle'
}
}
}
stage('PHPUnit Tests') {
steps {
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
sh '''
cd symfony
cp phpunit.xml.dist phpunit.xml
./bin/console cache:warmup --env=test
./vendor/bin/phpunit\
--coverage-clover '../reports/coverage/coverage.xml'\
--coverage-html '../reports/coverage'\
--log-junit '../reports/unitreport.xml'
'''
}
junit 'reports/unitreport.xml'
publishHTML([
allowMissing: true,
alwaysLinkToLastBuild: false,
keepAll: true,
reportDir: 'reports/coverage',
reportFiles: 'index.html',
reportName: 'PHPUnit Test Coverage Report'
])
}
}
}
}
I have found this snippet, which publishes the clover report. I thought that perhaps making the "failingTarget" high enough would exit 1 (fail the pipeline) if the coverage didn't make the requirement. It doesn't -- it outputs a warning, but the step still succeeds.
step([
$class:'CloverPublisher',
cloverReportDir: '../reports/coverage',
cloverReportFileName: 'coverage.xml',
healthyTarget: [methodCoverage: 90, conditionalCoverage: 90, statementCoverage: 90],
unhealthyTarget: [methodCoverage: 89, conditionalCoverage: 89, statementCoverage: 89],
failingTarget: [methodCoverage: 88, conditionalCoverage: 88, statementCoverage: 88]
])
How can I fail a Jenkins pipeline if my PHP code coverage is below a certain threshold?
Is xml the only file format available to output the coverage report in?
I would focus on reading the file and then evaluating the necessary elements to determine whether you want to throw an error.
for XML, that would involve importing groovy.util.XmlSlurper, reading the file, parsing it and then navigating the structure.
JSON would be easier, could just use readJSON jenkins basic step and then access the map.

Modify env.BRANCH_NAME variable with branch-env.BRANCH_NAME in jenkinsfile for a multibranch pipeline project

I have created a multibranch pipeline project and thus created jenkinsfile and put that in dev branch.
In one of the stage, I have to run mvn sonar:sonar -Dsonar.scm.branch=branch-${env.BRANCH_NAME} but it's giving error as bad substitution branch-${env.BRANCH_NAME}.
I need branch-${env.BRANCH_NAME} as a branch-name so that at sonar i can see branch-dev at branches section in sonar dashboard.
if i use mvn sonar:sonar -Dsonar.scm.branch=env.BRANCH_NAME then it provides output but it will act as short-lived branch in sonar. but at sonar we want branch as long-lived branch.
!/usr/bin/env groovy
pipeline {
agent { label 'ol73_slave-jdk8u192-git' }
options {
timestamps()
timeout(time: 2, unit: 'HOURS')
buildDiscarder(logRotator(numToKeepStr: '10'))
disableConcurrentBuilds()
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Unit Test and Code Scan') {
steps {
echo "*****JUnit Tests, JaCoCo Code Coverage, & SonarQube Code Qualiy Scan*****"
withMaven(jdk: 'jdk8_u192', maven: 'maven-3.3.9', mavenSettingsConfig: '79ecf9bd-8cbc-4d5e-b7d1-200241e16b52') {
sh '''
cd DARC
mvn clean package sonar:sonar -Dsonar.host.url=***** -Dsonar.login=******* -Dsonar.exclusions=file:**/src/test/** -B -Pcoverage -Dsonar.branch.name=branch-${env.BRANCH_NAME}
'''
}
}
}
}
}
In groovy Strings are encapsulated in single quotes '' and GStrings in double quotes ""
In order to do interpolation you need to be using GStrings. In your example, this should simply be
sh """
cd DARC
mvn clean package sonar:sonar -Dsonar.host.url=***** -Dsonar.login=******* -Dsonar.exclusions=file:**/src/test/** -B -Pcoverage -Dsonar.branch.name=branch-${env.BRANCH_NAME}
"""

No such DSL method 'publishHTML' found among steps

I have a jenkins DSL step that runs my python nosetests and creates a unittest coverage report.
Here is my jenkins stage.
stage ('Unit Tests') {
steps {
sh """
#. venv/bin/activate
export PATH=${VIRTUAL_ENV}/bin:${PATH}
make unittest || true
"""
}
post {
always {
junit keepLongStdio: true, testResults: 'report/nosetests.xml'
publishHTML target: [
reportDir: 'report/coverage',
reportFiles: 'index.html',
reportName: 'Coverage Report - Unit Test'
]
}
}
}
I get this error -
java.lang.NoSuchMethodError: No such DSL method 'publishHTML' found among steps.
How can I fix this error? I got this piece of code from a different repository.
The publishHTML method is provided by the HTLMPublisher Jenkins plugin. After installing the plugin on the primary Jenkins server, the publishHTML method will be available in the Jenkins Pipeline for both scripted and declarative syntax.

How do I use the report plugin on (PMD, PHPCPD, checkstyle, Jdepend...) in a Jenkins pipeline?

I'm using Jenkins 2.x with a Jenkinsfile to run a pipeline.
I have built a job using Jenkinsfile and I want to invoke the Analysis Collector Plugin so I can view the report.
Here is my current Jenkinsfile:
#!groovy
node {
stage 'Build '
echo "My branch is: ${env.BRANCH_NAME}"
sh 'cd gitlist-PHP && ./gradlew clean build dist'
stage 'Report'
step([$class: 'JUnitResultArchiver', testResults: 'gitlist-PHP/build/logs/junit.xml'])
step([$class: 'hudson.plugins.checkstyle.CheckStylePublisher', checkstyle: 'gitlist-PHP/build/logs/phpcs.xml'])
step([$class: 'hudson.plugins.dry.DryPublisher', CopyPasteDetector: 'gitlist-PHP/build/logs/phpcpd.xml'])
stage 'mail'
mail body: 'project build successful',
from: 'siregarpandu#gmail.com',
replyTo: 'xxxx#yyyy.com',
subject: 'project build successful',
to: 'siregarpandu#gmail.com'
}
I want to invoke invoke Checkstyle, Junit and DRY plugin from Jenkins. How do I configure these plugins in the Jenkinsfile? Do these plugins support pipelines?
The following configuration works for me:
step([$class: 'CheckStylePublisher', pattern: 'target/scalastyle-result.xml, target/scala-2.11/scapegoat-report/scapegoat-scalastyle.xml'])
For junit configuration is even easier:
junit 'target/test-reports/*.xml'
step([$class: 'hudson.plugins.checkstyle.CheckStylePublisher', checkstyle: 'gitlist-PHP/build/logs/phpcs.xml'])
Also according to source code repo, the argument 'checkstyle' should be named 'pattern'.
Repo:
https://github.com/jenkinsci/checkstyle-plugin/blob/master/src/main/java/hudson/plugins/checkstyle/CheckStylePublisher.java#L42
This is how I handle this:
PMD
stage('PMD') {
steps {
sh 'vendor/bin/phpmd . xml build/phpmd.xml --reportfile build/logs/pmd.xml --exclude vendor/ || exit 0'
pmd canRunOnFailed: true, pattern: 'build/logs/pmd.xml'
}
}
PHPCPD
stage('Copy paste detection') {
steps {
sh 'vendor/bin/phpcpd --log-pmd build/logs/pmd-cpd.xml --exclude vendor . || exit 0'
dry canRunOnFailed: true, pattern: 'build/logs/pmd-cpd.xml'
}
}
Checkstyle
stage('Checkstyle') {
steps {
sh 'vendor/bin/phpcs --report=checkstyle --report-file=`pwd`/build/logs/checkstyle.xml --standard=PSR2 --extensions=php --ignore=autoload.php --ignore=vendor/ . || exit 0'
checkstyle pattern: 'build/logs/checkstyle.xml'
}
}
JDepend
stage('Software metrics') {
steps {
sh 'vendor/bin/pdepend --jdepend-xml=build/logs/jdepend.xml --jdepend-chart=build/pdepend/dependencies.svg --overview-pyramid=build/pdepend/overview-pyramid.svg --ignore=vendor .'
}
}
The full example you can find here: https://gist.github.com/Yuav/435f29cad03bf0006a85d31f2350f7b4
Reference links
https://jenkins.io/doc/pipeline/steps/
It appears that the plugins need to be modified to support working as Pipeline Steps, so if they have not been updated, they don't work.
Here is a list of compatible plugins that have been updated:
https://github.com/jenkinsci/pipeline-plugin/blob/master/COMPATIBILITY.md
And here is the documentation about how the plugins need to be updated to support Pipelines:
https://github.com/jenkinsci/pipeline-plugin/blob/master/DEVGUIDE.md

Resources