Use PyLint on Jenkins with Warnings Plugin and Pipeline

17,470

Solution 1

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.

Solution 2

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.

Solution 3

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

Share:
17,470

Related videos on Youtube

Paweł Prażak
Author by

Paweł Prażak

Software Engineer particularly interested in distributed computing and security.

Updated on September 15, 2022

Comments

  • Paweł Prażak
    Paweł Prażak over 1 year

    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?

    • user1967397
      user1967397 over 5 years
      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
    • Paweł Prażak
      Paweł Prażak over 5 years
      sorry, I haven't used it for a while now
    • Emil
      Emil over 3 years
      The original Warnings Plugin has been superseded by Warnings Next Generation
  • Denis The Menace
    Denis The Menace about 7 years
    messagesPattern - warning messages to exclude from report
  • Pablo M
    Pablo M over 5 years
    It's incredible how hard it is to find docs on this topic. Thanks a lot for posting this Pawel.
  • Suresh Kota
    Suresh Kota about 5 years
    --exit-zero option available from Pylint 1.9.3
  • rkosegi
    rkosegi over 4 years
    instead of redirecting output to file and then cating it to console, you can just use | tee pylint.log which will do the same.
  • itcoder
    itcoder almost 2 years
    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?