I can use Jenkins.instance.getItem('job_name')
to access a job by name, either in the script console or in a Jenkinsfile.
But I am not able to do it for multibranch pipelines or any other job that's in a folder. If I try using the project's full name (including folder) like Jenkins.instance.getItem('folder_name/job_name')
, I just get null
as a result.
How do I access jobs in folders?
You can use getItemByFullName
to access a job by name:
Jenkins.instance.getItemByFullName('folder_name/job_name')
. It may require an approval to use it in a Jenkinsfile.
This also opens up more possibilities by retrieving build statusses:
def buildName = Jenkins.instance.getItemByFullName('folder_name/job_name')
echo "Last success: ${buildName.getLastSuccessfulBuild()}"
echo "All builds: ${buildName.getBuilds().collect{ it.getNumber()}}"
echo "Last build: ${buildName.getLastBuild()}"
echo "Is building: ${job.isBuilding()}"
BUT, all of these may require an approval when using it via a Jenkinsfile. Output for example:
[Pipeline] echo
Last success: folder/job_name #761
[Pipeline] echo
All builds: [767, 766, 765, 764, 763, 762, 761, 760]
[Pipeline] echo
Last build: folder/job_name #767
[Pipeline] echo
Is building: true
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