1

I have a Postman Collection with Global Variables that I am running via Jenkins (successfully), but I want to expose choices for the variables as Choice Parameters within Jenkins.

I asked an initial question here, as I'm building understanding: Exposing Postman Global variables as Jenkins Choice Parameters

...which set my fundamental understanding.

I'm just a little confused as to the format of the variables as passed from Jenkins to Newman CLI.

At present, I call my collection from within a shell script:

//paths to global & env vars up here...
run_collection() {
  collection=$1
  newman run ${collection} -e ${PM_ENV_VAR_PATH} --export-globals ${PM_GLOBAL_VAR_PATH} 
  -g ${PM_GLOBAL_VAR_PATH} --global-var "userCode={{userCode}}"

  if [ "$?" == 1 ]; then
    echo "should fail the build"
    exit 1
  fi

sleep 1
}

I have a global-vars.json in which I've tried to parameterize the userCode as follows:

{
  "type": "any",
  "value": "{{userCode}}",
  "key": "userCode"
}

Then in Jenkins my Choice Parameter is as follows:

enter image description here

This fails, probably for obvious reason - the userCode from the dropdown is not being passed. What is the syntax I need to use in the global-vars.json and passed to the CLI? When I use a hardcoded value such as:

--global-var "userCode=8978"

Then it successfully overrides any hardcoded value on global-vars.json

5
  • which plugin are you using ?
    – PDHide
    Apr 2 '20 at 13:05
  • DId you try $userCode instead of {{userCode}} in your groovy
    – PDHide
    Apr 2 '20 at 13:10
  • @PDHide in Jenkins, I am using Pipeline plugin and calling the script from there - I've added a snippet to the question
    – Steerpike
    Apr 2 '20 at 13:25
  • u don't have to parameterise it in global-vars, and for script try --global-vars=${params.UserCode} or --global-vars=${UserCode} or --global-vars=$UserCode , try any of these works
    – PDHide
    Apr 2 '20 at 13:35
  • Why are you using --export-globals ${PM_GLOBAL_VAR_PATH}, this won't use the global variable but stores the variable to the file specified. You have to use --globals ${PM_GLOBAL_VAR_PATH} if you want to use the global file
    – PDHide
    Apr 3 '20 at 9:09
1

Created a project

enter image description here

Called below command in windows batch step:

echo %user%

Output

enter image description here

So the variable is being stored as environmental variable , hence for shell script you can use

echo $UserCode

Your final shell command would be:

//paths to global & env vars up here...
run_collection() {
  collection=$1
  newman run ${collection} -e ${PM_ENV_VAR_PATH} --export-globals ${PM_GLOBAL_VAR_PATH} 
  -g ${PM_GLOBAL_VAR_PATH} --global-var "userCode=${userCode}"

  if [ "$?" == 1 ]; then
    echo "should fail the build"
    exit 1
  fi

sleep 1
}

Note: both $variable and ${variable} are same in shell script. ${variable} is used for parameterisation, you can read more at:

https://superuser.com/a/935427

so you can use both ${UserCode} and $UserCode

Also:

Why is --export-globals ${PM_GLOBAL_VAR_PATH} used, this won't use the global variable but stores the variable to the file specified. You have to use --globals ${PM_GLOBAL_VAR_PATH} if you want to use the global variable file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.