Jenkins build trigger - jenkins

I used jenkins remote trigger build option at job A and triggered another job B. When I open job B build, I could see "Started by remote project <path to job A>". I am trying to get the value from job B after execution but I not working. I tried working with BuildUser plugin which gave null output. Could someone help me to find a way to find the information?

The Parameterized Remote Trigger Plugin job setup options have a section Build Info with a field Parameters. Define parameters there like:
TRIGGERED_BY_JOB=${JOB_NAME}
TRIGGERED_BY_BUILD_NO=${BUILD_NUMBER}
and use them accordingly in your job B.
See Jenkins Set Environment Variables for other available information.

Related

Passing git changes to a file for use in later Jenkins jobs

ENVIRONMENT
I have a series of jobs in Jenkins where Job #1 is an initial build and job #n is deployment to production.
Only the first few jobs are connected, and all jobs are parameterized.
I only build the one time, and if that build is successful post build steps trigger a job to deploy that build to dev.
After deployment to dev I have to manually go to Jenkins and click to run the job to deploy to the next region/environment.
THE PROBLEM
I am able to successfully pass the $GIT_COMMIT to the downstream jobs because as a workspace based environment variable during job-run I can write it to a file for use later.
However, $CHANGES is an email-ext specific variable and I am having issues writing its contents to a file I can pass to downstream jobs for the purpose of tracking what the current build is deploying in a given environment.
My unfamiliarity with Groovy and my weak Google-fu have made trying pre-send script and post-send script difficult to work with to get the data I want passed to downstream jobs.
WHAT I HAVE TRIED
What works
I am able to send HTML emails using email-ext
I am able to pass $GIT_COMMIT to a file and use it in downstream jobs
I am able to load the contents of a file into an email using email-ext
What doesn't work
I cannot seem to use Groovy in a pre-send script to output the $CHANGES to a file for use.
Trying to output $CHANGES to a file in a post-send script also does not work, but probably wouldn't be best anyway since it would likely come after any opportunity to pass the file to a downstream job.
WHAT I HAVE NOT TRIED
I have seen suggestions to use the changelog registered by the SCM process, which apparently is recorded in XML which must then be parsed by either the initial build job or downstream jobs if it is to be formatted for inclusion in an HTML email.
HELP
If anyone has any suggestion on what to try next I would appreciate it. I'm pulling my hair out.
You can use this groovy script to access the build environment parameters of an arbitrary job on the same jenkins instance.
To execute the script you have to install the groovy plugin and execute the script as system groovy script.
import jenkins.model.Jenkins
job = Jenkins.instance.getJob("freestyle-test")
numbuilds = job.builds.size()
if (numbuilds == 0) { return }
lastbuild = job.builds[numbuilds - 1]
println 'JOB: ' + job.fullName
println ' -> lastbuild: ' + lastbuild.displayName + ' = ' + lastbuild.result
println ' -> lastbuild someEnv: ' + build.environment.get("SOME_ENV")
The coupling to the job is over the job name.
The selected build is the latest.

How can I analyze console output data from a triggered jenkins job?

I have 2 jobs 'job1' and 'job2'. I will be triggering 'job2' from 'job1'. I need to get the console output of the 'job1' build which triggered 'job2' and want to process it in 'job2'.
The difficult part is to find out the build number of the upstream job (job1) that triggered the downstream job (job2). Once you have it, you can access the console log, e.g. via ${BUILD_URL}consoleOutput, as pointed out by ivoruJavaBoy.
How can a downstream build determine by what job and build number it has been triggered? This is surprisingly difficult in Jenkins:
Solution A ("explicit" approach): use the Parameterized Trigger Plugin to "manually" pass a build number parameter from job1 to job2. Apart from the administrational overhead, there is one bigger drawback with that approach: job1 and job2 will no longer run asynchronously; there will be exactly one run of job2 for each run of job1. If job2 is slower than job1, then you need to plan your resources to avoid builds of job2 piling up in the queue.
So, some solution "B" will be better, where you extract the implicit upstream information from Jenkins' internal data. You can use Groovy for that; a simpler approch may be to use the Job Exporter Plugin in job2. That plugin will create a "properties" (text) file in the workspace of a job2 build. That file contains two entries that contain exactly the information that you're looking for:
build.upstream.number Number of upstream job that triggered this job. Only filled if the build was triggered by an upstream project.
build.upstream.project Upstream project that triggered this job.
Read that file, then use the information to read the console log via the URL above.
You can use the Post Build Task plugin, then you can get the console output with a wget command:
wget -O console-output.log ${BUILD_URL}consoleOutput

