Trigger another jenkins job execute shell - jenkins

How to trigger a jenkins job from another freestyle job execute shell section? For pipeline jobs we have groovy which makes it easy.

Two ways to do.
1 - Add a post-build step to build another project
2 - Create a user in jenkins to build remotely jobs (example remoteUser). Create a API TOken for this user. Edit the project to be triggered and make the option “Trigger Builds Remotely”, add an authentication token (IT'S NOT THE TOKEN OF THE USER, YOU CAN GENERATE A NEW FOR THIS).
Now can just use curl to start the job
curl -X POST HTTP://remoteUser:{remoteUser_API_TOKEN}#jenkins.domain/project/my_project/build?token=TOKEN

Related

How to trigger multiple Jenkins jobs remotely using an HTTP request

Is it possible to trigger multiple Jenkins jobs remotely using only jenkins url http://xxxxxx/?
Job name and parameters should be passed as script or json format.
You need to configure the jobs to enable the option named Trigger builds remotely (e.g., from scripts) under Build triggers where you need to enable the option and choose some authentication token.
You will also need to get the API token for your user to trigger the job. Log in to your Jenkins and in top-right corner click on your username and hit Configure button and create a new API token.
You can then invoke your job for example with curl:
curl -u <user>:<api_token> "http://<jenkins_url>/job/<job_name>/build?token=<job_token>"
To build the job with parameters, substitute /build with /buildWithParameters and pass the parameters in form of =, e.g.:
curl -u <user>:<api_token> "http://<jenkins_url>/job/<job_name>/buildWithParameters?token=<job_token>&<param1_name>=<param1_value>&<param2_name>=<param2_value>"...

Establish relationship between two Jenkins Jobs available on different Jenkins server

I am building Jenkins for Test / QA automation scripts, lets name it TEST_JOB. For application, I have application source code Jenkins build, name it DEV_JOB.
My scenario is when DEV_JOB completes execution (successfully), execute TEST_JOB immediately. I am aware about setting up project upstream / downstream [ Build after other projects are built ] to accomplish this task. But here, Problem is DEV_JOB is on different server than TEST_JOB. Due to which, TEST_JOB fails to recognize DEV_JOB.
Now, how would I achieve this scenario?
You can use Jenkins API for remote trigger of Job.
Say you have job on DEV_JOB on JENKINS_1, add a penultimate step(or upstream/downstream project having only this step) which invokes TEST_JOB using remote API call of JENKINS_2 server.
Example command would be
$(curl --user "username:password" "http://JENKINS_2/job/TEST_JOB/buildWithParameters?SOMEPARAMETER=$SOMEPARAMETER")
username:password is a valid user on JENKINS_2.
Avoid using your own account here but rather a 'build trigger' account that only has permissions to start those jobs.

In Jenkins how to trigger another build job based on the build parameter passed to current build job?

I have build jobs like a-build, a-deploy. b-build, b-deploy.
*-deploy are downstream job for *-build jobs. So they look like,
a-build
|
+-a-deploy
b-build
|
+-b-deploy
Now I have another job X-build. It accepts a-build, b-build etc as a parameter. So I if I run X-build with a-build as parameter it should complete with a post build action that triggers a-deploy. How can that be done?
You can accomplish this quite easily if you receive the job name in a parameter.
You can then use "Call/Trigger Builds on Other Projects" step, and use the Parameter you receive in the job name:
If the step is not available to you via Post Build menu, you can get to it via "Execute set of scripts" post-build step.

Jenkins - Upstream project dependency issue

Here is something I want to achieve:
I have a jenkins project which has 4 upstream projects. But I don't want to trigger this project when the upstream jobs are done building, but I want the trigger the project via remote API, which then waits on upstream projects until they are done building, if these projects are building.
Lets say all the 4 upstream projects can build the source code from any branch passed via API, but I want the downstream project to start only when a specific branch is passed to these upstream projects.
Scenario:
Lets say I have two clusters A and B, for the sake of this question, I want to deploy my code to cluster A, i.e front end and backend code. Now I have a project to build front end and 1 project to build backend (these two projects can build code for cluster A and B, based on the branch passed). Now, I have two deploy projects for cluster A which will deploy front end and backend. So, when I pass a branch to build code for cluster A, it will trigger the build projects. But now I only want these two deploy projects to start when this specific branch was passed.
If you want to control the builds remotely then use the Jerkins cli - I have found it very useful http://jenkinshost:8080/cli
You need to get the ssh key config right, add the public key of the user running the cli to the user you want to run the job in Jenkins using the Jenkins user configuration (not on the command line
Test key setup with
java -jar jenkins=cli.jar -s http://jenkinshost:8080 who-am-i
This should then report which user will be used to run the build in Jenkins
But I think you can use the Conditional Build Step plugin for your problem
https://wiki.jenkins-ci.org/display/JENKINS/Conditional+BuildStep+Plugin
This will allow you to put a conditional wrapper around a build step i.e.
if branch==branchA then
trigger step - deploy to clusterA
if branch==branchB then
trigger step - deploy to clusterB
Personally I find this plugin a bit clunky and it makes the job config page a little messy
Another solution I came up with was to always call the child job and then let it decide if it runs.
So I have a script step at the start of the child job to see if it should run
if [${branch}="Not the right branch name" ] ; then
echo "EXIT_GREEN"
exit 1
fi
You have now failed this job which would cause the parent job to go red but by using the Groovy Postbuild plugin https://wiki.jenkins-ci.org/display/JENKINS/Groovy+Postbuild+Plugin you can add a post build step like this
if (manager.logContains(".*EXIT_GREEN.*")) {
manager.addBadge("info.gif","This job had nothing to do")
manager.build.#result = hudson.model.Result.SUCCESS
}
Child job has run green (with an info icon against the build) but has actually not done anything. Obviously if the branch is one you want deploy then the first script step does not run the exit 1 and the job continues as normal

Can one Jenkins Trigger a job on a remote jenkins

I have 2 Jenkins hosts, and would like First Jenkins to trigger a job on remote Jenkins based on "SUCCESS" in result on the first one.
I have looked at various plugins , but they all seem to indicate ONE Jenkins host, where multiple jobs can be chained in this manner.
Meanwhile, a jenkins plugin became available which makes it a lot easier:
https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Remote+Trigger+Plugin
It's very easy to do using cURL requests, no need for plugins or master>slave relations. It took me 5 minutes from beginning to start.
Use the following manual:
https://www.nczonline.net/blog/2015/10/triggering-jenkins-builds-by-url/
You could set up a downstream job on host1 that only builds if first job on host1 succeeds.
In this job you would trigger a remote build much like i described it in this answer
Step 1: Install following plugins in both Jenkins.
Generic Webhook Trigger: Job can be triggered from http request.
HTTP Request Plugin: To send http request as build step
Any Build Step Plugin: To use any build step in post-build action.
Step 2: Configure job to be triggered(Jenkins B).
Select generic webhook trigger in build trigger and generate a token and paste.
After saving this job can be triggered by sending a http request to
http://JENKINS_B_URL/generic-webhook-trigger/invoke?token=TOKEN_VALUE
Step 3: In master Jenkins(Jenkins A) configure flexible publish settings in configure system to allow use all build steps as post build actions.
Step 4: In post build actions add another step “Flexible publish”.
Using this any build action can be used as post-build action. Add a HTTP Request action.
Provide Jenkins B webhook url in url field and save.
Yes. Configure your Jenkins nodes and label them, say masterand slave (Manage Jenkins -> Manage Nodes).
1) Configure Job A and specify that it can only run on master ("Restrict where this project can be run" and in the label field put master).
2) Configure Job B so that it is only triggered if Job A is successful:
"Post-build Actions" -> "Trigger only if build succeeds"
3) Pin Job B to slave similar to step 1.

Resources