Running sequentially job tasks on several environments using Jenkins - jenkins

I'm new to Jenkins. I'm trying to implement a specific scenario in a single job to build mobile applications using Jenkins.
In a single job I want to launch several tasks sequentially:
Task 1 (Windows) ---> Task 2 (Windows) ---> Task 3 (Windows) ---> Task 4 (Mac OSX)
Each job will be dedicated to a single project. Passing results from a task to another can be realised through the workspace, but it seems that job tasks must all run on the same environment. Is there any plugin that will let me run some tasks of the job in a particular slave ?
Thanks in advance

You could use trigger builds remotely on your slave jobs.
Then from the master job you can execute slave builds using curl. Like this:
$(curl --user "username:password" "http://jenkins.yourdomain.org/job/JOB-name/buildWithParameters?SOMEPARAMETER=$SOMEPARAMETER&token=TheSecretToken")
TheSecretToken is the token password you specified on your slave plugins.
And username:password is a valid user on your jenkins. Don't use your own account here but rather a 'build trigger' account that only has permissions to start specific jobs.

Define a job for each task you have mentioned.
Have a slave on the remote machine(s) - presumably the Mac.
In each job, set the relevant host that will run it (you have a parameter for that).
Use the "trigger parameterized build" plugin to trigger the jobs in the correct sequence, and make sure you pass "Current build parameters" in that section.
This plugin will allow you to pass other values as well - read its help for more details.

Try this
Build flow Plugin
Multijob Plugin

Related

Jenkins skip some jobs in chain of freestyle jobs

We got a requirement to implement CICD using Jenkins.
Here, Jenkins is running in windows machine and application server running in linux machine and build activity should happen in Linux system. So, We are connecting to linux machine using Jenkins's SSH plugin and executing jobs.
I have created list of freestyle jobs to checkout code from CVS, cleanup activity, Build , stop server, start server, Run Junit, run sonar. all these jobs are chained using 'build other projects' option in post build Action section.
Here, all jobs executes in sequential manner. But, sometimes I need to execute only few jobs like stop and start server.
So, please help me how we can randomly pick jobs which need to be run before triggering build.
Thanks,
Ganesha

how to trigger Jenkins job from ant script running on same Jenkins server

I'm running an ant job that runs several thing on master node and need to trigger several jobs on slave servers based on the options i select from the main job parameters
is there a way to call another job from within ant script without using jenkins-cli.jar as external command
You can trigger Jenkins jobs by doing an HTTP request:
Go to your Job Configuration
Build triggers > Check 'Trigger Builds Remotely' and think of an access token, e.g. SOME_SECURE_TOKEN.
In your ant script: execute a POST request to JENKINS_URL/job/JOB_NAME/build?token=SOME_SECURE_TOKEN
Note that if you have authentication in place, you need to set up a user who is authorized to start the other jobs. In that case read this more detailed explanation: https://www.nczonline.net/blog/2015/10/triggering-jenkins-builds-by-url/
Another solution would be to use the Parameterized Trigger Plugin to trigger builds from a Jenkins step. You mention that the jobs that need to be triggered may depend on the job parameters. In that case you can combine the Conditional Buildstep plugin with the Parameterized Trigger plugin.

Send command from TeamCity to run automation tests on Jenkins

Is there a way to add a build step in TeamCity which sends a request to Jenkins server, run some automation test scripts in Jenkins and sends back a response to Teamcity.
The idea basically is to automate the whole deployment process which also includes running of some automation tests created using python scripts (which will be done on Jenkins).
I am not sure if this is the best way of doing it but are there any better ways to achieve this? Also any hints on how to send command from Teamcity to Jenkins?
You can make an http request, as mentioned in the comment to start tests on Jenkins.
As for publishing the results bach to TeamCity, the possible solution might be:
after tests are done on Jenkins, publish the results that can be accessed externally (by TeamCity) and interpreted / reported by TeamCity (either in any of the supported formats), or manually, by the script, that will be run by TeamCity, using service messages
create a build configuration that will process the tests after the Jenkins build
set up a URL build trigger plugin, configure the trigger for the created build configuration. Point the trigger to the address where results are published. As soon as the content published is changed, the build will start and you will be able to download the tests results to TeamCity and process them
Got an easy to implement solution for the first part i.e. sending command from Teamcity to Jenkins
Using CURL:
Install/copy Curl to the Teamcity agent.
and then in your TC build configurations, create a new Command line build step similar to below (modify the parameters to your needs)
curl --user %jenkins_user%:%jenkins_pwd% -X POST http://%jenkins_instance_withport%/job/%jenkins_jobs_name%/buildWithParameters?token=%jenkins_token% --data "Build_Number=%build.number%"
e.g: curl --user admin:password -X POST http://jenkinssever:2123/job/test-build-image/buildWithParameters?token=rtbuild --data "Build_Number=1.2.0"
Here i could even pass the build number to Jenkins by using "-- data"
Do the below under Jenkins build configuration:
In Jenkins configuration:
In Jenkins configuration, update the below values:
"This project is parameterized"
Name: Build_Number
Default Value: 1.2.0
"Trigger builds remotely"
Authentication Token: rtbuild
Optional: for setting build number
"Set Build Name"
Build Name: #${Build_numuber}
done and you are good to go.please do let me know you if you have more questions.
the above is the implementation of the Initial comment
I think I found a way while trying to solve similar use-case, did it for batch files in Teamcity build steps. for Jenkins, we have to modify accordingly.
Also is there any specific reason for using Teamcity and Jenkins simultaneously, unless you are making use of already created Jenkins build?
Steps:
Get CLI based command for Jenkins:
https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+CLI
you can achieve in two ways
Method 1:
As build step is in current build.
Create a build step before your current step and trigger the Jenkins build using CLI
Based on the return value of the Jenkins build step, next step will execute
Method 2:
create a new build with above CLI step and add a dependency in your primary build.
so whenever the primary build is started, it will start the dependent CLI jenkins build. and once the dependent build is completed, will return success/failure, based on that the primary build will start.
i haven't tested the CLI of Jenkins but as Teamcity supports the steps and dependencies structure, expecting this will work.
will keep posted once i implement it.

run a Jenkins job on another Jenkins instance from the Jenkins job

I want to create a Jenkins job that starts other Jenkins jobs. That would be quite easy, because Jenkins Template Project Plugin allows us to create a build step of a type "use builders from another project". However, what makes my situation harder is that I have to start Jenkins jobs on other machines. Is there any standard way to do that?
In case you want only to trigger new build of Job You Have plenty of ways to accomplish it
you can use remote access API and Trigger a request to build target job from source Job.
https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API
Or you can use https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Remote+Trigger+Plugin
which is handy in handling server details and other stuff. you shoukld ensure ssh keys shared by both servers.

Jenkins - cleanup after job

I have a couple of unit testing / BDD jobs on our Jenkins instance that trigger a bunch of processes as they run. I have multiple Windows slaves, any one of which can run my tests.
After the text execution is complete, irrespective of the build status is passed/failed/unstable, I want to run "taskkill" and kill a couple of processes.
I had been doing that earlier by triggering a "Test_Janitor" downstream job - but this approach doesn't work anymore since I added more than one slave.
How can I either run the downstream job on the same slave as the upstream, or have some sort of a post build step to run "taskkill".
You can install the Post Build Task plugin to call a batch script on the slave (when your UT/BDD are completed).
The other solution is to call a downstream job and to pass the %NODE_NAME% variable to this job with the Parameterized Trigger plugin.
Next, you can use psexec to kill the processes on the relevant node.

Resources