4

I have a Jenkinsfile in Groovy for a declarative pipeline and two created Jenkins variables with names OCP_TOKEN_VALUE_ONE and OCP_TOKEN_VALUE_TWO and the corresponding values. The problem comes when I try to pass a method variable and use it in an sh command.

I have the next code:

private def deployToOpenShift(projectProps, environment, openshiftNamespaceGroupToken) {  
  sh """/opt/ose/oc login ${OCP_URL} --token=${openshiftNamespaceGroupToken} --namespace=${projectProps.namespace}-${environment}"""
}

The problem is, the method deployToOpenShift has in the openshiftNamespaceGroupToken variable, a value that is the name of variable that has been set in Jenkins. It needs to be dynamic and the problem is that Jenkins don't resolve the Jenkins variable value, just the one passed as String, I mean, the result is:

--token=OCP_TOKEN_VALUE_ONE

If I put in the code

private def deployToOpenShift(projectProps, environment, openshiftNamespaceGroupToken) {  
  sh """/opt/ose/oc login ${OCP_URL} --token=${OCP_TOKEN_VALUE_ONE} --namespace=${projectProps.namespace}-${environment}"""
}

works perfect but is not dynamic that is the point of the method variable. I have tried with the """ stuff as you can see, but not working.

Any extra idea?

Edited with the code that calls the method:

...
projectProps = readProperties file: './gradle.properties'
openShiftTokenByGroup = 'OCP_TOKEN_' + projectProps.namespace.toUpperCase()

...

stage ('Deploy-Dev') {
  agent any
  steps {
    milestone ordinal : 10, label: "Deploy-Dev Milestone"
    deployToOpenShift(projectProps, 'dev', openShiftTokenByGroup)
  }
}
6
  • Can you show the code that calls the method?
    – Rob Hales
    Commented Nov 14, 2017 at 18:29
  • Edited with the code @RobHales
    – dcalap
    Commented Nov 15, 2017 at 8:11
  • Your code is wrong because it define a simple string OCP_TOKEN_VALUE_ONE which is the name of the variable but not the value of it!
    – yorammi
    Commented Nov 15, 2017 at 8:58
  • @yorammi but as I told, this string is the name of a Jenkins variable which is correctly evaluated in the openshift plugin in another stage: openshiftBuild apiURL: "${OCP_URL}", authToken: "${openShiftTokenByGroup}", namespace: "${projectProps.namespace}-dev" I think the problem comes with the sh command stuff
    – dcalap
    Commented Nov 15, 2017 at 9:45
  • OK. Did you try to put ' instead of """ to wrap the sh command? For my experience it behave differently in some cases.
    – yorammi
    Commented Nov 15, 2017 at 10:21

1 Answer 1

4

I have got two different ways to do that. One is using evaluate from groovy like this:

def openShiftTokenByGroup = 'OCP_TOKEN_' + projectProps.namespace.toUpperCase()

evaluate("${openShiftTokenByGroup}") //This will resolve the configured value in Jenkins

The second one is the same approach but in the sh command with eval escaping the $ character:

sh """ 
eval \$$openShiftTokenByGroup
echo "Token: $openShiftTokenByGroup
 """

This will do the magic too and you'll get the Jenkins configured value.

3
  • Can it evaluate from a var that reads from json? config = readJSON(file: 'config.json') config.RHEL6_SOURCE_IMAGE_FAMILY
    – Xao
    Commented Apr 4, 2020 at 20:59
  • Hi @dcalap...Can you please help me out with my query? stackoverflow.com/questions/63467568/… Commented Aug 19, 2020 at 8:19
  • @dcalap I've tried your solution, but it didn't work because it's looking for the variables value that is mentioned through another variable, on a new section of the pipelin called script 1
    – Ori Wiesel
    Commented May 20, 2021 at 15:23

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