Power assertions work in the /script, but do not work in a Jenkinsfile driven job. Why? Is there a way to get it to work?
In Jenkinsfile job:
assert 1 == 2
at org.codehaus.groovy.runtime.InvokerHelper.assertFailed(InvokerHelper.java:404)
... wall of stack trace
In /script window
Assertion failed:
assert 1 == 2
|
false
In this trivial example, it is easy to figure out what is going on. In practice, one or both of the operands of "==" will be a variable. In the /script version, it will display the values (see the link above). In the Jenkinsfile console log you just get the assert statement as is with no hints.
Added after @daggett question:
node ()
{
stage('assert')
{
try
{
two = 2
assert 1==two
}
catch(Throwable t)
{
println t
error "assert failed"
}
}
}
output:
Assertion failed:
assert 1==two
Not possible as far as I can see. Jenkins pipeline groovy DSL uses custom CPS interpreter during interpretation of groovy. This means that it bypasses/overrides a lot of the standard implementation of groovy, and thus also the assert implementation. The assert implementation for Jenkins Pipeline CPS can be found here, while the real groovy implementation uses this class during the assert evaluation in order to record values and print proper exception.
In order to get similar behaviour in Jenkins pipeline CPS one would either need to refactor the groovy code base and Jenkins CPS code or duplicate a lot of the functionality in the AssertionWriter class linked above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With