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

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.

Related

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.

How to send first pipe result to second one in Jenkins pipeline?

we use Jenkins as CI tools.
we want to separate login from other process.
we define a job for login, in this job we validate user and if user is valid we get user id.
at other job we need to have user id to generate result,Our problem is how we can send first job result(here:user id) to second one?
You can do this with the use of two plugins:
EnvInject Plugin
Parameterize Trigger Plugin
EnvInject allows you to inject variables into the Jenkins environment so they are available even after that build step.
Parameterize Trigger plugin allows you to pass information in this build job to another build job you want to start as parameters.
Once you've determined the username (I assume in some sort of batch or bash, you don't note the OS) you'll need to write it to a file on the system using a key=value pair. Then use EnvInject to get the value from the file into the jenkins environment. After that you'll use the parameterize trigger plugin to build the next job with parameters. This will require that you check the This build is parameterized box in the second job and that you define the appropriate parameters (perhaps with a default value that you can use to intentionally fail the build if you don't get a good value).

Jenkins - Put build in Queue

I want to create the following flow:
I have some program which is making a REST CALL to one of my builds, it could make it any time in a day, but i don't want this my jennkins build to be executed immediately, only in a specific interval of time E.G between 3-5 AM, but only if it has been triggered by the REST Call.
Is there any plugin or a way to do it ?
The easiest way would probably be to do some PowerShell scripting and an additional job:
Create an empty dummy job (This will be the one you trigger through the REST api)
Create another job, which will be the one you actually want to execute within a specific interval of the day. Use a Cron Build Trigger
to setup when the job should execute in case of a trigger
Using the PowerShell Plugin and Run Condition Plugin, write some PowerShell code using the Jenkins REST api to fetch information about the "dummy" job. Here you will check whether the dummy job has run (triggered) or not today, and then you can decide with the Run Condition build step, if you want to proceed with the build.

How to call a jenkins Job based on User inputs

The issue here is once the first Job in a Jenkins pipeline is done, we need to ask some inputs from user and based on the user Inputs to decide the next job to be triggered(Job2 or Job3)
tried build flow and parameterzied trigger plugin but didn't find any suitable option under these.
Any other plugin or jenkins feature which can help in achieving the above scenario?
There are a few plugins I have tried which collect user input on manually triggered jobs in a build pipeline: Active Choices Plug-in 1.2 and Extensible Choice Parameter 1.3.2.
With Active Choices you define a list of selections and a default value. With Extensible Choice Parameter you can have a text box and a default value.
This is how they work for me in Build Pipeline 1.4.8 on jenkins 1.628
If you run the manual step directly in the pipeline the default is used and other parameters propagate through correctly.
If you open the step there is an option to Build with Parameters which will ask for the user input. This works but other parameters like the build number do not propagate through so the pipeline is broken, and the pipeline screen does not show the status.
Jenkins will never pause and ask a user for inputs. It is an automated build system. It doesn't expect anyone sitting at the console watching the progress.
You can provide "inputs" or parameters when you manually trigger the job, i.e on the first job in your pipeline. You can them pass these parameters to downstream jobs, either through the Parameterized Trigger plugin or through a file copied between jobs.
If you need a human decision in the middle of your build flow, consider Promoted Builds plugin. With this plugin, a human can select a build, and then decide which "Promotion" to execute (which could branch your workflow as you need). The promotions can also be automated if needed, based on criteria and not human input.

Jenkins - Trigger email based on input parameter

I have several Jenkins jobs where I want an email to be triggered (or not trigger) based on an input parameter.
The use case is that I have one deploy job per project, but that job is parametrized for each environment. We you run the job you can select which environment to deploy to (Dev1, Dev2, QA, etc). Ideally I would like to send out notification emails when a new build is sent to QA, but I do not want to send out notification emails when a new build is sent to Dev, because that happens all the time (with every developer commit) and would flood email boxes.
I've tried googling but haven't yet found a solution. I am currently using the email-ext plugin.
Thanks.
It is a very nasty way to solve the problem, but if you cannot do it by any other means...
Create a dependent job that exists primarily to pass / fail based on the value of the parameter, which would get passed as a parameter to the job. Then you could chain the decision the job made to the email notification.
Of course, if you were going to do this, it would probably be better to just write a small parametrized email sender using javax.mail and have Jenkins run it by "building" an ANT project that actually calls the "java" task. Then you have your own email sender (full control) and could make it a dependent job on the other tasks.
I hope someone chimes in with a simpler way...
In email-ext you can add a "Script - Before Build" or a "Script - After Build" trigger which will trigger on whether the specified groovy script returns true or false.
The help for the script reads:
Use this area to implement your own trigger logic in groovy. The last line will be evaluated as a boolean to determine if the trigger should cause an email to be sent or now.
They don't give many details of what's available in the script, but from the source code on Github, it looks like you have "build" and "project" at least, and some common imports done for you:
cc.addCompilationCustomizers(new ImportCustomizer().addStarImports(
"jenkins",
"jenkins.model",
"hudson",
"hudson.model"));
Binding binding = new Binding();
binding.setVariable("build", build);
binding.setVariable("project", build.getParent());
binding.setVariable("rooturl", JenkinsLocationConfiguration.get().getUrl());
binding.setVariable("out", listener.getLogger());
Caveat, I haven't tried this, but this should work as an example script:
build.buildVariables.get("MY_DEPLOYMENT_ENV") == "QA"
that should get the value of a String or Choice parameter you've created called "MY_DEPLOYMENT_ENV" and trigger the email if the current value is "QA"

Resources