Jenkins job submission via curl and get its build number - jenkins

How to submit the Jenkins job using curl api and get its buid number ?
Note - i tried with the following api, but its not printing any build number.
curl -X POST http://<hostname>:8080/job/<jobname>/build

In general, you can't do this: when you submit (or "trigger") a Jenkins job, this will not necessarily create and start a new build.
When triggering a job, only a request for starting a new build will enter the build queue. Depending on the availability of suitable executors, the actual build will be created (and started) immediately, later, or never at all. Also, multiple queued requests will normally be "squashed", so several submits will result in the same build number.
Bottom line: this can be done (with the constraints mentioned before), but it will require additional (Groovy) scripting for tracking the submit request through the build queue until the build will be started by Jenkins.

Related

Use Jenkins REST API to rebuild a build

I have a build in Jenkins which has failed for a temporary reason. I'd like to just re-build it. On the website I just click "rebuild" (from the rebuild plug-in), and if it is a parameterized job, I get asked if I want to keep the parameters. Importantly, the new job contains a "rebuild" link back to the original job enabling back-tracing.
How can I effect this "rebuild" from the REST API. I have code that will get a completed job, extract the parameters, and invoke a new job with those same parameters - so I've got most of the way there.
But I don't know how to establish the rebuild link so I can trace what job is a rebuild of what other job.
You can't -- the rebuild plugin does not implement a REST API (as of current version 1.25; I just checked the sources).
If your job has no parameters, then just GETing the <buildUrl>/rebuild URL should trigger a rebuild, though.
If your job does have parameters, then some more scripting will be needed to mimick what you do manually in those two steps.

Jenkins job wait for first successful build of other job

I have a Jenkins job that should not start building until another job has been built successfully at least once. They are not related per se, so I don't want to use triggers. Is there a way to do this?
Some background: I'm using SCM polling to trigger the second job. I've looked at the Files Found Trigger plugin, but that would keep on triggering the second job after the first on has been built. I've also found the Run Condition Plugin, but that seems to work only on build steps, not on the entire build.
Update - The second job copies artifacts from the first job. As long as the first job has never completed successfully, the Copy Artifact step fails. I am trying to prevent that failure, by not even attempting to build the second job until the first job has completed once.
One solution is to use the Build Flow plugin.
You can create a new flow job and use this DSL:
I've used 10 in the retry section but you can use any numbers.
This flow job can be triggered by monitoring the same SCM URL of your second job.
Update, here is a second solution.
You can use the HTTP Request plugin.
If you want to test that your first job has been built successfully at least once, you can test this URL:
http://your.jenkins.instance/job/your.job/lastSuccessfulBuild/
One example:
As my build has never been successful, the lastSuccessfulBuild URL doesn't exist. The HTTP Request changes my build status to failure.
Does it help?
The Block queued job plugin can be used for this: https://wiki.jenkins-ci.org/display/JENKINS/Block+queued+job+plugin

How to Remotely start jenkins build and get back result transactionally?

I had a request a to create a java client and start jenkins build for a specific job; and get back the result of that build.
The problem is, the system is used by multiple users and their build might messed up altogether. Also the get latest build my retrieve me the previous finished build instead of current one. Is there anyway to do build/get result transactionally?
I don't think there's a way to get true transactional functionality (in the way that, say, Postgres is transactional), however, I think you can prevent collisions amongst multiple users by doing the following:
Have your build wrapped around a script (bash, Python, or similar) which takes out an exclusive lock on a semfile before the build and releases it after its done. That is, a file which serves as a semaphore that the build process must be able to exclusively lock in order to be able to proceed.
That way, if you have a build in progress, and another user triggers one, the in-progress build will have the semfile locked, and the 2nd one will block waiting for the exclusive lock on that file, getting the lock only once the 1st build is complete and has released the lock on the file.
Also, to be able to refer to each remote build after the fact, I would recommend you refer to my previous post Retrieve id of remotely triggered jenkins job.

To get build status through environment variable

I am using jenkins for continous integration.
For my build purpose, i triggering email using Ant task. I am not able to find an environment variable to pass ant for sending email build status(success/failure/stable).
i want to know how can i get environment variable for build status?..if not available, what is the alternative option for build status?
Thanks in Advance
varghese
Why use ANT to send emails from Jenkins when you have two great plugins that does just that?
The default mail notification is quite good, and if you would like to have more control
I suggest using the Email-ext plugin which is very comprehensive.
If still wish to use ANT to sent your mail-notifications including the status
you will have to break your process into two steps,
where the first part runs the build and the second one runs the ANT script to report the status.
In this case, you will need to trigger the second job from the first job via the Parameterized Build plugin -
see my answer here:
trigger other configuration and send current build status with Jenkins
The build status is not set until the job has finished running, so there is no easy way to push build status to a process triggered within the build itself. You could pull build status via the API, but this would have to be an externally triggered process due to the constraint mentioned above. Any reason you aren't using the built in email support or one of the excellent email extension plugins such as this one?

Provide userName and shortDescription in Jenkins remote API job triggering

I know how to provide build parameters:
wget --post-data='json={"parameter": {"name": "testparam", "value": "HELLO"}}' http://jenkins/job/Job1/build?delay=0sec
But, is it possible to provide a shortDescription and userName in a Jenkins remote API build request via wget/curl?
How should it look like in json or xml? Is there any manual/guidance on the net?
I will use this in along with the problem described in Trigger dynamic set of jobs. I want to provide triggered job with the calling job name and build number.
You may consider using Jenkins CLI (http://[jenkins-host]/cli for help in the browser). You can specify a user to a build CLI command. I'm not sure what you mean by short description when starting a build, though.
Update: Please see Jenkins Wiki Authenticating Scripted Clients. I've created a user foobar ('full name' Foo Bar) and tried the following:
wget --auth-no-challenge --http-user=foobar --http-password=[apiToken] http://jenkins.yourcompany.com/job/your_job/build
Where the token is obtained from user configuration page: http://localhost:8081/user/foobar/configure. It worked. The user has to exist, though. Also, you must specify --auth-no-challenge option, otherwise it kicks off the build as anonymous. The status description says Started by user Foo Bar.
Another Update If everything else fails, you may consider the following workaround: start all builds via the Parameterized Trigger Plugin with an additional boolean parameter that tells the triggered job whether to run or not. In case the job is asked not to run it would fail immediately and call a 'clean-up' job passing to it the build info; the clean-up job then will delete the build from the system.

Resources