Jenkins jobs to run concurrently with conditions - jenkins

I have a Jenkins job and does something(by calling a Ansible playbook) on a environment(a user selected parameter). I want to setup the job to run in conditional concurrent mode, like the jobs can run concurrently only for different environments.
I saw there was a same question:
Jenkins - Stop concurrent job with same parameter
posted 6 years ago and I did what the recommendation said, but when I tried with the plugins, I do not see the:
Builder: Set the build result
Result: Aborted
for the first Conditional step (single) in the answer.
So something have changed since then? Are there any new solutions for my case?
Thanks!

Related

Is it possible to pause a stage until a time is reached in a Jenkins declarative pipeline?

I have a requirement to only allow deployments to production in a specific time range, e.g.: between 04:00 and 06:00.
My Jenkins pipeline is triggered by a Git webhook and starts a flow consisting of the usual stages (Build, Test, Deploy Dev, Deploy Pre, Deploy Pro).
When we reach the Deploy Pro stage I would need to check whether the current time is in the allows range to go on. Otherwise I need to pause the pipeline (freeing the Jenkins agent) until that time range is reached.
Is is possible to implement this? How?
To do this, you can use sleep method, which can get different units: NANOSECONDS, MICROSECONDS, MILLISECONDS, SECONDS, MINUTES, HOURS, DAYS (by default it's SECONDS).
E.g. sleep(time:3,unit:"SECONDS") (or just sleep 3 - it's the same).
So, check whether the current time is in the allows range, and if it's not, then specify time for sleep method as value of 04:00 - <current time> (you need to use some arithmetics here to get right value in seconds or minutes, but it shall be not hard).
But if all the other stages are quite fast, I recommend you to use Poll SCM option (with specifying time range) for all pipeline, because it's much more easier and the result will be the same - job will be finished only after 04:00.
Your Jenkins pipeline is triggered by a Git webhook and starts a flow consisting of the usual stages (Build, Test, Deploy Dev, Deploy Pre, Deploy Pro).
Option 1:
skip the deployment using 'when' condition and if time is not between
Option 2: separate the pipelines for CI and CD And CD will trigger only during that or keep in pause stage using shell cmd

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

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

Jenkins configurations gets reverted by SYSTEM user anomaly

I am running Jenkins version 2.85 on Kubernetes as pod(Affinity set to one workernode). I am creating Jobs using Salt Jenkins module by passing XML to this module.
I am using Jenkins Global Library for preforming job execution.
My Job config looks like this
I am calling GobalLibrary with my parameters like repoURL, componet etc..,
Things goes well for weeks and now I landed to a weird situation where my job configurations(config.xml) gets updated/revert automatically.
Intermittently my "Build with parameter" options disappears and I can see only "Build now" in Jenkins GUI. Initially I thought someone is doing this, so to track the config changes I installed Job config history plugin in Jenkins and what I find is strange. Someone with "SYSTEM" username is making/reverting changes.
This is how it looks
and what I find is SYSTEM user revert only JOB config changes, not the PIPELINE.
I am not sure what's going wrong behind the scenes and how to stop or fix this. This is my Production instance so I am more worried.
I can see a SYSTEM user in my Jenkins
but I can not delete that user
Few relevant Question I find for this but with no answers
Configuration of Jobs getting updated by System user on Jenkins
Jenkins SYSTEM user removes custom workspace configuration
I am not sure if this Jenkins Bug or some plugin is playing with my soul.
Need help! :(
Okay I find the answer to this problem.
I have used properties in my Jekins Global Library something like this
// Disable concurrent builds
//properties([disableConcurrentBuilds()])
which overrides my external job configuration(done via salt).
Hint I get from this blog:
https://st-g.de/2016/12/parametrized-jenkins-pipelines
I also had this problem. For me it was solved when I changed the Build triggers -> Build Periodically settings from 'H 23 * * *' to '00 23 * * *'. (As I want my build to execute every night at 23:00.) Where H lets Jenkins decide when to run the job somewhere between 23:00 and 23:59 to spread load evenly. It seems Jenkins sometimes decided that it would be best to run my job on a different server and changed the parameters automatically.
In my case the issue was that the Jenkinsfile was removing the parameters I added to the pipeline from Jenkins console. Adding the same parameters in the JenkinSfile (stage -> script -> properties -> parameters) solved the issue.
In a nutshell, make sure that your Pipeline script is using the same configuration that your pipeline uses.
Jenkins documentation on parameters: https://www.jenkins.io/doc/book/pipeline/syntax/#parameters

Jenkins pipeline parallel not exeucting

I'm trying to test out the parallel functionality for a Jenkins pipeline job, but for some reason the individual build steps of the parallel job never get passed off to an executor and processed. Normal single-threaded pipeline jobs have no issue processing. I tried restarting the Jenkins server in case some resources were locked up, but it did not help.
The full script I'm trying to execute is:
def branches = [:]
branches["setup"] = {node("nsetup") {
echo "hello world"
}}
parallel branches
I have only one node, the master, and it has 5 available executors. It is configured to "use as often as possible". I'm pretty new to Jenkins and setting up a server for the first time, so maybe there's something I missed in the configuration that isn't related to the job.
Does anybody have any suggestions?
And 2 minutes after I post I figure it out! Every time.
Turns out I just didn't have any idea how the "node" command really works. By specifying a parameter in the parentheses, it was preventing it from releasing to an executor. I'm guessing that must tell it to try executing on a certain node matched by label, and I was using it like it was some random logging field. Oops!

Jenkins jobs on slave servers

I have many Jenkins Jobs that I need to run on every Build,
At present time I have 4 slave servers.
I would like the jobs to run in parallel as much as possible, hence I defined the jobs as follow:
Execute concurrent builds if necessary - Disabled
Restrict where this project can be run - Enabled with the following values SalveLinux1HT||SalveLinux2HT||SalveLinux3HT||SalveLinux4HT
To my understanding if Job A and B are triggered at the same time, one should use 1HT and the other should use 2HT and they can run in parallel
however Jenkins build job A on all 4 slaves and only after it's finished he will build job B on all 4 slaves
This is the opposite of my goal
Any ideas?
Thanks in advance
You can use
Build Flow Plugin
You can find both installation and configuration instructions of this plugin at the above mentioned link.
If you want to run any jobs in parallel you can use following scripts:
parallel (
// job A and B will be scheduled in parallel.
{ build("jobA") },
{ build("jobB") }
)
// jobC will be triggered after jobs A and B are completed
build("jobC")

Resources