How to Send Gradle Parameters TO Jenkins? - jenkins

I’ve seen many articles for sending Jenkins parameters to Gradle, which are passed as System variables, but have not come across doing the opposite. In my build.gradle file I have specified 2 parameters for a file name and version number. In order to pass this file name to the next job in the pipeline, I’d like to send the parameters to the next Jenkins job so it can make use of the file.
Is sending gradle params back to Jenkins possible using built in functionality??

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.

How to programmatically generate config.xml from Jenkinsfile?

Jenkins has the ability to upload new jobs via its REST API. Those new jobs require an XML document which, to the best of my searching, has no schema available.
When creating jobs as part of an SCM repo, you can include a Jenkinsfile and it automatically gets translated into a job with the config.xml filled out.
I tried creating a minimal config.xml and including the Jenkinsfile content in the <script>…</script> section of the xml file. This works for trivial jobs, but does not work for jobs that have parameters: The job gets uploaded as a parameterless job. The first time you trigger a build of the job, it fails - but then the job turns into a job-with-parameters, and can properly be built.
How do I convert a Jenkinsfile, possibly with parameters or other "advanced" features, into a working config.xml file on the first try? Or, alternatively, is it possible to directly upload the Jenkinsfile to the Jenkins REST API to create the job?
Thanks in advance,
— Johnson

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?

How to send first pipe result to second one in Jenkins pipeline?

we use Jenkins as CI tools.
we want to separate login from other process.
we define a job for login, in this job we validate user and if user is valid we get user id.
at other job we need to have user id to generate result,Our problem is how we can send first job result(here:user id) to second one?
You can do this with the use of two plugins:
EnvInject Plugin
Parameterize Trigger Plugin
EnvInject allows you to inject variables into the Jenkins environment so they are available even after that build step.
Parameterize Trigger plugin allows you to pass information in this build job to another build job you want to start as parameters.
Once you've determined the username (I assume in some sort of batch or bash, you don't note the OS) you'll need to write it to a file on the system using a key=value pair. Then use EnvInject to get the value from the file into the jenkins environment. After that you'll use the parameterize trigger plugin to build the next job with parameters. This will require that you check the This build is parameterized box in the second job and that you define the appropriate parameters (perhaps with a default value that you can use to intentionally fail the build if you don't get a good value).

Retrieve a variable from child job in jenkins

I have a job that is reused/called from two parent jobs, I need to retrive a variable from this job to use it in each parent job.
The shell variable is a list of artifacts with size and md5sum.
The goal is to use this list in Editable email notification.
how I can retrieve it, let's call it ${artifacts_list}.
Thank you,
Although i have not tested it, you can try this approach: You can echo the variables and their values in a file (name=value) and read the file in your parent job. For making those variables available in your parent job's environment, you might need EnvInject Plugin
A cumbersome way is to use the Jenkins API to read the JSON of the other job's specific run, and extract the parameters from it.
An easier way is to:
Export job's parameters to file (use Export Parameters Plugin for that, or simply echo out the required param in format param=value like the other answer suggested)
Archive the exported parameters file at the end of the build using "Archive Artifacts" post-build action
In the downstream job, use Copy Artifacts plugin to bring in that file
Use EnjInject plugin to read the file and make it available as environment variables.

Resources