Trigger a build via API and know for sure, the resulting build number

66 views
Skip to first unread message

andrew...@gmail.com

unread,
Jul 20, 2015, 1:14:44 PM7/20/15
to jenkin...@googlegroups.com
Hello,
I have a Jenkins job that gets triggered programatically by the API and I need to get that resulting build number. 
My current process is to get the next build number, trigger a build, then I keep polling Jenkins to determine when it has finished.

The problem arises when I want to this job to be triggered by more than one API call.
Multiple API calls could call at the same time, get the same next build number, and wait for the build even though they have different triggered build numbers.

Are there any good ways around this?

The best solution I can think of right now is to generate a random string to be used as an ID. Post that ID as a parameter than use the API to find which build has that matching ID. Thoughts?


Thank you

Victor Martinez

unread,
Jul 21, 2015, 5:18:52 AM7/21/15
to jenkin...@googlegroups.com
Hi Andrew,

 Have you checked out the Jenkins CLI? It does provide another way of launching builds and wait for them:

java -jar jenkins-cli.jar -s http://jenkins.com/ build JOB [-c] [-f] [-p] [-r N] [-s] [-v] [-w]
Starts a build, and optionally waits for a completion.
Aside from general scripting use, this command can be
used to invoke another job from within a build of one job.
With the -s option, this command changes the exit code based on
the outcome of the build (exit code 0 indicates a success)
and interrupting the command will interrupt the job.
With the -f option, this command changes the exit code based on
the outcome of the build (exit code 0 indicates a success)
however, unlike -s, interrupting the command will not interrupt
the job (exit code 125 indicates the command was interrupted)
With the -c option, a build will only run if there has been
an SCM change

JOB : Name of the job to build -c : Check for SCM changes before starting the build, and if there's no change, exit without doing a build -f : Follow the build progress. Like -s only interrupts are not passed through to the build. -p : Specify the build parameters in the key=value format. -s : Wait until the completion/abortion of the command. Interrupts are passed through to the build. -v : Prints out the console output of the build. Use with -s -w : Wait until the start of the command

Further reading: 
  <YOUR_JENKINS_URL>/cli/command/build


Cheers

andrew...@gmail.com

unread,
Jul 22, 2015, 1:27:35 PM7/22/15
to Jenkins Developers
This looks very promising. Thank you, I will try this out!

andrew...@gmail.com

unread,
Jul 22, 2015, 2:58:38 PM7/22/15
to Jenkins Developers
This should work perfectly! Thank you!

Jesse Glick

unread,
Jul 27, 2015, 2:45:49 PM7/27/15
to Jenkins Dev
On Mon, Jul 20, 2015 at 1:14 PM, <andrew...@gmail.com> wrote:
> The best solution I can think of right now is to generate a random string to
> be used as an ID. Post that ID as a parameter than use the API to find which
> build has that matching ID. Thoughts?

Run.queueId (in newer Jenkins versions) exists for this purpose
already. The HTTP response from scheduling the build specifies the
queue ID.
Reply all
Reply to author
Forward
0 new messages