Execute only selected jobs using Jenkins pipe line - jenkins

I have this jenkins pipe line which has multiple stages. Inside these stages, there are multiple jobs being executed.
When I build the job I'd like to have a set of check boxes and the pipe line should build only what I've checked inside the pipeline stages. Is there any plugins or methods I can use to achieve this?
Sample pipeline code.
As per below example, there are jobs called job_A1, job_B1, job_C1, job_D1, job_A2, job_B2, job_C2 and job_D2. If I click Build with parameters, it should prompt me check boxes and I should be able to check any job I want so that the pipe line will build only the ones I checked.
Thanks in Advance.
pipeline {
agent {label 'server01'}
stages {
stage('Build 01') {
steps {
parallel (
"BUILD A1" : {
build job: 'job_A1',
parameters:[
string(name: 'PARAM01', value: "$PARAM01"),
string(name: 'PARAM02', value: "$PARAM02")
]
},
"BUILD B1" : {
build job: 'job_B1',
parameters:[
string(name: 'PARAM01', value: "$PARAM01"),
string(name: 'PARAM02', value: "$PARAM02")
]
},
"BUILD C1" : {
build job: 'job_C1',
parameters:[
string(name: 'PARAM01', value: "$PARAM01"),
string(name: 'PARAM02', value: "$PARAM02")
]
},
"BUILD D1" : {
build job: 'job_D1',
parameters:[
string(name: 'PARAM01', value: "$PARAM01"),
string(name: 'PARAM02', value: "$PARAM02")
]
},
)
}
}
stage('Build 02') {
steps {
parallel (
"BUILD A2" : {
build job: 'job_A2',
parameters:[
string(name: 'PARAM01', value: "$PARAM01"),
string(name: 'PARAM02', value: "$PARAM02")
]
},
"BUILD B2" : {
build job: 'job_B2',
parameters:[
string(name: 'PARAM01', value: "$PARAM01"),
string(name: 'PARAM02', value: "$PARAM02")
]
},
"BUILD C2" : {
build job: 'job_C2',
parameters:[
string(name: 'PARAM01', value: "$PARAM01"),
string(name: 'PARAM02', value: "$PARAM02")
]
},
"BUILD D2" : {
build job: 'job_D2',
parameters:[
string(name: 'PARAM01', value: "$PARAM01"),
string(name: 'PARAM02', value: "$PARAM02")
]
},
)
}
}
}
}

Thanks #mbn217 for your answer, but ExtendedChoice parameter didn't help much in my scenario.
Anyway, I could do it using boolean parameters and calling it inside the pipeline using the script tag.
Example pipeline script
stage ('BUILD A') {
steps {
script {
if (params.get('boolA',true)) {
build job: '_build_A', parameters: [string(name: 'param1', value: "$param1"),string(name: 'param2', value: "$param2")]
} else {
echo "A is not selected to build"
}
}
}
}
stage ('BUILD B') {
steps {
script {
if (params.get('boolB',true)) {
build job: '_build_B', parameters: [string(name: 'param1', value: "$param1"),string(name: 'param2', value: "$param2")]
} else {
echo "B is not selected to build"
}
}
}
}

You can use ExtendedChoiceParameter to accomplish what you want. Basically you will nee to parametrize job Names too using this jenkins plugin.
You can use a list of checkboxes as shown in the screen shot

Related

How to pass file parameters to upstream pipeline job in build step plugin

def BUILD_USER = currentBuild.getBuildCauses('hudson.model.Cause$UserIdCause')
pipeline {
agent {label "master"}
parameters {
string(name: 'BUILD', defaultValue: '123')
booleanParam(name: 'Deploy', defaultValue: 'true')
booleanParam(name: 'Upgrade_Config', defaultValue: 'true')
booleanParam(name: 'SchemaComparison', defaultValue: 'false')
booleanParam(name: 'Publish_Server', defaultValue: 'false')
}
stages {
stage ('Start Deployment') {
agent {label "master"}
steps{
script{
sh '''
rm -rf /params/parameters
cd /params
echo $BUILD_USER
python3 buildParameters.py --Build=$BUILD --Publish_VM=$Publish_Server --userName=BUILD_USER --Upgrade_Config=$Upgrade_Config
'''
file = readFile('/params/parameters.txt')
}
}
}
stage ('UpgradeConfigurations') {
when {
expression { params.Deploy == true }
}
agent {label "master"}
environment {
file = "${file}"
}
steps{
script{
println("${file}")
build(job: 'UpgradeConfigurations', parameters: [ file(name: 'parameters', file: "${file}"), string(name: 'build_uniqe_id' , value: "${BUILD_USER}") , booleanParam(name: 'Deploy' , value: "${Deploy}") , booleanParam(name: 'SchemaComparison' , value: "${SchemaComparison}")], propagate: false, wait: false )
}
}
}
}
}
buildParameters.py file generate some additional parameters in parameters.txt file on master vm and I am trying to pass it to the upstream job UpgradeConfigurations
Upstream job UpgradeConfigurations is getting started but file parameters are not getting passed as parameters to it.
I have tried using base64file as well but no luck.
Referred Build Plugin doc:
https://www.jenkins.io/doc/pipeline/steps/pipeline-build-step/

