Using text editor for configuring Jenkins job's UI elements - jenkins

Is it possible to edit/save the code of "Job Configuration" (the structure and order of UI elements) ?
The pipeline job get the pipeline code from git, but this occurs only AFTER the job started. I want to edit the UI of job parameters (BEFORE user press "build")
Its necessary for 2 reasons :
its much easier to cut/paste text lines, than move elements up/down with the mouse.
can be saved and deployed easily on a new Jenkins machine

You can edit the configuration before the job is run to add the initial build parameters. If you then have the build parameters also defined in the pipeline, the parameters will be overwritten with the ones defined in the pipeline.
However, for that first build, if the build parameters defined in the job configuration and the ones in the pipeline match, the build parameter values will be used in that initial run.

Related

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.

Storing Jenkins pipeline job metadata?

Is there a way where to store some metadata from Jenkins pipeline job, e.g:
We have a Jenkinsfile which builds a gradle project, creates docker image and pushes it to google cloud
Then a "Subjob" is launched which runs integration tests (IT) on that docker image. Subjob receives a couple of parameters (one of them - the generated docker image name)
Now sometimes that IT job fails, and I would like to re-run it from the main job view, so idealy:
we have a plugin which renders a custom button in blue ocean UI on the main job
By clicking that button a subjob is invoked again with the same parameters (plugin queries the jenkins api, get params of this job, and resubmits the subjob).
The problem ? How to get/set those parameters. I could not seem to find a mechanism for that, expect artifact storage. I could get away with that by creating a simple json/text file and uploading it as artifact, and then retrieving it in my plugin, but maybe there is a better way?
Stage restart is not coming to Scripted Pipelines so that does not look like ant option.
Maybe you can use the Jenkins API to get the details of the build?
https://your_jenkins_url.com/job/job_name/lastBuild/api/json?pretty=true
Instead of lastBuild you can also use the build number or one of lastStableBuild, lastSuccessfulBuild, lastFailedBuild, lastUnstableBuild, lastUnsuccessfulBuild, lastCompletedBuild
There is a parameters key there with all parameter names and values used in the build.
More details on https://your_jenkins_url.com/job/job_name/api/
Also, any reason you can't use the replay button in the IT job?

Jenkins Delivery Pipeline plugin and parameterized builds

I am using the Jenkins Delivery Pipeline plugin. I have some parameterized builds that are manually triggered. If I trigger a build from the project page, the build shows a screen where parameters need to be defined (as expected). However, when I start a parameterized build from the Delivery Pipeline plugin, it just starts the build without offering a screen. This is strange behavior, is it possible to get the pipeline plugin to show the parameterized build screen?
Thanks for your help!
I'm answering this question in general.
You need to use the Parameterized Trigger Plugin, or use the Build Pipeline Plugin. This issue with Delivery Pipeline plugin is still being solved by the Jenkins Team. See this link for the update about the issue at https://issues.jenkins-ci.org/browse/JENKINS-25685. You can get to know when it gets resolved from that link.
You can use the Build Pipeline plugin if it can be incorporated in your use case. There was a similar issue for the Build Pipeline plugin which is fixed now. It got fixed within 22 days (https://issues.jenkins-ci.org/browse/JENKINS-25427, https://github.com/jenkinsci/build-pipeline-plugin/pull/56). You can just hope that the same issue with the Delivery Pipeline plugin is fixed soon.
Can you provide me the version of Jenkins, environment and the plugin version? I can update my answer based on the answers you give.
When you are using the Delivery Pipeline plugin, and you have manually-triggered parameterized builds, as long as you configure the upstream job to pass along the parameters to the downstream job, when the "build trigger button" is clicked on the pipeline view page, the parameters are automatically passed along.
For instance, let's say you have a setup like this:
Compile_Project ---> Deploy_Project
Let's assume that you are passing a variable called versionNumber from the Compile_Project to the Deploy_Project jobs. Let's also assume that you're using Subversion for your SCM, and your versionNumber looks like 1.0.${SVN_REVISION}. ${SVN_REVISION} is automatically provided by Jenkins, so your version number will look something like 1.0.1234, where 1234 is the commit number provided by Subversion.
On your Delivery Pipeline view, let's say that it's configured to show 3 pipeline instances, and that manual triggers are enabled in the view settings. Your pipeline view page might look something like this (yay ASCII art!):
Compile_Project ---> Deploy_Project (>)
Compile_Project ---> Deploy_Project (>)
Compile_Project ---> Deploy_Project (>)
In this case, I'm using (>) to represent the manual trigger button. The button on the bottom would attempt to deploy version 1.0.1234, the middle button would attempt to deploy version 1.0.1235, and the top button would attempt to deploy version 1.0.1236, assuming your project has gotten consecutive SVN commits.
In order to pass the parameter from the Compile_Project to the Deploy_Project job, you need to do the following. (Note: it sounds like you've already done this part, but I'm including it just in case you might have missed a step, and also for the sake of completeness.)
In the Compile_Project job's configuration, as a Post-Build Action, choose "Build other projects (manual step)". In the "Downstream project names" box, enter Deploy_Project, and then from the "Add Parameters" drop-down, select "Predefined Parameters". In the "Parameters" text area that appears, create a parameter to pass along, which I'll call VERSION_NUMBER. What you'll enter in the text area is then VERSION_NUMBER=1.0.${SVN_REVISION}. This will enable the parameter to get passed from the Compile_Project to the Deploy_Project. However, you're not quite done yet.
In the Deploy_Project job's configuration, you need to set it up to accept the parameter you're passing into the job. To do so, configure the Deploy_Project, and check the "This build is parameterized" checkbox. Then add a String parameter from the "Add parameter" drop-down. In the "Name" field, enter VERSION_NUMBER. At this point, you can then use ${VERSION_NUMBER} in the Deploy_Project's configuration wherever you need in order to specify the correct version number of the project to deploy.

How to run parametrized build after commit to repository?

I have parametrized build job in Jenkins. It has configured SCM polling and the build job is started after new commit.
Parameters for this build job are location profiles defined in main pom.xml. Count of these profiles is static and persistent. So after every commit I need to build a project for the same profiles. One profile is started for one build.
It is able for manual triggering when I write profile name and start the build job. But after new commit this build job is started without parameters. So is there any way how to define list of parameters for build job - one parameter per one build.
An SCM change will trigger a related Jenkins job. Once. That's it.
When that job is triggered, and is configured with parameters, it does have default parameters.
For string parameters, it's the default value entered in configuration page (if you haven't entered one, the default is just that: none).
For single choice-style parameters, it's the top-most value.
For multi choice-style parameters, again, unless a default is provided in configuration, it's nothing.
If what you want is to trigger multiple runs of the same build for the same SCM change, then you've configured your jobs wrong.
Either create a matrix job, and configure an axis for every "profile" as you call it.
Or create multiple jobs, and chain them, so that first is triggered by SCM change, and the rest are triggered in sequence
If you only want to specify one default string that should be picked up while building using Poll SCM feature, then you should try using the following method:
Select the following options in Extended Choice Parameter:
In Simple Parameter Types section, go for Single Select in Parameter Type
Instead of Choose Source for Value, go for Choose Source for Default Value. Now enable the radio-button named Default Value. Enter whatever string you want to specify. Build will pick-up the given string as the default one.
Hopefully, it should work. At least, it works when i use Build periodically option. :)

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.

Resources