How to re-run a build with the same parameters scheduled by cron - jenkins

I know about the rebuild and replay functionality, but both of them are manually triggers. So here is our problem:
We have multiple servers which can be deployed with any branch that exists. But this deploy is manually. But we want to ensure, that at least once a day, the latest version of that branch is deployed to avoid having servers being outdated.
So what I want to do it, create a scheduler job that runs once a day and triggers a Jenkins job to rebuild the last job using the exact same parameters.
Would be great if someone has some input here :-)

You can try out the Persistent Parameter plugin and use it to define the relevant parameters inside the deploy job that you want to reuse.
This plugin enables you to set you input parameters (string, text, boolean and choice) with default values taken from the previous build - so every time you run the build with parameters manually or trigger it from another job the values that will be used are the values from the last execution.
Your caller job can still pass parameters to the deploy job during the daily execution - but for all parameters that are not passed their latest value will be used.
You can also override parameters defined as persistent as it just affects the default value.

Related

Jenkins: How can I know if an automatic process or a user has triggered a build?

Is there an environment variable in Jenkins which tells me if the build has been ran manually or automatically triggered by polling?
My pipeline works like a charm if automatically triggered, but if manually ran... it always fails, so I think I'm going to edit the pipeline to check how the build has been triggered.
Unfortunately variable env.BUILD_CAUSE is not set in Pipeline builds.
For pipeline jobs see following example
if ( currentBuild.rawBuild.getCauses()[0].toString().contains('UserIdCause') ){
// do steps for manual trigger here
}
Other possible causes to compare can be found here.
The ability to get causes for a workflow run was released in version 2.22 (2018 Nov 02) to the Pipeline Supporting APIs Plugin. The feature was requested in JENKINS-41272.
A couple methods were added to the currentBuild global variable with that release:
getBuildCauses
Returns a JSON array of build causes for the current build
EXPERIMENTAL - MAY CHANGE getBuildCauses(String causeClass)
Takes a string representing the fully qualified Cause class and returns a JSON array of build causes filtered by that type for the current build, or an empty JSON array if no causes of the specified type apply to the current build
See answer https://stackoverflow.com/a/53342374/5955565.
I have copy-pasted it here because this question is shown first in search results (unlike How to differentiate build triggers in Jenkins Pipeline).
See also ${YOUR_JENKINS_URL}/pipeline-syntax/globals for a complete, and up to date, list of properties available on currentBuild.

Jenkins plugin code that should excute before any kind of job is run in Jenkins

I am new to Jenkins plugin development. M trying to write a plugin that should be executed before any Multi configuration type job runs in Jenkins.
In this plugin I want to write rules that will check what configuration parameters user has selected while submitting the job, based on selected parameters, I want to decide whether to allow the job to run or to restrict it.
User should be shown reason as to why that job cannot be run in the Console Output.
Does anyone have any ideas which class I need to extend or which interface I need to implement in order to get a hook into Jenkins job run?
You could look at the Matrix Execution Strategy which allows for a groovy script to select which matrix combinations to run. I would think if your script threw an exception it would stop the build.
For background, the multi configuration projects run a control job (or flyweight) which runs the SCM phase then starts all the actual combinations. This plugin runs after the flyweight SCM checkout.
If nothing else, this will give you a working plugin to start from
Disclaimer: I wrote this plugin
Blocked queue job plugin was what I needed
Out of the box that plugin supports two ways to block the jobs -
Based on the result of last run of another project.
Based on result of last run of the current project
In that plugin the BlockQueueItemTaskDispatcher.java extends Jenkin's QueueTaskDispatcher providing us a hook into Jenkins logic to allow or block the jobs present in the queue from running.
I used this plugin as a starting point for developing a new plugin that allows us to restrict project based on parameters selected and the current time. Ultimate goal is to restrict production migrations from running during the day.
Overriding the isBlocked() method of QueueTaskDispatcher gave access to hudson.model.Queue.Item instance as an argument to me. Then I used the Item instance's getParams method to get access to build parameters selected by the user at runtime. Parsed the lifecyle value from it. Checked the current time. If lifecycle was Production and current time was day time then restricted the job by returning non null CauseOfBlockage from isBlocked() method. If that condition was false, then returnedCauseOfBlockage as null allowing the queued job to run.

