Build Job from Jenkins Pipeline with variable - jenkins

I'm not able to build a job from jenkins pipeline passing a variable for job name. Please find below the code snippet. If I replace ${service} with 'microservice' it will trigger the job
service = 'microservice'
echo "TESSSSSSSSTTT ${service}"
build(job: "'${service}'", parameters: [string(name: 'ENVNAME', value: 'uat')])
The error faced is:
[Pipeline] echo
TESSSSSSSSTTT microservice
[Pipeline] build
[Pipeline] End of Pipeline
ERROR: No item named 'microservice' found
Finished: FAILURE
I'm using this method because I want to hit a build command for all the microservices passing the names with multi-line string parameter, finally I want to set this build into a loop.

I assume your job is not called 'microservice' in Jenkins but microservice (without the ticks).
So change your build line to not include single quotes after the double quote:
build(job: "${service}", parameters: [string(name: 'ENVNAME', value: 'uat')])
Did some simple test with my jenkins instance and was able to call a job by setting the service variable to an existing job name.

Related

How to get build number as a parameter for downstream job from upstream job with declarative pipeline code

I have Two jobs like one is CI jod & another one is CD. I want CI build number should use on CD number.. Can you please help me with declarative pipeline script to get build number as a parameter. here CI job is calling CD job.
Jenkins already provides a simple means to access the number of the current build using env.BUILD_NUMBER. So if you wanted to pass the build number of CI to the downstream job CD, you could do
build([
job : 'CD',
parameters: [
string(name: 'MAIN_BUILD_NUMBER', value: "${env.BUILD_NUMBER}")
]
])
Then in the CD job, declare a parameter like this:
parameters {
string(defaultValue: null, description: 'Build No', name: 'MAIN_BUILD_NUMBER')
}
You should then be able to use ${env.MAIN_BUILD_NUMBER} anywhere in your CD jobs' Jenkinsfile.

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}")
]
}
...

How to pass NodeParameterValue as paramter in "build" pipeline step in jenkins

I have a Jenkins job that accepts NODE as parameter. I want to trigger the job from Jenkins pipeline using "build step" plugin. Passing the value as string is not working as the job is landing on random node
Here is my pipeline code. RepairAgentWorkspace expect NODE_TO_REPAIR to be a node type.
node{
build job: 'RepairAgentWorkspace',
parameters: [string(name: 'NODE_TO_REPAIR', value: "git_agent")]
}
Above code triggers the job but it's not landing on "git_agent" agent. Any idea how can we pass node as param?

How do you pass parameter into a pipeline script but not make it a parameterized build in the jenkins UI

How do you pass parameter into a pipeline script but not make it a parameterized build in the jenkins UI.
This script will get used in more then one place and I would like to be able to pass some values into the script but not make it a parameterized build where the users can set them.
In its simplest form, here's how you would do that.
The job that you want to pass parameters to looks like this (scripted pipeline). Let's call this job "ParamJob".
print "Parameter passed: ${params.myParam}"
The job that you would kick "ParamJob" off with would look like this ..
build job: 'ParamJob' , parameters:[
string(name: 'myParam', value: 'Test parameter')
]
And the output of "ParamJob" is
Parameter passed: Test parameter

Can a freestyle Jenkins job configured with only Deploy war to container plugin be parameterized + corresponding groovy syntax in jenkins pipeline

I tried/google to get equivalent groovy snippet which would do exact activity which "Deploy war to container" does for a Freestyle job.
Whatever I found in more than a week perm-comb are concluding to use SSH-Agent (which requires a private key generation on unix system, which I cannot do).While "Deploy war to container" takes only username and password and deploys war to remote Unix server.[This username and password is unix system user.]
Leaving this request I tried to create a parameterized Jenkins freestyle job which has only a post build action.In that post build action "Deploy war to container" section should accept $warFilename , $tomcatUrl like.
But this job is not working.
Jenkins job config image
I tried below two syntax ,First one is for calling parameterized job.
2nd One is for using username_pasword to execute scp.
stage ('deploy:tomcat') {
steps{
build job: 'TOMCAT_DEPLOYMENT', parameters: [string(name: 'WAR_FILES', value: "app_Pipeline/build/libs/Test.war"), string(name: 'TOMCAT_PATH', value: 'http://ip:port')]
}
}
stage ('deploy:tomcat') {
steps{
sshagent(['SSH_AGENT_cred']) {
sh 'scp /some-path/build/libs/Test.war admin#destinationIp:/apache-tomcat-8/webapps/'
}
}
}
1) Can we have a groovy syntax which can deploy war on remote server.(using only username and password , no private key related prerequisites ) ?
or
2) A jenkins freestyle job which can be generic enough to take war file path and tomcat details to deploy ?

Resources