I have a parameterized job with pipeline.
for example: Predefined String Parameter: IP
I'm trying to define a new String in the pipeline in order to use it as a new parameter when I'm calling to another "build job"
I have tried the following method:
import hudson.model.*
node('master'){
if(ipaddr =='192.168.1.1'){
def parameter = new StringParameterValue("subnet", '255.255.255.0') //not working
echo parameter //not working
}
stage ('Stage A'){
build job: 'jobA', parameters:
[
[$class: 'StringParameterValue', name: 'ip', value: ip],
[$class: 'StringParameterValue', name: 'subnet', value: subnet] //not working
]
}
}
this way it's not working and I get the error:
Scripts not permitted to use new hudson.model.StringParameterValue
after changing the line:
def parameter = new StringParameterValue("subnet", '255.255.255.0')
to:
subnet = '255.255.255.0'
I got the error:
groovy.lang.MissingPropertyException: No such property: subnetmask for
class: groovy.lang.Binding.
I can't call to a new job with the predefined parameter ip and the new parameter subnet
without the subnet it's working
any idea of how can I define new String parameter in the pipeline?
jenkins version: 2.19.4
You can have it working if you just avoid instantiating StringParameterValue because as David M. Karr mentionned pipelines sandbox is pretty restrictive. Instead, just use your simple variable when calling your job, like this :
def subnet = ""
if(ipaddr == '192.168.1.1') {
subnet = '255.255.255.0'
echo subnet
}
stage ('Stage A'){
build job: 'jobA', parameters:
[
[$class: 'StringParameterValue', name: 'ip', value: ipaddr],
[$class: 'StringParameterValue', name: 'subnet', value: subnet]
]
}
It's pretty simple, StringParameterValue params expect String to be passed, so as long as you pass string values you should be just fine !
By default, the sandbox the pipeline job executes in is very restricted. You have to override the security restrictions when you find them. Go to "Manage Jenkins" and "In-process Script Approval". You should see in the list the last violation that occurred. Select it for approval and rerun your script.
Related
I have a pipeline project say "A" which is string parameterized, and I am trying to call another build say "B" which is also string parameterized, using the command as follows :
build job: 'B', parameters: [[$class: 'StringParameterValue', name: 'tagg', value: "$env.tag"]]
The target is used to pass the parameter which was taken as input from A and assign use it in B.I tried to receive the parameter echo "$env.tagg" which gave null, echo "$tagg" gave no such parameter found error.
So how do I receive the parameters sent from A in B.
I think you need to let Jenkins know what env.tag is, try something like:
build job: 'B', parameters: [[$class: 'StringParameterValue', name: 'tagg', value: String.ValueOf($env.tag)]]
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 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)
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' ...)
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...