28

I want to use PyLint on Jenkins with Warnings Plugin and Pipeline, since Violations plugin is deprecated.

There are no docs or complete examples.

There is some information:

timeout(time: 5, unit: 'MINUTES') {
  sh 'npm run lint:ci'
  step([$class: 'WarningsPublisher',
    parserConfigurations: [[
      parserName: 'JSLint',
      pattern: 'pmd.xml'
    ]],
    unstableTotalAll: '0',
    usePreviousBuildAsReference: true
  ])
}

and workarounds:

pylint || exit 0

Is there a more robust solution?

3
  • The graphs and tables that are displayed are all basic and older style, have you had any luck getting current graphing to work with your configuration ? the ui in Jenkins does not display like this for me : wiki.jenkins.io/display/JENKINS/Warnings+Plugin Commented Nov 28, 2018 at 20:46
  • sorry, I haven't used it for a while now Commented Dec 5, 2018 at 10:13
  • 2
    The original Warnings Plugin has been superseded by Warnings Next Generation
    – Emil
    Commented Jan 4, 2021 at 23:53

4 Answers 4

27

I've managed to get it working:

sh 'pylint --disable=W1202 --output-format=parseable --reports=no module > pylint.log || echo "pylint exited with $?")'
sh 'cat render/pylint.log'

step([
        $class                     : 'WarningsPublisher',
        parserConfigurations       : [[
                                              parserName: 'PYLint',
                                              pattern   : 'pylint.log'
                                      ]],
        unstableTotalAll           : '0',
        usePreviousBuildAsReference: true
])

I'm still not sure how to configure it.

From what I was able to read from the source code and tests, those might be the possible parameters because they are the constructor parameters:

  • healthy - Report health as 100% when the number of annotations is less than this value
  • unHealthy - Report health as 0% when the number of annotations is greater than this value
  • thresholdLimit - determines which warning priorities should be considered when evaluating the build stability and health
  • defaultEncoding - the default encoding to be used when reading and parsing files
  • useDeltaValues - determines whether the absolute annotations delta or the actual annotations set difference should be used to evaluate the build stability
  • unstableTotalAll - annotation threshold
  • unstableTotalHigh - annotation threshold
  • unstableTotalNormal - annotation threshold
  • unstableTotalLow - annotation threshold
  • unstableNewAll - annotation threshold
  • unstableNewHigh - annotation threshold
  • unstableNewNormal - annotation threshold
  • unstableNewLow - annotation threshold
  • failedTotalAll - annotation threshold
  • failedTotalHigh - annotation threshold
  • failedTotalNormal - annotation threshold
  • failedTotalLow - annotation threshold
  • failedNewAll - annotation threshold
  • failedNewHigh - annotation threshold
  • failedNewNormal - annotation threshold
  • failedNewLow - annotation threshold
  • canRunOnFailed - determines whether the plug-in can run for failed builds, too
  • usePreviousBuildAsReference - determines whether to always use the previous build as the reference build
  • useStableBuildAsReference - determines whether only stable builds should be used as reference builds or not
  • canComputeNew - determines whether new warnings should be computed (with respect to baseline)
  • shouldDetectModules - determines whether module names should be derived from Maven POM or Ant build files
  • includePattern - Ant file-set pattern of files to include in report
  • excludePattern - Ant file-set pattern of files to exclude from report
  • canResolveRelativePaths - determines whether relative paths in warnings should be resolved using a time expensive operation that scans the whole workspace for matching files.
  • parserConfigurations - the parser configurations to scan files
  • consoleParsers - the parsers to scan the console

And the parserConfigurations javadoc says only:

  • pattern - the pattern of files to parse
  • parserName - the name of the parser to use

where the list of the parsers seams to be here.

If you have more information or something needs correcting feel free to edit or drop a comment.

3
  • messagesPattern - warning messages to exclude from report Commented Apr 20, 2017 at 3:55
  • 3
    It's incredible how hard it is to find docs on this topic. Thanks a lot for posting this Pawel.
    – Pablo M
    Commented Dec 5, 2018 at 9:57
  • 1
    instead of redirecting output to file and then cating it to console, you can just use | tee pylint.log which will do the same.
    – rkosegi
    Commented Aug 22, 2019 at 5:02
13

Note that an alternative to || exit 0 or || echo "failed" (which are fine) is to use pylint --exit-zero:

--exit-zero         Always return a 0 (non-error) status code, even if
                    lint errors are found. This is primarily useful in
                    continuous integration scripts.
1
  • 1
    --exit-zero option available from Pylint 1.9.3 Commented Mar 22, 2019 at 9:53
9

For Declarative Pipeline Syntax

sh 'python3 -m pylint --output-format=parseable --fail-under=<threshold value> module --msg-template="{path}:{line}: [{msg_id}({symbol}), {obj}] {msg}" | tee pylint.log || echo "pylint exited with $?"'
echo "linting Success, Generating Report"
recordIssues enabledForFailure: true, aggregatingResults: true, tool: pyLint(pattern: 'pylint.log')

It will break the build if the pylint rate is less then the threshold value. And using Warnings Next generation plugin it will create graphs like below

enter image description here

1
  • This is working for the most part for me. But it isn't failing the build if the threshold is below. sh 'poetry run pylint --output-format=parseable --fail-under=9.5 ./src --msg-template="{path}:{line}: [{msg_id}({symbol}), {obj}] {msg}" | tee pylint.log || echo "pylint exited with $?"' echo "Linting Success, Generating Report" recordIssues enabledForFailure: true, aggregatingResults: true, tool: pyLint(pattern: 'pylint.log') My output from Pylint: Your code has been rated at 9.48/10 Do you have any suggestions?
    – itcoder
    Commented May 10, 2022 at 21:35
1

This worked for me:

   stage('Static code analysis'){
        steps {
           sh 'PYTHONPATH=path/to/extended pylint -f parseable --reports=no * > pylint.log' //remove pythonpath if not needed in your environment
        }
        post {
            always {
                sh 'cat pylint.log'
                    recordIssues healthy: 1, tools: [pyLint(name: 'report name', pattern: '**/pylint.log')], unhealthy: 2
            }
        }
    }

Jenkins was using this plugin https://plugins.jenkins.io/warnings-ng/ for the recordIssues step

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.