Jenkins with pylint gives build failure

后端 未结 8 1131
天命终不由人
天命终不由人 2021-01-04 12:25

I added a build step to execute a Python script.
In this script pylint is called with the lint.Run(..args) to check the code.
The script works but in the end, the bu

8条回答
  •  离开以前
    2021-01-04 12:40

    I agree with @dmeister, but with pipeline code (Jenkinsfile) I suggest a try/catch and then parsing the error. This way you can determine if you're just getting status bits from pylint (see the Pylint docs), whether pylint reports a usage error, or whether there was a catastrophic fail:

    try {       
        sh 'pylint --output-format=parseable my_module'
    } catch ( pylint_rc ) {
        // pylint_rc will be of the form
        // "hudson.AbortException: script returned exit code NN"
        // where NN is 1-63 and represents bit field;
        // bits 0-4 indicate lint-ish issues in analyzed code,
        // bit 5 indicates pylint usage error
        echo "pylint_rc= \'$pylint_rc\'"
        String rc = "$pylint_rc"
        String code = rc.split()[5]
        echo "Isolated return code string value $code"
        int value = code.toInteger()
    
        // catastrophic/crash error returns a 1; else there is a pylint return code
        int error_bits_code = value & 0x20
        int lint_bits_code = value & 0x1f
        echo "pylint error_bits_code=$error_bits_code ; lint_bits_code=$lint_bits_code"
        if ( (value == 1) || (error_bits_code != 0) ) {
            currentBuild.result = "FAILURE"
            throw pylint_rc
        }
    }
    

    Apologies to groovy purists - groovy isn't my thing, so I'm sure this can be improved on - let me know. There is one known hole: if pylint detects only "fatal"-type errors (bit 0) and no other issues of any kind (bits 1-4 are not set) then this code will incorrectly throw an exception. But my code flags tons of issues, so that's not a problem for me. The fix (?parse error msg?) might be trivial for someone with groovy chops.

提交回复
热议问题