can we run Jenkins file in pipeline?

I've Pipeline Generic Webhook from Bitbucket, this is a job to trigger another job.
currentBuild.displayName = "Generic-Job-#" + env.BUILD_NUMBER
pipeline {
agent any
triggers {
GenericTrigger(
genericVariables: [
[key: 'actorName', value: '$.actor.display_name'],
[key: 'TAG', value: '$.push.changes[0].new.name'],
[key: 'REPONAME', value: '$.repository.name'],
[key: 'GIT_URL', value: '$.repository.links.html.href'],
],
token: '11296ae8d97b2134550f',
causeString: ' Triggered on $actorName version $TAG',
printContributedVariables: true,
printPostContent: true
)
}
stages {
stage('Build Job DEVELOPMENT') {
when {
expression { return params.TARGET_ENV == 'DEVELOPMENT' }
}
steps {
build job: 'DEVELOPMENT',
parameters: [
[$class: 'StringParameterValue', name: 'FROM_BUILD', value: "${BUILD_NUMBER}"],
[$class: 'StringParameterValue', name: 'TAG', value: "${TAG}"],
[$class: 'StringParameterValue', name: 'GITURL', value: "${GIT_URL}"],
[$class: 'StringParameterValue', name: 'REPONAME', value: "${REPONAME}"],
[$class: 'StringParameterValue', name: 'REGISTRY_URL', value: "${REGISTRY_URL}"],
]
}
}
}
}
Another Pipeline
pipeline {
agent any
stages {
stage('Cleaning') {
steps {
cleanWs()
}
}
def jenkinsFile
stage('Loading Jenkins file') {
jenkinsFile = fileLoader.fromGit('Jenkinsfile', "${GIT_URL}", "${TAG}", null, '')
}
jenkinsFile.start()
}
}
can i run Jenkinsfile in Pipeline ? Because every project I make has a different Jenkinsfile, it can't be the same, but when I run this it doesn't execute the Jenkinsfile
it works for me :D
Sample Pipeline
stage 'Load a file from GitHub'
def jenkinsFile = fileLoader.fromGit('<path-jenkinsfile>', "<path-git>", "<branch>", '<credentials>', '')
stage 'Run method from the loaded file'
jenkinsFile
pipeline {
agent any
stages {
stage('Print Hello World Ke #1') {
steps {
echo "Hello Juan"
}
}
}
}
before run the pipeline, you must install plugin "Pipeline Remote Loader Plugin Version"

changing the Jenkins pipeline to use password parameter or Credentials parameter instead od Hudson password

Below is the pipeline script for my job:
pipeline {
agent any
stages {
stage('Runbook database deploy job') {
steps {
withCredentials([
usernamePassword(credentialsId: 'RUNBOOK_RDS_DBADMIN', passwordVariable: 'DB_APPTEAM_PASS', usernameVariable: 'DB_APPTEAM_USER'),
usernamePassword(credentialsId: 'nexus_download_user', passwordVariable: 'NEXUS_APPTEAM_PASS', usernameVariable: 'NEXUS_APPTEAM_USER')
]) {
build job: 'DBA-TSS/db_aurora-mysql_DEPLOY/', parameters: [
string(name: 'INSTALL_TYPE', value: "${INSTALL_TYPE}"),
string(name: 'SDLC', value: "${SDLC}"),
string(name: 'DB_ARTIFACT', value: "${DB_ARTIFACT}"),
string(name: 'DEV_NEXUS_ID', value: NEXUS_APPTEAM_USER),
[$class: 'hudson.model.PasswordParameterValue', name: 'DEV_NEXUS_PASSWORD', value:hudson.util.Secret.fromString(NEXUS_APPTEAM_PASS)],
string(name: 'DEV_NEXUS_REPO', value: "${DEV_NEXUS_REPO}"),
string(name: 'DB_USERNAME', value: DB_APPTEAM_USER),
[$class: 'hudson.model.PasswordParameterValue', name: 'DB_PASSWORD', value:hudson.util.Secret.fromString(DB_APPTEAM_PASS)],
string(name: 'REQUESTER_EMAIL', value: "${REQUESTER_EMAIL}")
]}
}
}
}
}
And now as hudson is not supported , we want to replace it with Credentials id or password parameter to pass it to downstream job for password variable. can you please help us on this.

