Taking out common config of jenkins jobs - jenkins

I have about 200 jenkins, each of them has a long config page but actually most config are the same. Everytime when I need to update something in the common config, I write a groovy script to loop though those jobs and update them one by one. It's a pain because it takes about 5 minutes to update those jobs by the groovy script. I am wondering is there a jenkins plugin(or something else) that I can use to put the common config in one place? jenkins slicing plugin doesn't work well, I think it conflicts with another plugin.
Thanks

Sounds like a job for the job-dsl plugin
From the wiki page
The Jenkins job-dsl-plugin attempts to solve this problem by allowing
jobs to be defined with the absolute minimum necessary in a
programmatic form, with the help of templates that are synced with the
generated jobs. The goal is for your project to be able to define all
the jobs they want to be related to their project, declaring their
intent for the jobs, leaving the common stuff up to a template that
were defined earlier or hidden behind the DSL.

Related

Generating Jenkins Jobs WIth JobDSL and a Template

I have a lot of jenkins jobs which are very similar. They all have the same input parameters and same build steps so i was wondering whether theres a way to generate these jobs through Job DSL and a template.
I would like to keep separate groovy files for each job which has all the parameters for the template (for e.g: github repo, env, etc). Job dsl will read through these parameter files and uses the template to generate new jenkins jobs.
Do you think this is possible, Ive looked everywhere for a solution but it doesnt seem like something like this exists

Add condition to transition using script runner

I am using the scriptrunner plugin for Jira.
Is it possible to add a condition to a transition using scriptrunner?
Currently, my condition is in a script which I have manually added to the workflow.
But I was wondering if there is a way to do it automatically?
I was looking through documentation on: https://docs.atlassian.com/
I came across this method:
replaceConditionInTransition which is a method of WorkFlowManager.
But I'm unsure on how to use this.
Any help would be appreciated.
Conditions as any another scripts can be added from file system. You can store scripts in any VCS (bitbucket, github, gitlab, etc) and automatically deploy them to Jira server file system through any CI/CD system (teamcity, jenkins, bamboo, gitlab, etc).
So, as result process will be looks like. 1. commit changes in you script to vcs 2. wait a bit for auto deploy (e.g. triggered by commit) 3. done. As additional you can write any script/service/etc for commit these changes automatically if needed.
Also look at script roots it's helpful way which allows reuse any of script fragments through helpers classes.
It's rather conceptual answer basically because implementation is depends on environment, but I hope that you get at least one more point of view to solve this task.
I think that using the Java API to modify Jira workflows is pretty tough. You could dig around in the workflow editor to see how conditions are added there. Remember that you have to do this in a draft workflow and then publish it, which takes some time in large projects
I like the idea of replacing a script file as easier, if it can be done when no issues are transitioning

How to create a Jenkins job config.xml?

When I create a jenkins job via the API, I use a previous job's config.xml, make my modifications and then make the POST call to create the job.
My questions is, is there a way to generate this programmatically? I.e. is there a structure of a config.xml, what XML entities it should have, what values, etc so I can write a small module to generate one and send it to the jenkins API call?
I don't think there's any mandatory XML entities. Submitting an empty structure should result in a job that has default values for all settings.
What you want to do is exactly what's done by the Jenkins Job Builder. It provides a YAML-based framework for creating Job configuration XML files and submitting them to Jenkins. It's a common alternative to the Job DSL plugin. I wouldn't recommend to re-implement such a solution yourself -- handling all the plugin-specific XML configuration parts will be a nightmare.
We create our jobs using Job DSL plugin. You can try the playground http://job-dsl.herokuapp.com/.
At first, it seems that it is hard to learn, but after the first seed job, it is much better.
When we started writing our scripts we were afraid that there will not be suitable API methods for our needs. It turned out that we had one such case, which was solved using the configure block.
Get started guide here.

Can a single seed job process DSLs from multiple repos?

I recently managed to convert several manually-created jobs to DSL scripts (inlined into temporary 'seed' jobs), and was pleasantly surprised how straightforward it was. Now I'd like to get rid of the multiple seed jobs and try to structure things more cleanly.
To that end, I created a new jenkins-ci repo and committed all the Groovy DSL scripts to it. Then I created a job-generator Jenkins job that pulls from the jenkins-ci repo and has a single Process Job DSLs step. This step has the Look on Filesystem box ticked, with the DSL Scripts field set to jobs/*.groovy. With global push notifications already in place, this works more-or-less as intended: if I make a change to the jenkins-ci repo, the job-generator job automatically runs and regenerates all the jobs—awesome!
What I don't like about this solution is that it has poor locality of reference: the DSL scripts for the job live in a completely separate repository from the code. What I'd really like is to keep the job DSL scripts in each individual code repository, in a jenkins subfolder, and have a single seed job that processes them all. That way, changes to CI setup could be code-reviewed right alongside the code. To me, that just feels like an ideal setup.
Unfortunately, I don't have a clear idea about how to make this happen. If I could figure out a way to make the seed job watch multiple repos, such that a commit to any one of them would trigger it, perhaps I could inject another build step before the Process Job DSLs step and (somehow) script my way to victory, but... I'm unsure how to even get to that point. (I certainly don't want to do full clones of each repo in the generator job just to pull in the DSL scripts!)
I suspect I'm not the first person to wish they could put the Job DSL scripts alongside the code, though perhaps I'm over-estimating the benefits. Any advice on this topic would be much appreciated—thanks!
Unfortunately there is no direct way of solving this. Several feature requests have been opened (JENKINS-33275, JENKINS-37220), but AFAIK no one is working on any of them.
As a workaround you can use the Pipeline Multibranch Plugin and create a multibranch project for each of your repositories. You must then add a simple Jenkinsfile to each repo/branch and use the Jenkinsfile to execute your Job DSL scripts. See Use Job DSL in Pipeline scripts for details. This would require minimal coding, but I think each repo must be cloned for this to work because the Job DSL files must be available on the file system.
You can use Job DSL to create the multibranch jobs, see multibranchPipelineJob in the API viewer. This would be your "root" seed job.
If your repos are hosted on GitHub, you can also checkout the GitHub Organization Folder Plugin. With that plugin you must only create one job for each organization instead of multiple multibranch jobs.

Generic jenkins jobs

I wonder, if it is possible to create generic parametric jobs ready to copy where the only post copy action is to redefine its parameters.
During my investigation I find out that:
- I can use parameters in svn path definition
- I can define the flow of builds using *Build Flow Plugin*
However I cannot force Jenkins to use parameters inside job names definition for promotion process. Is any way to achieve that?
Since I create sometimes branches from master I would like to copy the whole configuration of jobs but only one difference most times is that in the job name definition I replace master with branch name.
Why it not possible to use a parameter as the branch name?
Then when you start to build the job, you can input the parameters based on the branch you want to build.
Or find some way to get the branch info and inject it.
For example, we have one common job, which will monitor maybe 20 projects, if any of those job was merged into git, our gerrit trigger plugin will start to work, and all of job name, and branch is got dynamically from parameters.
Hope this works for you.

Resources