0

I need to segregate Target Environment based on JobName... Any idea what I may be doing wrong:

Jenkinsfile

#!groovy
import groovy.json.JsonSlurperClassic
node {
    def BUILD_NUMBER=env.BUILD_NUMBER
    def RUN_ARTIFACT_DIR="tests/${BUILD_NUMBER}"
    def PACK_DIR="MyPackage${BUILD_NUMBER}"
    def SFDC_USERNAME
    def workspace = env.WORKSPACE
    def MY_JOB_NAME = env.JOB_NAME
    def SFDC_HOST = env.SFDC_HOST_DH_TEST
    println MY_JOB_NAME

    if (MY_JOB_NAME.contains("BV_SFDX_CI_PreINT_Validate")) {

        print "For PreInt"
        def HUB_ORG=env.HUB_ORG_DH_PI
        def JWT_KEY_CRED_ID = env.JWT_CRED_ID_DH_PI
        def CONNECTED_APP_CONSUMER_KEY=env.CONNECTED_APP_CONSUMER_KEY_DH_PI
        println JWT_KEY_CRED_ID

    }

    else {

        print "For Int"
        def HUB_ORG=env.HUB_ORG_DH_IN
        def JWT_KEY_CRED_ID = env.JWT_CRED_ID_DH_IN
        def CONNECTED_APP_CONSUMER_KEY=env.CONNECTED_APP_CONSUMER_KEY_DH_IN

    }

    println 'KEY IS' 
    println JWT_KEY_CRED_ID
    println HUB_ORG
    println SFDC_HOST
    println CONNECTED_APP_CONSUMER_KEY
}

Error:

[Pipeline] echo
KEY IS
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
groovy.lang.MissingPropertyException: No such property: JWT_KEY_CRED_ID for class: groovy.lang.Binding
    at groovy.lang.Binding.getVariable(Binding.java:63)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:270)
    at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:289)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:293)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:269)
    at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:29)
    at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
    at WorkflowScript.run(WorkflowScript:33)
    at ___cps.transform___(Native Method)
    at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:74)
    at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
    at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:66)
    at sun.reflect.GeneratedMethodAccessor700.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
    at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
    at com.cloudbees.groovy.cps.Next.step(Next.java:83)
    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
    at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
    at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)
    at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:174)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:370)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:93)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:282)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:270)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:131)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
    at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:59)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Finished: FAILURE

Any help is appreciated as I am stuck for a long time over here.

0

Moving the declaration def JWT_KEY_CRED_ID outside the blocks would eliminate the error e.g.:

def JWT_KEY_CRED_ID
if (MY_JOB_NAME.contains("BV_SFDX_CI_PreINT_Validate")) {
    JWT_KEY_CRED_ID = env.JWT_CRED_ID_DH_PI
    ...
}
else {
    JWT_KEY_CRED_ID = env.JWT_CRED_ID_DH_IN
    ...
}
println JWT_KEY_CRED_ID
  • Hello Keith,Thanks for your response... Yes I agree, but I am trying to setup different parameters based on the Job Name, but the param scope is getting limited to within the if else loop. :( – Varun SFDC Jul 14 at 18:34
  • Hi @VarunSFDC, Not sure what you mean by "param scope"; can you add more detail to the question? Having all the def outside of the blocks and assigning one or the other value in the blocks looks appropriate here. – Keith C Jul 14 at 18:35
  • Hi Keith, I am trying to create a same Jenkinsfile to deploy develop branch on multiple Environments, Hence I am following such an approach where depending on the JobName, it will pick the Environment Configuration to deploy the Source Code to. Hence I have to declare it within the if-else – Varun SFDC Jul 14 at 18:39
  • Hi Keith, I will try it and update you. – Varun SFDC Jul 14 at 18:49
  • Hi Keith, That worked great... Will accept it as the correct ans. Many thanks – Varun SFDC Jul 14 at 18:57

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.