Jenkins

Get the List of Jobs in Jenkins

In Jenkins, a job refers to a specific task that can be run manually or is automated to run on a schedule. Jobs are the main building block on Jenkins pipelines and allow Jenkins to perform the tasks such as building and testing the software, creating artifacts, deploying applications, and many more.

Jenkins supports various job types such as pipelines, multi-configuration projects, freestyle projects, and many more.

In this tutorial, we will learn how to get the list of running Jobs in the Jenkins node using the Groovy scripts.

Setting Up a Sample Job

Before we discuss how we can get the list of Jobs in Jenkins, we need to set up and configure a sample job for demonstration purposes.

For this tutorial, we will use a simple Go WebSocket chat application. Check our tutorial on Go WebSockets to discover more.

The first step is to login into the Jenkins dashboard:

http://localhost:8080

Select “New Item” in the Jenkins dashboard to set up a new pipeline.

Next, set the name of the Pipeline that you wish to assign and choose the project type as “Pipeline”.

Finally, click OK to configure your build application.

Enter the details of the job, including the description and build triggers.

NOTE: Ensure that the source code for your application is located in the Jenkins workspace directory. This may vary depending on your node and configuration.

Next, select the script in the Pipeline section and paste the following script code:

pipeline {
    agent any
    tools {
        go 'go1.19.4'
    }
    environment {
        GO111MODULE='on'
        CGO_ENABLED = 0
        GOPATH = "${JENKINS_HOME}/jobs/${JOB_NAME}/builds/${BUILD_ID}"
    }
    stages {
        stage('Pre-Testing') {
            steps {
                echo 'Setting up dependencies'
                bat 'go version'
                bat 'go get -u github.com/gorilla/websocket'
            }
        }
        stage ("unit-testing") {
            steps {
                echo 'Unit Testing initiated...'
                bat 'go test ./...'
            }

        }
        stage ('Build') {
            steps {
                echo "Compilation and Build Started"
                bat "go build"
            }
        }
    }
}

Once completed, click on “Save” to save the Pipeline.

Finally, select “Build Now” on the left-hand pane to initiate the build process.

This should begin a build process as shown in the following status:

Get the List of Jobs in Jenkins

To show the list of Jobs in Jenkins, navigate to the Jenkins Dashboard -> Manage Jenkins -> Script Console.

In the script console, run the following script to get the names of all the jobs in the Jenkins node:

Jenkins.instance.getAllItems(AbstractItem.class).each {
    println it.fullName + " - " + it.class
};

Click run to execute the script.

This should return a list of the jobs in Jenkins as shown in the following output:

The previous command prints the job name and other details including the invoked plugins.

To exclude the directory name from the job name, run the following script:

Jenkins.instance.getAllItems(Job.class).each{
    println it.name + " - " + it.class
}

Output:

To only show the multibranch jobs, run the following script:

Jenkins.instance.getAllItems(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject).each {it ->
    println it.fullName;
}

Output:

Conclusion

You discovered how to find the jobs in a given Jenkins instance using the Jenkins Script Console and various Groovy scripts.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list