Jenkins - Running concurrent jobs with "circular" parameter

I'd like to run several builds concurrently in Jenkins for the same job. I run at maximum 3 builds concurrently. I want each build to run with a parameter that must be unique from a pool for parameters. For instance, pool=[1, 2, 3]: The 1st build picks "1", 2nd picks "2" and the 3rd picks "3".
I must ensure that different builds can't pick the same parameter.
After building, the parameter is available again.
How can I do it?
Alternative: How can I count the number of builds running in this project and pass it as parameter?
At first, select the checkbox button named build-concurrently-if-neccesary to ensure the same job could build concurrently. you'd better read the help-html seriously before
The isolated environments for building different jobs make that data could not be shared each other in a simple way.
Here is a solution that trigger the buildWithParameters link by jenkins rest api to control the pool in the program procedure of your own.
add a string-parameter in job's config.
post the string parameter to http://$JENKINS_SERVER_URL/job/$JOB_NAME/buildWithParameters
Maybe it's the most convenient way if no available plugin found.
I found a plugin in github and asked the author to publish it. It works well and solves my problem.
Jenkins Parameter Pool Plugin

How to run parametrized build after commit to repository?

I have parametrized build job in Jenkins. It has configured SCM polling and the build job is started after new commit.
Parameters for this build job are location profiles defined in main pom.xml. Count of these profiles is static and persistent. So after every commit I need to build a project for the same profiles. One profile is started for one build.
It is able for manual triggering when I write profile name and start the build job. But after new commit this build job is started without parameters. So is there any way how to define list of parameters for build job - one parameter per one build.
An SCM change will trigger a related Jenkins job. Once. That's it.
When that job is triggered, and is configured with parameters, it does have default parameters.
For string parameters, it's the default value entered in configuration page (if you haven't entered one, the default is just that: none).
For single choice-style parameters, it's the top-most value.
For multi choice-style parameters, again, unless a default is provided in configuration, it's nothing.
If what you want is to trigger multiple runs of the same build for the same SCM change, then you've configured your jobs wrong.
Either create a matrix job, and configure an axis for every "profile" as you call it.
Or create multiple jobs, and chain them, so that first is triggered by SCM change, and the rest are triggered in sequence
If you only want to specify one default string that should be picked up while building using Poll SCM feature, then you should try using the following method:
Select the following options in Extended Choice Parameter:
In Simple Parameter Types section, go for Single Select in Parameter Type
Instead of Choose Source for Value, go for Choose Source for Default Value. Now enable the radio-button named Default Value. Enter whatever string you want to specify. Build will pick-up the given string as the default one.
Hopefully, it should work. At least, it works when i use Build periodically option. :)

can I configure build to happen every minute but deploy should happen only once a day in a single Jenkins job?

In a single Jenkins job, we can trigger a build by specifying a schedule and also by polling. But then, in both the cases, the build is triggered, and the deploy operation that I have configured as a post-build step (using PostBuild Task plugin) also happens. I want that the build happens whenever a change is detected by polling, but deploy should happen only according to the schedule I have provided.
Is it possible to do it in a single job, or do I have to configure 2 separate jobs for them ?
You said you are using PostBuild Task plugin. This allows to do a regular expression on the console log to determine whether to execute a task or not.
Builds started by schedule will have Started by timer at the top of the log. All you need to do is add this expression to your PostBuild step under "Log Text" field. If you are already using some criteria in there, click "Add" button to add another "Log Text" field, and use the "AND" operator between them
It will be cleaner to do it in 2 jobs. However, if you really need to have it in one job, you could use a combination of Jenkins plugins to do the job.
Use EnvInject Plugin to expose the BUILD_CAUSE and/or BUILD_CAUSE_SCHEDULED* environment variable(s). (This may not be necessary, you might be able to reference the Jenkins variables within the Jenkins configuration by default)
Use Flexible Publish plugin, post-build action, to set up a conditional publish step when BUILD_CAUSE == SCHEDULED, or when BUILD_CAUSE_SCHEDULED == true. (Just test one condition.) Note that you'll need to use Jenkins' expression syntax, like so:
${ENV,var="BUILD_CAUSE_SCHEDULED"}
* BUILD_CAUSE_SCHEDULED is not its real name, you'll need to find this out on your own, sorry.

Resources