Getting any other job’s status within Jenkins declarative pipeline
What is a pipeline:
It is a way to define the Jenkins workflow as code or in simple words instead of creating freestyle jobs and chaining them through post-build action. We can create the entire pipeline using code written in groovy
What is groovy?
Groovy is powerful, optionally typed and dynamic language to develop an application on Java Platform where its syntax is Java-like. Its typing discipline is strong, static and dynamic. The best things about Groovy are that since it extends JDK, it accepts Java code. Groovy can be used as both programming and scripting Language. Groovy is a superset of Java which means Java program will run in Groovy environment but vice-versa may or may not be possible. Whereas Java is strongly and statically typed programming language. Reference : geeksforgeeks.org
Difference between the declarative pipeline and scripted pipeline?
Though both these pipelines are based on the groovy DSL, the scripted pipeline uses stricter groovy based syntaxes because it was the first pipeline to be built on the groovy foundation. Since this Groovy script was not typically desirable to all the users, the declarative pipeline was introduced to offer a simpler and more optioned Groovy syntax. Declarative pipeline is a new introduction to Jenkins world.
How to get the result of a job?
Jenkins is built on java and provides methods to access the Jenkins objects from the execution JVM, one of such method is .getItemByFullName()
This will return the full object of the Item with the name provided
So if we need to get java object (which will have all attributes including result) of a job called “Test Job” . We can get it as jenkins.model.Jenkins.instance.getItemByFullName(“Test Job”)
Now from this object we have to find the job.getLastBuild().getResult().toString()
This will give the status of the last build of that particular Job
But how to run this is Declarative pipeline?