Jenkins Pipeline: Param in batch call - jenkins

I have a pipeline build working in Jenkins, and have made the project parameterized to pass some information into the pipeline script. Everything works well except for inserting a "String Parameter" into a call to a batch file.
string(name: 'SANDBOX_ID', defaultValue: 'Build', description: 'Build Identifier (BuildNNN)')
I can see the parameter added, and can populate it via "Build with Parameters". I tried using it like so:
steps {
bat 'FtpPublisher.exe -srcDir "%WORKSPACE%/Builds/WebGL/Build" -targetDir "/Builds/${params.SANDBOX_ID}"'
}
This always results in a new folder created on my FTP server with the name "${params.SANDBOX_ID}" instead of the actual SANDBOX_ID parameter.

Found answer here after some more searching: pass parameter from jenkins parameterized build to windows batch command
Fixed by using %SANDBOX_ID% instead of ${params.SANDBOX_ID}

Related

Need option to fill values in Jenkins parameterized pipeline script stored in Github whenever it run

I have created a jenkins parameterized pipeline script as below. I have stored it on my Github repository.
properties([parameters([string(defaultValue: 'Devasish', description: 'Enter your name', name: 'Name'),
choice(choices: ['QA', 'Dev', 'UAT', 'PROD'], description: 'Where you want to deploy?', name: 'Environnment')])])
pipeline {
agent any
stages {
stage('one') {
steps {
echo "Hello ${Name} Your code is Building in ${Environnment} "
}
}
stage('Two') {
steps {
echo "Hello ${Name} hard testing in ${Environnment}"
}
}
stage('Three') {
steps {
echo "Hello1 ${Name} deploying in ${Environnment}"
}
}
}
}
Then, I have created a jenkins job by choosing pipeline option. While creating jenkins pipeline Under build triggers section, I have checked GitHub hook trigger for GITScm polling checkbox and Under Pipeline section, I have chosen Pipeline script from SCM followed by choosing Git in SCM, providing Repository URL where the above written JenkinsFile script is stored.
Then, Under Github repository settings, I have gone to webhooks and added one webhook where I specified my Payload URL as myJenkinsServerURL/github-webhook/. which will enable a functionality like whenever there will be any push event occurred within the repository, it will run the jenkins pipeline I created above.
Now, the situation is, when I am running this jenkins job from Classic UI by clicking Build with parameters, I am getting a text box to fill my name and a dropdown having list of 4 options ('QA', 'Dev', 'UAT', 'PROD') I gave above in script to choose, in which server I want to deploy my code, then it gets run.
But when I am committing in Github, it starts jenkins pipeline but not asking for parameters value instead just taking default value Devasish in name and QA in server.
What should I do to get an option of filling these details but not from Classic UI.
Thanks in advance.
As you have noted, when you trigger your pipeline manually, it will ask for the Build Parameters and let you specify values before proceeding.
However, when triggering the pipeline thru automatic triggers (e.g. SCM triggers/webhooks), then it is assumed to be an unattended build and it will use the defaultValue settings from your Jenkinsfile build parameters" definition.

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

Load Jenkins build parameters from SCM

Is it possible to have Jenkins pull a prameterized build from SCM (Git)?
I'm currently using "Pipeline script from SCM" where Jenkins retrieves the pipeline script but not the build parameters e.g. "String Parameter", "Choice Parameter", etc.
Jenkinsfile (Jenkins pipelines) are capable of this.
https://jenkins.io/doc/book/pipeline/syntax/#parameters
Be aware that parameters are post processed. So the 1st build will just be "Build" not "Build with parameters". After the 1st build, it will change.
This can be alleviated by using default values and always referencing the params using the full params.PARAM_NAME syntax. Don't just reference it as PARAM_NAME as this will cause Jenkins to search for env.PARAM_NAME by default.
Yes, you can retrieve parameters from a Parametrized Build in a Pipeline script from SCM Jenkins job.
To access them in the Jenkins file, use env.[PARAMETER_NAME].
For example:
echo 'Param value: ' + env.SOME_PARAMETER

trigger parameterized build on other projects does not pass parameter

In a Jenkins pipeline, parameters defined in upstream job is not being passed to downstream job even "current build parameters" option is selected. When I try to echo the parameter value, it is null.
Example: I have checked "This is a parameterized build". There is a string parameter in upstream job called "version" with the default value as "abc". When I run echo $version on the downstream job, in console output I do not see a value being printed.
Environment info:
Jenkins Version: 2.89.3
O/S: CentOs 7
Jenkins installed plugin info:
Build with Parameters: 1.4
Parameterized Trigger plugin:2.35.2
In the downstream job you need to set a variable to accept the parameter being passed.
Example: If foo parameter was passed from upstream job. The downstream should have a parameter (string or one of your choice) same name as foo and default value as $foo.
Finally I found out that the way this parameter works in jenkins 1.x and 2.x versions is different. In the newer version of jenkins, all the down stream projects must be parameterized with the defined parameter and the empty value. However, in Jenkins 1.x was able to pass the parameter from upstream to downstream without defining the parameter in the upstream project. Using only "Current build parameter" was able to do the work but not anymore. Not sure If I am the only one finding it bizarre.

How can I pass “build-step parameters” to a downstream project (freestyle project with parameterized triggers)?

I have a Jenkins setup with two freestyle-projects linked via Jenkins upstream/downstream post-build triggers (Jenkins Parameterized Trigger plugin). The upstream project builds something that is uploaded somewhere. And the downstream project picks that new thing up and tests it.
At the moment the downstream project finds the build by date by looking for “the latest”. I want to change that.
I would like to change upstream to generate a random string as filename in the build step and pass that to downstream as parameter. How can I do that?
(I know how to generate a random string – the point is: how to pass build-step information down to the “downstream job”?)
In your upstream pipeline script, add this:
build job: 'your-downstream-job', parameters: [[$class: 'StringParameterValue', name: 'YOUR_STRING_PARAM', value: "${yourRandomGeneratedStringVariable}" ]]
And your your-downstream-job should be e.g. a normal job which is parameterized (check the tick at This project is parameterized), with the StringParameter YOUR_STRING_PARAM.
Then just read the value of this environment variable and you can go on.

Resources