Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins.instance.getItem fails for jobs in folders

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?

like image 414
PortMan Avatar asked Dec 10 '22 02:12

PortMan


1 Answers

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
like image 170
Unforgettable631 Avatar answered Dec 26 '22 11:12

Unforgettable631