How create a drop-down list in Jenkis pipeline? - jenkins

I created a Jenkins pipeline that creates a backup of an Oracle Database and storage in a GitLab repository and, if necessary, execute a rollback. I want to create a drop-down list with a file in this GitLab repository that can select exactly what backup I want to execute. This is in a Stage block of Jenkins pipeline. It is possible?
Thanks

You can't create a dropdown list within a stage block in Jenkins, because the pipeline variables have already been determined by that point (from the properties([ parameters ([])]) blocks).
You can create a select list in the parameters block, but that wouldn't allow you to dynamically select from a list of files. Alternatively you could create a bunch of manual jobs based on a list of files, and kick off just the ones you need, but this doesn't seem like a CI/CD pattern that will scale well. You may want to figure out a better way to perform this job.

Related

How to reusably define an ordered list of build-steps for Jenkins and an arbitrary script?

Situation: I have a Jenkins pipeline job file with lots of (regularly changing) stages hard-coded with Groovy and I need a way to locally reproduce, what being done on CI.
In order to let the developer locally do, "what Jenkins would do" without having to manually keep a list of steps synchronized with the respective Jenkinsfile I'm looking for a way to store the ordered list of stages accessible by both Jenkins and a local script.
In other words - I want to be able to check out my project repository and run
make what-Jenkins-would-do
(make is only an example - I really don't want to use make for this)
So, given a set of scripts which contain what's being executed on each stage I now just need the order of execution stored in a sophisticated way.
What I'd love to have is a way to let Jenkins read a list of pipeline steps from a Yaml/JSON file which then can be used in other scripts, too.
Here is what's going through my mind
I can't be the only one - there must be a tiny nice solution for this need
maybe I could use Groovy locally, but that would add another heavy dependency to my project and the Groovy-scripts contain lot's of Jenkins- and node-specific stuff I don't need locally
Don't want to store information redundantly
Just executing a 'do it all' script in both Jenkins and locally is not an option of course - I need individual stages.
Jenkins / Groovy and pipeline jobs are a requirement - I can't change that
So what's the modern solution to this? Is there something like
node('main') {
stage('checkout') {
// make the source code available
}
stages_from_file("build-stages.yaml")
}
?

How to copy parameters from one pipeline to another without copying entire pipeline?

On our team, only few people have Jenkins access to perform admin operations as it is Production Jenkins server which developers continuously use for builds.
Sometimes I have to enhance any pipeline or fix issues of pipeline. For that admin has created one pipeline for me so I can add code there and test it. I am suppose to use only that pipeline to test anything.
But I test different pipelines, each pipelines has different parameters list. In this case, I've to add parameters one by one and copying all details of that parameter like Groovy Script, default value etc. which takes lot of time.
Is there any way/plugin using which we can simply copy only parameters from one pipeline to other?
I think you should know each job has a config.xml which represents the job configuration. You can get it by <job_url>/config.xml.
Get the config.xml of the job you want to debug, then extract the xml block for job parameters from the config.xml
Prepare an empty structure config.xml, inject the job parameters' xml block into the empty config.xml
Call Jenkins Rest API to update/save the config.xml to your debug job, then your debug job has target job's params.
You can write a script to implements above 3 steps.

Jenkins declarative pipeline parameters

Is there a way to pass in a parameter to a Jenkinsfile from the organization folder instead of the job level or global level?
Essentially what I'm looking to do is have one Jenkinsfile that handles whatever situation I need, and have multiple organization folders that send it parameters. So basically I can have one organization folder that scans and grabs all of the feature branches, and when I run one of the jobs it merges them to develop. Another one that grabs all of the develop branches, and when I run one of the jobs it just builds them. etc.
I need some way to pass parameters to my Jenkinsfile to say "Hey I'm this folder, this is what you should do". I can't find a way to do so. I thought of making multiple Jenkinsfiles but it would be confusing to know which one to place in each repo. I would change the names of the Jenkinsfiles so it's obvious which one to use, but the only option I get for "Project Recognizer" in the configuration is "Pipeline Jenkinsfile" so I don't know how I can change the names and the organization folder still recognize it.
Is there something I'm missing? Any way to send a parameter to my Jenkinsfile from the folder instead of a global level? Or is there some other way to solve my problem and be able to tell my Jenkinsfile what to do depending on what organization folder it is in inside of Jenkins?
Or is there some other way to solve my problem and be able to tell my Jenkinsfile what to do depending on what organization folder it is in inside of Jenkins?
A simple way to check in which organization folder job is built is to parse it from env.JOB_NAME parameter. For example:
Jobs hierarchies:
feature/job1
feature/job2
production/job1
production/job2
To make Jenkins Pipeline to do different functionality whether they are in feature or production organization:
def topFolder = env.JOB_NAME.split('/')[0]
// In code somewhere else:
if (topFolder == 'feature') {
doSomething()
} else if (topFolder == 'production') {
doOther()
}

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