Tuesday, July 26, 2011

List all jenkins jobs with a perforce scm configuration

Here is an instruction on how to execute this groovy script on your jenkins instance to display all jobs that use perforce as SCM provider. This was one of the first scripts I wrote to get an idea about the jenkins CLI and how to execute custom scripts. This script can be used to examine your perforce configurations used for your jobs. In my case it was the foundation to change all passwords for certain perforce user.

You need to have a java installation on your machine and java must be available on your PATH. groovy itself is not required to execute the scripts.
I assume that you have Jenkins running on your machine, so that I refer to localhost to access the Jenkins instance.
  1. If you already have a copy of jenkins-cli.jar on your machine, then skip to step 2. Otherwise open a browser and navigate to this URL http://localhost:8080/cli. Follow the instructions and download the jenkins-cli.jar to a known directory
  2. Open a terminal or command window and change into the directory where your jenkins-cli.jar is located
  3. Type java -jar jenkins-cli.jar -s http://localhost:8080 help. This will output a list of all available commands that this jenkins instance provides.
  4. Okay, now go ahead and save this snippet as listAllProjectsWithPerforceSCM.groovy to the same directory where you previously downloaded the jenkins-cli.jar
  5. Now enter the following on the command line:
    java -jar jenkins-cli.jar -s http://localhost:8080 groovy listAllProjectsWithPerforceSCM.groovy
This will show you something similar to this output:

Job 'Test' uses the following perforce configuration
P4Port: localhost:1666
P4Client: testbuilder
P4User: testbuilder
P4Password: 0f0kqlwaDeXrEj0PA0z/+IXZM1f8G8QsgBlUgnUv8bbR2bzXLfa3AlrK8xqw==

That's it. There is nothing complicated about this script, but it shows some of the capabilities to automate certain tasks. Take the script as example to play around with it and explore new ways to interact with jenkins.

Thanks for reading this post.


yamex5 said...
This comment has been removed by the author.
yamex5 said...

Thank you for the information, but I do have a question. Hudson and the hudson cli are installed as well as groovy (I have run cli and groovy commands from a terminal) but I am getting syntax errors. I am new to groovy, so it's likely an error on my part.
The interpreter complains on the very first line: "No such property: hudson for class: listjobs" where listjobs.groovy is the name of my script.

Thank you in advance for any help!
Mike L.

Ingo said...

I'm sorry to hear that it's not working for you.

First of all, you don't need groovy to execute groovy scripts in the jenkins cli, since groovy is part of jenkins.

Are you using Jenkins or Hudson? I used Jenkins for this example and I never tried Hudson.

Can you send me your groovy script? I'm going to have a look at it.