Jenkins pipeline groovy - jenkins

Below is my simple jenkins pipeline groovy script by which it will create a pipeline with these 2 stages and the jobs we want to build,I want the job names in that for build and code analysis should get updated in the script under job configuration everytime by taking the data from the UI where the user will just provide the build jobname and code analysis jobname using Eclipse -
jenkinsfile script :-
stage('Build'){
build job: 'job1'
}
stage('Code_Analysis'){
build job: 'job2'
}

While your question is a bit vague, I guess the problem is that your Jenkinsfile does not include the node statement:
node {
stage('Build'){
build job: 'job1'
}
stage('Code_Analysis'){
build job: 'job2'
}
}

So you want to pass job name as parameters. You can use string parameter e.g. JOB1 & JOB2. Then pass that to build job as it is. No need of curly braces or inverted commas like shown below-
stage('Build'){
build job: JOB1
}
stage('Code_Analysis'){
build job: JOB2
}

I suppose the job name can be utilised using ${name} variable in jenkins pipeline

Related

How to access pipeline name in the freestyle job

I have a pipeline which triggers a jenkins freestyle job.
How to access the pipeline name from the freestyle job.
I tried using ${BUILD_CAUSE} but it shows the value as UPSTREAMTRIGGER.
How to get the pipeline name?
JOB_NAME may contain the name of the job that you're after, unless it was triggered by an upstream job.
See a list of generic Jenkins environment variables that are available in most builds. The best way to find out what environment variables are available is to run sh 'printenv' in the steps.
If you need the name of the upstream job in a downstream job, I'd suggest setting a parameter on the downstream job and then referencing it when you trigger the job.
e.g. Upstream Pipeline job:
...
step {
build job: 'freestyle-downstream-job', parameters: [
string(name: 'upstream_job_name', value: "${JOB_NAME}")
]
}
...

Jenkins build multibranch pipeline from another multibranch pipeline

I have Jenkins setup with 2 multibranch pipeline which depend on each other let say multibranchPipelineA and multibranchPipelineB. I would like a job from multibranchPipelineA to build specific branch in multibranchPipelineA and wait the build to finish
I have tried use below from multibranchPipeleA Jenkinfile
stage('Build MiniApp Libs') {
steps {
build(
job: "../multibranchPipeleB/master",
propagate: true,
wait: true
)
}
}
But always receive No item named ../multibranchPipeleB/master found.
If I use single pipeline, let's say pipelineB, then the below work
../pipelineB
How can I build specific branch multibranchPipeline from another multibranchPipeline jobs? and wait the build to finish?
To build another multibranchPipeline you do not need .. before its name. So in your case just use:
job: "multibranchPipeleB/master"

Jenkins - Execute Pipeline from Specific folder

I have a main pipeline in jenkins that will checkout the code , compile, test and build, then push image to docker. This is the high level of CI pipeline that I have. Say job name "MainJobA"
I need to create a new job , just for JavaDoc generation. For that i have created a new script in Git Repo and configured the same in Pipeline job.
Now i need to execute this sub job of javadoc generation and publishing the html reports from the workspace of "MainJobA" . I need to run the SubJobA's pipeline stages from
/home/jenkins/workspace/MainJobA
How can i achieve this?
There is build step exist in jenkins declarative pipelines.
Use it like:
pipeline {
agent any
stages {
stage ("build") {
steps {
build 'Pipeline_B'
}
}
}
}

Is it possible to run a same jenkins build job in parallel stages in jenkins pipeline

I have a jenkin job name called A. I want this to run in parallel on different nodes in jenkins pipeline. When i try to run this, my job is in queue even when i try to run in different node.
sample code is below. I have added exact syntax of build job inside steps. For demonstration purpose i have added comments only. Thanks for help in advance
pipeline{
agent none
stages{
stage("A"){
parallel{
stage("A1"){
agent{
label 'node a'
}
steps{
// build job with name A
}
}
stage("A2"){
agent{
label 'node b'
}
steps{
// build job with name A
}
}
}
}
}
}
You can add a fake parameter to the job, and then call it with different value for the parameter for each call. This will force it to call the same job twice.

How can I pass parameters from a job to the other job when both the jobs are building inside a pipeline?

I have 3 jobs building in pipeline job as below
node('master') {
stage ('first')
parallel firstBranch: {
build 'job1'
build 'job2'
}
stage ('second')
build 'job3'
}
My job2 can take parameters and it should be passed from job1. How can I do that in pipeline?
I cannot use Parameterized Trigger Plugin as I don't want to trigger one job from another.
I want a way to pass the parameters from one job1 to job2 inside pipeline project.

Resources