Is it possible to schedule a step in Jenkins pipeline? - jenkins

What ideally I need to do is run a Jenkins pipeline that will run immediately after I start the execution , checking out code, running a build but just running the deployment in a particular date/time. So Would it be possible to schedule a particular step in jenkins pipeline? This step would be executed at date/time provided by user input. If that’s not possible is there any alternative way of doing it?

The is no such way to schedule the steps of the Jenkins Job. But the Following approach may help you to achieve what you are trying to do.
Step-1: Install "Schedule Build Plugin" which will provide you a capability to schedule the job to run on certain data and time.
Step-2: Create a pipeline job with the code that you want to execute at a certain date and time. (Note: You will not schedule this job at this time)
Step-3: Remove the pipeline code that you already copied to another pipeline job and add a build step to schedule the build on the second pipeline that you will create in step-2.

Related

Jenkins: distinguish CI build from scheduled build

All my Jenkins jobs are triggered both by a Github webhook, but also via a scheduled build one per week. The build process is heavily cached to make the webhook CI builds finish quickly.
I would like to add a line to my build script which wipes the cache during the weekly scheduled build, and make it build from scratch. Is there a variable in the build script to identify if a build was triggered by a webhook or schedule?
Maybe the envInject plugin will give you what you need?
This plugin also exposes the cause of the current build as an
environment variable. A build can be triggered by multiple causes at
the same time e.g. an SCM Change could have occurred at the same time
as a user triggers the build manually.
The build cause is exposed as a comma separated list:
BUILD_CAUSE=USERIDCAUSE, SCMTRIGGER, UPSTREAMTRIGGER, MANUALTRIGGER
In addition, each cause is exposed as a single envvariable too:
BUILD_CAUSE_USERIDCAUSE=true
BUILD_CAUSE_SCMTRIGGER=true
BUILD_CAUSE_UPSTREAMTRIGGER=true
BUILD_CAUSE_MANUALTRIGGER=true

Run jenkins job without triggering downstream jobs

I have a jenkins job with a couple of downstream jobs which are triggered upon the job finishing correctly.
There are times when I want to run the initial job without triggering the downstream jobs. Is this possible?
It sounds like the conditional build step plugin might be of help. You can configure it to trigger other jobs based on various conditions, like so:
Here, the conditional build step has been configured to run downstream-job if foo.txt exists in the current workspace.

How do I run a Jenkins job on a weekend, AFTER another job has completed?

I have a job in Jenkins that I only want to run on Saturday, but it must only be ran after another job is finished (this is a report job, so data must be in place).
I understand how to use the Jenkins con expressions, but can I chain that in sequence with another job? The idea being once the first job is completed, the date is checked. If it's Saturday, run the second job.
Can Jenkins do that, or is it something I need to write via a script?
You can use Jenkin's Flexible Publish Plugin to set conditional build step:
https://wiki.jenkins-ci.org/display/JENKINS/Flexible+Publish+Plugin

jenkins: promote before job ends

I'm trying to configure a job in jenkins in a way that the promotion process (Promoted builds plugin) happens in the middle of a run. The point is that there are some annoying tasks like javadocs, sonar integration,... that can be run even after the promotion process, therefore I would like to make the build, run all needed tests and then promote the build automatically. Other tasks can run after the promotion process.
Do you know how I can implement this using the Promoted builds plugin?
Thanks in advance for the help.
No, Promotions only run on completed builds. If you want to run it in the middle of the job, then it's a build step, not a promotion.
You can either configure build steps for your actions (same actions as promotions). You can even call other jobs, and wait for them to complete (or not wait).
Or you can configure your "after promotion" tasks as a second promotion that is executed after the first one.

In Jenkins, if next trigger build is in pending state then how to abort running build and start running next pending build?

In Jenkins, If one build is currently running and next one is in pending state then what should i do so that running one should get aborted and next pending one should start running and so on.
I have to do it for few projects and each project has few jobs in it, I tried to save build_number as env variable in one text file (build_number.txt) and take that number to abort previous triggered build but making build_number.txt file for each job is not looking efficient and then I have to create many build_number files for each job for every project.
Can anyone please suggest me some better approach
Thanks
Based on the comments, if sending too many emails is the actual problem, you can use Poll SCM to poll once in 15 minutes or so, or even specify quiet time for a job. This will ensure that build is taken once in 15 minutes. Users should locally test before they commit. But if Jenkins itself is used for verifying the commits I don't see anything wrong in sending an email if build fails. After all, they are supposed to know that, no matter even if they fixed it in a later update intentionally or unintentionally.
But if you still want to abort a running job if there are updates, you can try the following. Lets call the job to be aborted as JOB A
Create another job that listens on updates same as that of the job that needs to be aborted
Add build step to execute groovy script
In the groovy script use Jenkins APIs to check if JOB A is running. If yes, again use APIs to abort the job.
Jenkins APIs are available here

Resources