Jenkins pipeline - jenkins

I have create job template at Jenkins and created jobs based out of template. But when I added properties: Set Job properties -> Build Triggers -> Build periodically code to schedule jenkins job execution, the relation of job with template is removing after first execution and job became stand alone job. Whatever change I made to template is not picking by the job after that. Is there a option to schedule jobs while using job template?

If I understand correctly if you create a new job after modification of template , the changes are reflected but the changes are not reflected in the existing job?
It could be a bug in older version of jenkins. Try to configure the existing jobs and save it again to make the changes reflected.

Related

Seed job repeats building infinitely every minute due to 'job_template' change

I use jenkins-job-dsl plugin. Created seed job to run myJobs.jenkins_jobs file, inside which I have written job job_template and another job, which is using 'job_template'. However, after building seed job, it continues to build again and again, until I disable it.
In https://jenkinsci.github.io/job-dsl-plugin/#path/job-using I see
Creates a new job configuration, based on the job template referenced by the parameter and stores this. When the template is changed, the seed job will attempt to re-run, which has the side-effect of cascading changes of the template the jobs generated from it.
However, I'm not sure what could I do to get rid of this constantly rebuilding.
My myJobs.jenkins_jobs file looks like this:
job('job_template'){
}
job('railgun-db-importer-DSL') {
using 'job_template'
}
SOLUTION
The error was that template job had 'description' field updated with date after every run - this caused it to change every run, and run again every run. After putting separate 'description' in every job and hardcoding template job's description, so it doesn't change itself upon run, I got rid of perpetual runs.
You must not maintain template jobs by job-dsl.
The idea behind a template job is that you can create new jobs via job-dsl, based on an existing job that's not maintained by job-dsl (this is the template job).
Typically, you want to do that if there's some complex plugin configuration which is hard to implement in job-dsl directly -- in those cases, it can be more simple to create a template job manually, and use it as a basis for further configuration via job-dsl.
In your example, every DSL run will touch the template job; since modifications of the template job will trigger DSL again, this can lead to the infinite loop that you observe.

Remove unnecessary jobs from build pipeline on Jenkins

I'm a bit new to Jenkins and I'm having an display issue with Build Pipeline plugin. I'm executing some jobs in parallel using JobFanIn Plugin, i.e. the next job in the pipeline will only be executed when all the previous jobs are concluded. However, the Build Plugin believes that all jobs will trigger a new instance of the last job. The execution goes right, but the display isn't.
Bellow is possible to observe what is happening in practice. The GENERATE TEST REPORT job will only be triggered when all TEST jobs are finished and this occurs ok. But since the Build Pipeline plugin expects 3 instances of this job to happen, only one happens and the others appeat as pending forever.Any ideas?
First image displaying what is happening
Second image displaying what is supposed to happen

Jenkins GitHub Organization jobs triggered by schedule

I've setup an Octopus organization with multiple repositories inside.
One of those repositories has automated tests. I'm trying to setup the Jenkinsfile in that repo to make the job trigger periodically instead of only on SCM change.
I haven't found documentation that shows this is possible. Even the job, after is automatically created shows the BUILD TRIGGER option but of course it can't be saved.
Jenkins Organization Job Configuration Screenshot
I found the way of doing this by using the "properties" option. In this example, the job will trigger by changes pushed to GitHub and also on a periodic basis (every 60 minutes):
properties([pipelineTriggers([cron('H/60 * * * *'), [$class: 'GitHubPushTrigger']])])

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 job wait for first successful build of other job

I have a Jenkins job that should not start building until another job has been built successfully at least once. They are not related per se, so I don't want to use triggers. Is there a way to do this?
Some background: I'm using SCM polling to trigger the second job. I've looked at the Files Found Trigger plugin, but that would keep on triggering the second job after the first on has been built. I've also found the Run Condition Plugin, but that seems to work only on build steps, not on the entire build.
Update - The second job copies artifacts from the first job. As long as the first job has never completed successfully, the Copy Artifact step fails. I am trying to prevent that failure, by not even attempting to build the second job until the first job has completed once.
One solution is to use the Build Flow plugin.
You can create a new flow job and use this DSL:
I've used 10 in the retry section but you can use any numbers.
This flow job can be triggered by monitoring the same SCM URL of your second job.
Update, here is a second solution.
You can use the HTTP Request plugin.
If you want to test that your first job has been built successfully at least once, you can test this URL:
http://your.jenkins.instance/job/your.job/lastSuccessfulBuild/
One example:
As my build has never been successful, the lastSuccessfulBuild URL doesn't exist. The HTTP Request changes my build status to failure.
Does it help?
The Block queued job plugin can be used for this: https://wiki.jenkins-ci.org/display/JENKINS/Block+queued+job+plugin

Resources