triggering another jenkins job from jenkinsfile in pipeline - jenkins

I am trying to invoke a separate jenkins job whose direct job url is https://jenkins.example.com/job/jobName/. This job runs with one parameter name "branch", whose value is "Master".
Below is how I am giving in my Jenkinsfile, but when I run it, gives me error
ERROR: No item named https://jenkins.example.com/job/jobName found
if ("${params.buildParam}" == 'test' || !params.buildParam ){
stage('Test') {
def job = build job: 'https://jenkins.example.com/job/jobName/', parameters: [[$class: 'StringParameterValue', name: 'branch', value: 'Master']]
}
}

The build step takes a job name as parameter, not a URL. So try
build job: '/jobName'
to refer using the absolute path. Depending on where your pipeline job is, you might use something like the following as well:
build job: '../../jobName/'
btw. you can avoid string interpolation here:
if (params.buildParam == 'test' ...)

Related

Passing parameters down a Jenkins pipeline

I cannot get Jenkins to pass a string Parameter down the pipeline.
When I run the pipeline, I input the string value for $ServiceName and the job continues but it doesn't pass this param to the first job in the pipe (NEWSERVICE - Add New). In the jenkins file in the 'build' stage I've tried params.ServiceName, $params.ServiceName, env.ServiceName, $env.ServiceName, $env:ServiceName. No luck.
I need to pass the param to a Powershell build process in the NEWSERVICE job (which currently just echos the Param with $env:ServiceName - but it's always empty) Any help would be vastly appreciated.
pipeline {
agent any
parameters{
string(name: 'ServiceName',
defaultValue: '',
description: '',)
}
stages {
stage('Add new Service'){
steps {
build(job: "NEWSERVICE - Add New", parameters: [string(name: 'ServiceName', value: params.ServiceName)])
}
}
}
}
In Pipeline you need to pass string parameters like this:
parameters: [
[$class: 'StringParameterValue', name: 'ServiceName', value: ServiceName]
],
Refer this to understand different type of variable passing while calling a job from Jenkinsfile.
https://jenkins.io/doc/pipeline/steps/pipeline-build-step/
If you are looking to use a Jenkins env var in a powershell script - which i do all the time! - the env var has to be set up as such in Jenkins first. In other words,
env.myVar in a Jenkins context will be visible as $env:myVar in a PowerShell context. But you need to set it as an env var, local Jenkins variables won't be visible to a child script (unless passed in as a parameter). I have a detailed writeup here: https://stackoverflow.com/a/62538778/556078

How to pass choice parameter to call a job inside jenkins pipeline

How can I pass choice parameters for the downstream job when called inside a stage in the jenkins pipeline?
I tried the below solutions but none worked:
stage('build job') {
steps{
script{
build job: 'test',
parameters: [
choice(choices: "option1\noption2\noption3\n", description: '', name: 'choiceParam')
]
}
}
}
fails with java.lang.UnsupportedOperationException: no known implementation of class hudson.model.ParameterValue is using symbol ‘choice’
Tried these as well:
parameters:
[
[$class: 'ChoiceParameterValue', name: 'choiceParam', value: "1\n\2\n3\n"],
]
fails with java.lang.UnsupportedOperationException: no known implementation of class hudson.model.ParameterValue is named ChoiceParameterValue
I actually want to pass the choice parameter as a build parameter like "$choiceParam" for value so that I can just update the jenkins job configuration instead of always updating the values in the pipeline script
Can someone please help me with this
Thanks
When you are building a job via the Build step, you are kicking it off so you need to have "selected" a value.
In this instance you would pass in the desired 'String' choice. Not a list of choices. i.e. "1"
We create our list of params and then pass that in. So: our current job has these input params:
choice(name: 'ENV', choices: product, description: 'Env'),
choice(name: 'ENV_NO', choices: envParams(product), description: 'Env No'),
We pass these downstream by setting them:
List<ParameterValue> newParams = [
new StringParameterValue('ENV', params.ENV),
new StringParameterValue('ENV_NO', params.ENV_NO),
]
build(job: "job", parameters: newParams, propagate: false)

Jenkins Pipeline: build job with default paramter value if value not supplied

I have a Jenkins pipeline job that accepts 1 parameter with default value.
This job is building another job passing the parameter value.
Code of parent job:
node
{
stage ('build job_1')
{
build job: 'job_1',
parameters: [[$class: 'StringParameterValue', name: 'DROP_LOC', value: 'default_value']]
}
}
Expected behaviour:
If parent job get DROP_LOC parameter value, it should use it to build job_1
If parent job has no DROP_LOC parameter value, it should use the default value to build job_1
Please help how to do that? What is the correct code?
You can just use DROP_LOC variable directly
stage ('build job_1')
{
build job: 'job_1',
parameters: [[$class: 'StringParameterValue', name: 'DROP_LOC', value:DROP_LOC]]
}

How can I pass parameters from parallel builds downstream in a jenkins pipeline

I have been trying to set up a pipeline in jenkins which runs all my robot test builds in parallel and then, after they all finish, runs another build which includes sending 1 email with results for all the tests (rather than spamming with 1 per build).
I know that the robot plugin returns the variables $(ROBOT_PASSPERCENTAGE) and $(ROBOT_PASSRATIO) which we currently use. I was hoping there was a way of extracting them and using as a parameter for the downstream pipline build.
Just as a test I was trying groovy of the form below, but can't figure out how you get the variables and pass into the downstream build.
Any help appreciated.
stage('set up') {
node {
build job: 'setup', propagate: false
}
}
stage('run suites') {
parallel 'test set 1':{
node {
build job: 'test set 1', propagate: false
def 1_PASSPERCENTAGE = build.buildVariableResolver.resolve("ROBOT_PASSPERCENTAGE")
def 1_PASSRATIO = build.buildVariableResolver.resolve("ROBOT_PASSRATIO")
println "FOO=$CRM_PASSPERCENTAGE"
println "FOO=$CRM_PASSRATIO"
}
}, 'test set 2':{
node {
build job: 'thankQ Robot Mission Personnel Tests', propagate: false
def 2_PASSPERCENTAGE = build.buildVariableResolver.resolve("ROBOT_PASSPERCENTAGE")
def 2_PASSRATIO = build.buildVariableResolver.resolve("ROBOT_PASSRATIO")
println "FOO=$MP_PASSPERCENTAGE"
println "FOO=$MP_PASSRATIO"
}
}
}
stage('results') {
node {
println "FOO=$2_PASSPERCENTAGE"
println "FOO=$2_PASSRATIO"
println "FOO=$1_PASSPERCENTAGE"
println "FOO=$1_PASSRATIO"
}
}
From Jenkins pipeline steps reference, you can call a downstream job with parameters like this :
build job: downstreamJob, parameters: [
[$class: 'StringParameterValue', name: 'passPercentage', value: "${1_PASSPERCENTAGE}"],
[$class: 'StringParameterValue', name: 'passRatio', value: "${1_PASSRATIO}"]
]
As for how to get your Robot variables I have never used it but I guess you could always use the URL of the test build (e.g. your test set 1 job) and parse the log file or the build page for the variables you are looking for. Something like this :
def robotLog = script: 'curl http://your-jenkins/job/test-set-1/lastBuild/robot.log', returnStdout: true // First determine which URL corresponds to the robot.log file, or use the main page of your build.
def percentageMatcher = robotLog.trim() =~ 'Pass percentage.*(\\d+)%' // Again, find the exact regex here
def 1_PASSPERCENTAGE = percentageMatcher[0][1]
... // Same thing with pass ratio...

How can I trigger another job from a jenkins pipeline (jenkinsfile) with GitHub Org Plugin?

How can I trigger build of another job from inside the Jenkinsfile?
I assume that this job is another repository under the same github organization, one that already has its own Jenkins file.
I also want to do this only if the branch name is master, as it doesn't make sense to trigger downstream builds of any local branches.
Update:
stage 'test-downstream'
node {
def job = build job: 'some-downtream-job-name'
}
Still, when executed I get an error
No parameterized job named some-downtream-job-name found
I am sure that this job exists in jenkins and is under the same organization folder as the current one. It is another job that has its own Jenkinsfile.
Please note that this question is specific to the GitHub Organization Plugin which auto-creates and maintains jobs for each repository and branch from your GitHub Organization.
In addition to the above mentioned answers: I wanted to start a job with a simple parameter passed to a second pipeline and found the answer on http://web.archive.org/web/20160209062101/https://dzone.com/refcardz/continuous-delivery-with-jenkins-workflow
So i used:
stage ('Starting ART job') {
build job: 'RunArtInTest', parameters: [[$class: 'StringParameterValue', name: 'systemname', value: systemname]]
}
First of all, it is a waste of an executor slot to wrap the build step in node. Your upstream executor will just be sitting idle for no reason.
Second, from a multibranch project, you can use the environment variable BRANCH_NAME to make logic conditional on the current branch.
Third, the job parameter takes an absolute or relative job name. If you give a name without any path qualification, that would refer to another job in the same folder, which in the case of a multibranch project would mean another branch of the same repository.
Thus what you meant to write is probably
if (env.BRANCH_NAME == 'master') {
build '../other-repo/master'
}
You can use the build job step from Jenkins Pipeline (Minimum Jenkins requirement: 2.130).
Here's the full API for the build step: https://jenkins.io/doc/pipeline/steps/pipeline-build-step/
How to use build:
job: Name of a downstream job to build. May be another Pipeline job, but more commonly a freestyle or other project.
Use a simple name if the job is in the same folder as this upstream Pipeline job;
You can instead use relative paths like ../sister-folder/downstream
Or you can use absolute paths like /top-level-folder/nested-folder/downstream
Trigger another job using a branch as a param
At my company many of our branches include "/". You must replace any instances of "/" with "%2F" (as it appears in the URL of the job).
In this example we're using relative paths
stage('Trigger Branch Build') {
steps {
script {
echo "Triggering job for branch ${env.BRANCH_NAME}"
BRANCH_TO_TAG=env.BRANCH_NAME.replace("/","%2F")
build job: "../my-relative-job/${BRANCH_TO_TAG}", wait: false
}
}
}
Trigger another job using build number as a param
build job: 'your-job-name',
parameters: [
string(name: 'passed_build_number_param', value: String.valueOf(BUILD_NUMBER)),
string(name: 'complex_param', value: 'prefix-' + String.valueOf(BUILD_NUMBER))
]
Trigger many jobs in parallel
Source: https://jenkins.io/blog/2017/01/19/converting-conditional-to-pipeline/
More info on Parallel here: https://jenkins.io/doc/book/pipeline/syntax/#parallel
stage ('Trigger Builds In Parallel') {
steps {
// Freestyle build trigger calls a list of jobs
// Pipeline build() step only calls one job
// To run all three jobs in parallel, we use "parallel" step
// https://jenkins.io/doc/pipeline/examples/#jobs-in-parallel
parallel (
linux: {
build job: 'full-build-linux', parameters: [string(name: 'GIT_BRANCH_NAME', value: env.BRANCH_NAME)]
},
mac: {
build job: 'full-build-mac', parameters: [string(name: 'GIT_BRANCH_NAME', value: env.BRANCH_NAME)]
},
windows: {
build job: 'full-build-windows', parameters: [string(name: 'GIT_BRANCH_NAME', value: env.BRANCH_NAME)]
},
failFast: false)
}
}
Or alternatively:
stage('Build A and B') {
failFast true
parallel {
stage('Build A') {
steps {
build job: "/project/A/${env.BRANCH}", wait: true
}
}
stage('Build B') {
steps {
build job: "/project/B/${env.BRANCH}", wait: true
}
}
}
}
The command build in pipeline is there to trigger other jobs in jenkins.
Example on github
The job must exist in Jenkins and can be parametrized.
As for the branch, I guess you can read it from git
Use build job plugin for that task in order to trigger other jobs from jenkins file.
You can add variety of logic to your execution such as parallel ,node and agents options and steps for triggering external jobs. I gave some easy-to-read cookbook example for that.
1.example for triggering external job from jenkins file with conditional example:
if (env.BRANCH_NAME == 'master') {
build job:'exactJobName' , parameters:[
string(name: 'keyNameOfParam1',value: 'valueOfParam1')
booleanParam(name: 'keyNameOfParam2',value:'valueOfParam2')
]
}
2.example triggering multiple jobs from jenkins file with conditionals example:
def jobs =[
'job1Title'{
if (env.BRANCH_NAME == 'master') {
build job:'exactJobName' , parameters:[
string(name: 'keyNameOfParam1',value: 'valueNameOfParam1')
booleanParam(name: 'keyNameOfParam2',value:'valueNameOfParam2')
]
}
},
'job2Title'{
if (env.GIT_COMMIT == 'someCommitHashToPerformAdditionalTest') {
build job:'exactJobName' , parameters:[
string(name: 'keyNameOfParam3',value: 'valueOfParam3')
booleanParam(name: 'keyNameOfParam4',value:'valueNameOfParam4')
booleanParam(name: 'keyNameOfParam5',value:'valueNameOfParam5')
]
}
}

Resources