How to pass parameter value to stage build in Jenkins pipeline?

I have a pipeline created which takes the parameter value from the user. I want to trigger a jenkin job using this parameter.
How can I pass the parameter value to the build's parameter.
Here is my code:
pipeline {
agent any
parameters {
string(name: 'SYSTEM', defaultValue: '', description: 'Enter array. Example:SYS-123')
string(name: 'EMail', defaultValue: '', description: 'Enter email id')
}
stages {
stage('Example') {
steps {
echo "Hello ${params.SYSTEM}"
echo "Hello ${params.EMail}"
}
}
stage('core-rest-api-sanity') {
steps {
build job: 'xyz', parameters: [string(name: 'E-Mail', value: ${params.EMail}), string(name: 'SYSTEM', value: ${params.SYSTEM})]
}
}
}
}
In the above code, I am taking email and system details from the user. Then I want to trigger my job "xyz" which would require these parameter.
pipeline {
agent any
parameters {
string(name: 'SYSTEM', defaultValue: '', description: 'Enter array. Example:SYS-123')
string(name: 'EMail', defaultValue: '', description: 'Enter email id')
}
stages {
stage('Example') {
steps {
echo "Hello ${params.SYSTEM}"
echo "Hello ${params.EMail}"
}
}
stage('core-rest-api-sanity') {
steps {
build job: 'xyz', parameters: [string(name: 'E-Mail', value: params.EMail),
string(name: 'SYSTEM', value: params.SYSTEM)]
}
}
}
}

How to setup parameters into jenkins pipeline script

We have a jenkins jobs that run autotests with parameters:
HOST;
EXPEIMENT;
TAKE_NEW_SCREENSHOT;
XML_NAME.
All of this parameters have default values,
see screenshot before running parametrizing job:
I need to run several jobs simultaneously with only 2 parameters: HOST and EXPERIMENT.
I created next pipeline-script:
def tasks = [:]
parameters {
string(name: 'HOST', defaultValue: 'www', description: 'host: www, dev3, etc',)
string(name: 'EXPERIMENT', defaultValue: 'withoutExperiment',)
}
tasks['Actions MyBox'] = {
build job: 'MyDocs_Actions_And_Manage_Buttons_MyBox_Tests', parameters: [
string(name: 'HOST', value: 'www'),
string(name: 'EXPERIMENT', value: 'withoutExperiment'),
booleanParam(name: 'TAKE_NEW_SCREENSHOT', value: false),
string(name: 'XML_NAME', value: 'my_docs_actions_buttons_mybox_tests')
]
}
tasks['DashBoard General'] = {
build job: 'DashBoard_General_Tests', parameters: [
string(name: 'HOST', value: 'www'),
string(name: 'EXPERIMENT', value: 'withoutExperiment'),
booleanParam(name: 'TAKE_NEW_SCREENSHOT', value: false),
string(name: 'XML_NAME', value: 'my_docs_dash_board_general_tests')
]
}
tasks['Actions InBox'] = {
build job: 'MyDocs_Actions_Buttons_InBox_Tests', parameters: [
string(name: 'HOST', value: 'www'),
string(name: 'EXPERIMENT', value: 'withoutExperiment'),
booleanParam(name: 'TAKE_NEW_SCREENSHOT', value: false),
string(name: 'XML_NAME', value: 'my_docs_actions_buttons_inbox_tests')
]
}
parallel tasks
and specified parameters in "General" pipeline configuration:
But when I run this pipeline item with parameter value != default value, for example specify HOST = dev12,
anyway all jobs running simultaneously with default parameter values and build shows null specified parameter,
Help me please define a problem.
You're passing hardcoded values to your tasks. For example, you defined
tasks['Actions MyBox'] = {
build job: 'MyDocs_Actions_And_Manage_Buttons_MyBox_Tests', parameters: [
string(name: 'HOST', value: 'www'),
string(name: 'EXPERIMENT', value: 'withoutExperiment'),
booleanParam(name: 'TAKE_NEW_SCREENSHOT', value: false),
string(name: 'XML_NAME', value: 'my_docs_actions_buttons_mybox_tests')
]
}
In this case all parameters are hardcoded and each time when pipeline is executed the value of HOST will be www. And that's why you have null in the HOST parameter description in build execution info (because you're not specifying it in build job command).
So, you need to use something like string(name:'HOST', value: "${params.HOST}")

Resources