how can I use a jenkins job to loop another jenkins job - jenkins

I have a requirement where I need 2 Jenkins job, One job to take user parameters (eg: user chooses number 10 which means I need to run second job 10 times)
second job will be the actual execution of test automation.
The loop will continue irrespective of the result (pass/fail) of previous iteration
just trying to represent this as a Snippet,
Job A :
User input = 10
for(num<=10){
Job B(num)
}
Job B :
execute(num)
P.S: If there is a solution where I can achieve this in a single job, Please suggest

You can use Jenkins upstream/downstream concept, please refer below url:-
Jenkins Upstream/Downstream Linking

You can do it:
You can use the same solution for Linux and Windows machine (in windows write cmd syntax and in Linux Bash)
You create a loop for example in Bash:
!/bin/bash
for i in {1..5}
do
curl
done
If you do not know CURL please use the link below.
https://serverfault.com/questions/888176/how-to-trigger-jenkins-job-via-curl-command-remotely

Related

Can Jenkins run some code or it is only scheduling external programs?

I'm new to Jenkins and I heard it is really good for continues integration.
My flow is not complicated: I need to get a list from SQL by some query, parse it line by line, send each line to some virtual machines (which will run this line and create some file as result), and then analyze the results.
Where in Jenkins can I program my code?
Is Jenkins' purpose is only to schedule external programs one by one and not to run the code in Jenkins itself?
Is there a way to write code in jenkins that is not a bunch of CMD commands?
you can do the following ( syntaxic pipline ) :
- stage 1 : execute command over ssh (jenkins plugin) to execute sql query
- stage 2 : send each line to dedicated VM with Ansible playbook
- stage 3 : analyze depend on the tech you are using there are plenty of jenkins plugin to connect to monitoring and analyzing tools like grafana or zabbix .. that can ease the process
I think what you are asking for is scripted pipeline: https://jenkins.io/doc/book/pipeline/
This allows you to write Groovy code to execute on the Jenkins master. You can do almost anything you want from there, just like any programming language.

Jenkins will look for available node

I have a job in jenkins that run batch file on few nodes. I did it using node parameter. I also added condition that it will run few times. This job is running in parallel but when one host is stack I would like it to continue running this job on other nodes. This is not working. Jenkins is going to the first parameter in the node and trying to run on it and if it is stuck it will not continue to other node. I would like jenkins to look for available node. I tried using multi job plunging and it also didnt work. Also tried working with label but it is not running in parallel....any help ?
I think you can add labels to your node , if you have 4 slaves on ly 2 are windows both will have the label windows , than in your job use the Restrict where this project can be run , add the label value - windows
if you need to run the same job in parallel you can create a matrix job , the same flow will run on several slaves.

how to run multiple builds on a single Job in jenkins

I have a script to run to which i give multiple parameters in a loop, it takes each parameter completes the cycle and then the next one.
I need to run this on Jenkins, is there any option that i can run multiple builds on a single job ? I mean each parameter should be a single build and all the builds should run in queue not parallel.
We have two options :
1.) In my case i use a shell script and csv file as an input to it. So i wrote a simple groovy script for getting the inputs from csv file. Then i used parameterized plugin to run the script
2.) In case if you want to see the workflow on GUI you can go for pipeline

Script to build a set of jobs, wait until they finish and write a report about them

I'm planning to write a script that would choose a set of Jenkins jobs with a regexp, start their builds and wait until they finish. If most of them fail, I want to change some global env vars (I already know how to do this) and build them again. Then I want to collect the test results and format it into a nice report.
I can't put this into the individual jobs' post-build actions, I need info about all of them to write the report and to do the rebuilding.
My current idea is to use the Jenkins REST API, but before I do so: is this already implemented somewhere?
Give a look at Multi-Job plugin and see if it fits your requirement.
Else go with REST API.
Because unique requirement of our build process I had to use jenkins cli :
java -jar jenkins-cli.jar -s http://<jenkinsURL> build <job-name> -s <parameters if any>
The trailing -s makes it wait till the job is complete.

Jenkins: Slave selection based on user's choice

I'm trying to configure a jenkins job so that based on the choice of the user the node/slave should be selected.
example : if choice = windows ->slave1
if choice = Linux ->slave2
I've tried with configuration matrix i'm getting error that the nodes are offline.Is there any plugin to do such selections in jenkins
The simplest is to create two build jobs that are then tagged to a specific slave.
When you configure the job you can add labels to it that specify the requirements for this job.
Example:
linux-build, add the label linux
windows-build: add the label windows
Then, when you create the slaves you need to assign them labels as well specifying the capabilities.
Example:
Windows system, add the windows label
Linux system, add the linux label
After this your builds will automatically go to the correct system and you never have to specify anything again. This is better then having to manually specify and trigger a job.
One more advantage, if the job fails you know why.. maybe you linux job always succeeds but you windows job always fails, if you combine this in one job you can't really see this pattern but if you have two separate builds you will immediately see a dark cloud forming over you windows build.
Checkout the following plugin:
https://wiki.jenkins-ci.org/display/JENKINS/NodeLabel+Parameter+Plugin
I used the Jenkins CLI to implement the dynamical slave selection.
1) create two jobs: job A triggers job B
2) at job A, input the followings at Build/Execute Shell
if choice = windows, SERVER=slave1
if choice = Linux, SERVER=slave2
java -jar jenkins-cli.jar -s http_to_jenkins-server:port build buildname -p SERVER_LABEL=$SERVER -v -w --username yourusername --password yourpassword
3) at job B, select "This build is parameterized" and add a String Parameter SERVER.
Hope it helps.

Resources