In Jenkins how to trigger another build job based on the build parameter passed to current build job?

I have build jobs like a-build, a-deploy. b-build, b-deploy.
*-deploy are downstream job for *-build jobs. So they look like,
a-build
|
+-a-deploy
b-build
|
+-b-deploy
Now I have another job X-build. It accepts a-build, b-build etc as a parameter. So I if I run X-build with a-build as parameter it should complete with a post build action that triggers a-deploy. How can that be done?
You can accomplish this quite easily if you receive the job name in a parameter.
You can then use "Call/Trigger Builds on Other Projects" step, and use the Parameter you receive in the job name:
If the step is not available to you via Post Build menu, you can get to it via "Execute set of scripts" post-build step.

Jenkins Promoted Build Parameter Value

I have a jenkins job that runs some tests and promotes the build if all tests pass.
I then have a second job that has a 'Promoted build parameter' which is used for deployments.
THe idea is that the deply job should let you pick one of the prompted builds for deploying. The issue I'm having is that I can pick a promoted build, but I have not idea how I access information about the build.
I've named the build parameter
SELECTED_BUILD
And according to the docs this should then be available via the environment.
The problem is that it doesn't seem to be bound to anything.
If I run a build step to exectute this sheel script:
echo $SELECTED_BUILD
echo ${SELECTED_BUILD}
The values are not interpolated / set.
Any idea how I can access the values of this param?
Many Thanks,
Vackar
Inject the information as variable user jenkins plugin (eject evn variable).
while triggering parameterized build on other job, pass above variable as parameter to next job.

Can one Jenkins Trigger a job on a remote jenkins

I have 2 Jenkins hosts, and would like First Jenkins to trigger a job on remote Jenkins based on "SUCCESS" in result on the first one.
I have looked at various plugins , but they all seem to indicate ONE Jenkins host, where multiple jobs can be chained in this manner.
Meanwhile, a jenkins plugin became available which makes it a lot easier:
https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Remote+Trigger+Plugin
It's very easy to do using cURL requests, no need for plugins or master>slave relations. It took me 5 minutes from beginning to start.
Use the following manual:
https://www.nczonline.net/blog/2015/10/triggering-jenkins-builds-by-url/
You could set up a downstream job on host1 that only builds if first job on host1 succeeds.
In this job you would trigger a remote build much like i described it in this answer
Step 1: Install following plugins in both Jenkins.
Generic Webhook Trigger: Job can be triggered from http request.
HTTP Request Plugin: To send http request as build step
Any Build Step Plugin: To use any build step in post-build action.
Step 2: Configure job to be triggered(Jenkins B).
Select generic webhook trigger in build trigger and generate a token and paste.
After saving this job can be triggered by sending a http request to
http://JENKINS_B_URL/generic-webhook-trigger/invoke?token=TOKEN_VALUE
Step 3: In master Jenkins(Jenkins A) configure flexible publish settings in configure system to allow use all build steps as post build actions.
Step 4: In post build actions add another step “Flexible publish”.
Using this any build action can be used as post-build action. Add a HTTP Request action.
Provide Jenkins B webhook url in url field and save.
Yes. Configure your Jenkins nodes and label them, say masterand slave (Manage Jenkins -> Manage Nodes).
1) Configure Job A and specify that it can only run on master ("Restrict where this project can be run" and in the label field put master).
2) Configure Job B so that it is only triggered if Job A is successful:
"Post-build Actions" -> "Trigger only if build succeeds"
3) Pin Job B to slave similar to step 1.

Resources