Details
-
Type:
Bug
-
Status: Resolved (View Workflow)
-
Priority:
Major
-
Resolution: Not A Defect
-
Labels:None
-
Similar Issues:
Description
I am instantiating an object from within my vars (custom step (testCredentials)) shared library. I then execute a method (getRequiredPackages) on this instance that contains withCredentials step. A groovy.lang.MissingPropertyException is thrown on the withCredentials variable (see stack trace below)
vars/testCredentials.groovy
#!/usr/bin/env groovy import com.mycorp.deployment.classes.Source def call(body) { def config = [:] body.resolveStrategy = Closure.DELEGATE_FIRST body.delegate = config body() def sourceVersion = config.version timestamps { node() { try { stage('Test Credentials') { Source buildToolsSource = new Source(steps, sourceVersion) buildToolsSource.getRequiredPackages(steps) } } catch (e) { steps.sh "echo 'Error (${e}) occurred marking the build as failure.'" throw e } finally { steps.cleanWs() } } } }
Groovy class
/src/com/mycorp/deployment/classes/Source.groovy
#!/usr/bin/env groovy package com.mycorp.deployment.classes import org.jenkinsci.plugins.workflow.cps.DSL class Source implements Serializable { String version Source(DSL steps, version) { this.version = version } def getRequiredPackages(DSL steps) { steps.withCredentials([steps.string(credentialsId: 'git-oauth-token', variable: 'GIT_OAUTH_TOKEN')]) { steps.sh(returnStdout: false, script: "curl -L " + "-H 'Authorization: token ${GIT_OAUTH_TOKEN}' " + "https://api.github.com/repos/mycorp/myrepo/zipball/${this.version} " + "-o _packages/myrepo-${this.version}.zip " + "-vvv" ) } } }
I'm getting the following run-time exception:
groovy.lang.MissingPropertyException: No such property: GIT_OAUTH_TOKEN for class: com.magento.deployment.classes.Source
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:458)
at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.getProperty(DefaultInvoker.java:34)
at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
Trying to access the variable via env (${env.GIT_OAUTH_TOKEN}) produces the following error:
groovy.lang.MissingPropertyException: No such property: env for class: com.magento.deployment.classes.SourceNew
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:458)
at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.getProperty(DefaultInvoker.java:34)
at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
at com.magento.deployment.classes.SourceNew.getRequiredPackages
Workaround
When I execute the same method from an inline script (rather than a object instance) it works fine. The following modified files works:
vars/testCredentials.groovy
#!/usr/bin/env groovy import com.mycorp.deployment.classes.Source import com.mycorp.deployment.scripts.Utility def call(body) { def config = [:] body.resolveStrategy = Closure.DELEGATE_FIRST body.delegate = config body() def sourceVersion = config.version timestamps { node() { try { stage('Test Credentials') { Source buildToolsSource = new Source(steps, sourceVersion) //buildToolsSource.getRequiredPackages(steps) new Utility().getRequiredPackages(steps, buildToolsSource) } } catch (e) { steps.sh "echo 'Error (${e}) occurred marking the build as failure.'" throw e } finally { steps.cleanWs() } } } }
Inline script
/src/com/mycorp/deployment/scripts/Utility.groovy
#!/usr/bin/env groovy package com.mycorp.deployment.scripts import org.jenkinsci.plugins.workflow.cps.DSL import com.mycorp.deployment.classes.Source def getRequiredPackages(DSL steps, Source) { steps.withCredentials([steps.string(credentialsId: 'git-oauth-token', variable: 'GIT_OAUTH_TOKEN')]) { steps.sh(returnStdout: false, script: "curl -L " + "-H 'Authorization: token ${GIT_OAUTH_TOKEN}' " + "https://api.github.com/repos/mycorp/myrepo/zipball/${source.version} " + "-o _packages/myrepo-${source.version}.zip " + "-vvv" ) } } return this
I am attempting to create an abstract class with implementation details that other classes can extend. It is not ideal to have to implement this type of logic (withCredentials) in a separate inline scripts to workaround this issue.
Tested with version:
jenkins 2.60.3
credentials binding plugin 1.13
credentials plugin 2.1.16
Attachments
Activity
Field | Original Value | New Value |
---|---|---|
Component/s | pipeline [ 21692 ] |
Description |
I am instantiating an object from within my *vars* (custom step (testCredentials)) shared library. I then execute a method (*getRequiredPackages*) on this instance that contains withCredentials step. A groovy.lang.MissingPropertyException is thrown on the withCredentials variable (see stack trace below) *vars/testCredentials.groovy* {code:java} #!/usr/bin/env groovy import com.mycorp.deployment.classes.Source def call(body) { def config = [:] body.resolveStrategy = Closure.DELEGATE_FIRST body.delegate = config body() def sourceVersion = config.version timestamps { node() { try { stage('Test Credentials') { Source buildToolsSource = new Source(steps, sourceVersion) buildToolsSource.getRequiredPackages(steps) } } catch (e) { steps.sh "echo 'Error (${e}) occurred marking the build as failure.'" throw e } finally { steps.cleanWs() } } } } {code} Groovy class */src/com/mycorp/deployment/classes/Source.groovy* {code:java} #!/usr/bin/env groovy package com.mycorp.deployment.classes import org.jenkinsci.plugins.workflow.cps.DSL class Source { String version Source(DSL steps, version) { this.version = version } def getRequiredPackages(DSL steps) { steps.withCredentials([steps.string(credentialsId: 'git-oauth-token', variable: 'GIT_OAUTH_TOKEN')]) { steps.sh(returnStdout: false, script: "curl -L " + "-H 'Authorization: token ${GIT_OAUTH_TOKEN}' " + "https://api.github.com/repos/mycorp/myrepo/zipball/${this.version} " + "-o _packages/myrepo-${this.version}.zip " + "-vvv" ) } } }{code} I'm getting the following run-time exception: {quote}groovy.lang.MissingPropertyException: No such property: GIT_OAUTH_TOKEN for class: com.magento.deployment.classes.Source at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53) at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:458) at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.getProperty(DefaultInvoker.java:34) at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20) {quote} Trying to access the variable via *env* ($\{env.GIT_OAUTH_TOKEN}) produces the following error: {quote}groovy.lang.MissingPropertyException: No such property: env for class: com.magento.deployment.classes.SourceNew at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53) at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:458) at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.getProperty(DefaultInvoker.java:34) at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20) at com.magento.deployment.classes.SourceNew.getRequiredPackages {quote} h3. Workaround When I execute the same method from an inline script (rather than a object instance) it works fine. The following modified files works: *vars/testCredentials.groovy* {code:java} #!/usr/bin/env groovy import com.mycorp.deployment.classes.Source import com.mycorp.deployment.scripts.Utility def call(body) { def config = [:] body.resolveStrategy = Closure.DELEGATE_FIRST body.delegate = config body() def sourceVersion = config.version timestamps { node() { try { stage('Test Credentials') { Source buildToolsSource = new Source(steps, sourceVersion) //buildToolsSource.getRequiredPackages(steps) new Utility().getRequiredPackages(steps, buildToolsSource) } } catch (e) { steps.sh "echo 'Error (${e}) occurred marking the build as failure.'" throw e } finally { steps.cleanWs() } } } } {code} Inline script */src/com/mycorp/deployment/scripts/Utility.groovy* {code:java} #!/usr/bin/env groovy package com.mycorp.deployment.scripts import org.jenkinsci.plugins.workflow.cps.DSL import com.mycorp.deployment.classes.Source def getRequiredPackages(DSL steps, Source) { steps.withCredentials([steps.string(credentialsId: 'git-oauth-token', variable: 'GIT_OAUTH_TOKEN')]) { steps.sh(returnStdout: false, script: "curl -L " + "-H 'Authorization: token ${GIT_OAUTH_TOKEN}' " + "https://api.github.com/repos/mycorp/myrepo/zipball/${source.version} " + "-o _packages/myrepo-${source.version}.zip " + "-vvv" ) } } return this{code} I am attempting to create an abstract class with implementation details that other classes can extend. It is not ideal to have to implement this type of logic (withCredentials) in a separate inline scripts to workaround this issue. h3. Tested with version: jenkins 2.60.3 credentials binding plugin 1.13 credentials plugin 2.1.16 |
I am instantiating an object from within my *vars* (custom step (testCredentials)) shared library. I then execute a method (*getRequiredPackages*) on this instance that contains withCredentials step. A groovy.lang.MissingPropertyException is thrown on the withCredentials variable (see stack trace below) *vars/testCredentials.groovy* {code:java} #!/usr/bin/env groovy import com.mycorp.deployment.classes.Source def call(body) { def config = [:] body.resolveStrategy = Closure.DELEGATE_FIRST body.delegate = config body() def sourceVersion = config.version timestamps { node() { try { stage('Test Credentials') { Source buildToolsSource = new Source(steps, sourceVersion) buildToolsSource.getRequiredPackages(steps) } } catch (e) { steps.sh "echo 'Error (${e}) occurred marking the build as failure.'" throw e } finally { steps.cleanWs() } } } } {code} Groovy class */src/com/mycorp/deployment/classes/Source.groovy* {code:java} #!/usr/bin/env groovy package com.mycorp.deployment.classes import org.jenkinsci.plugins.workflow.cps.DSL class Source implements Serializable { String version Source(DSL steps, version) { this.version = version } def getRequiredPackages(DSL steps) { steps.withCredentials([steps.string(credentialsId: 'git-oauth-token', variable: 'GIT_OAUTH_TOKEN')]) { steps.sh(returnStdout: false, script: "curl -L " + "-H 'Authorization: token ${GIT_OAUTH_TOKEN}' " + "https://api.github.com/repos/mycorp/myrepo/zipball/${this.version} " + "-o _packages/myrepo-${this.version}.zip " + "-vvv" ) } } }{code} I'm getting the following run-time exception: {quote}groovy.lang.MissingPropertyException: No such property: GIT_OAUTH_TOKEN for class: com.magento.deployment.classes.Source at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53) at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:458) at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.getProperty(DefaultInvoker.java:34) at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20) {quote} Trying to access the variable via *env* ($\{env.GIT_OAUTH_TOKEN}) produces the following error: {quote}groovy.lang.MissingPropertyException: No such property: env for class: com.magento.deployment.classes.SourceNew at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53) at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:458) at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.getProperty(DefaultInvoker.java:34) at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20) at com.magento.deployment.classes.SourceNew.getRequiredPackages {quote} h3. Workaround When I execute the same method from an inline script (rather than a object instance) it works fine. The following modified files works: *vars/testCredentials.groovy* {code:java} #!/usr/bin/env groovy import com.mycorp.deployment.classes.Source import com.mycorp.deployment.scripts.Utility def call(body) { def config = [:] body.resolveStrategy = Closure.DELEGATE_FIRST body.delegate = config body() def sourceVersion = config.version timestamps { node() { try { stage('Test Credentials') { Source buildToolsSource = new Source(steps, sourceVersion) //buildToolsSource.getRequiredPackages(steps) new Utility().getRequiredPackages(steps, buildToolsSource) } } catch (e) { steps.sh "echo 'Error (${e}) occurred marking the build as failure.'" throw e } finally { steps.cleanWs() } } } } {code} Inline script */src/com/mycorp/deployment/scripts/Utility.groovy* {code:java} #!/usr/bin/env groovy package com.mycorp.deployment.scripts import org.jenkinsci.plugins.workflow.cps.DSL import com.mycorp.deployment.classes.Source def getRequiredPackages(DSL steps, Source) { steps.withCredentials([steps.string(credentialsId: 'git-oauth-token', variable: 'GIT_OAUTH_TOKEN')]) { steps.sh(returnStdout: false, script: "curl -L " + "-H 'Authorization: token ${GIT_OAUTH_TOKEN}' " + "https://api.github.com/repos/mycorp/myrepo/zipball/${source.version} " + "-o _packages/myrepo-${source.version}.zip " + "-vvv" ) } } return this{code} I am attempting to create an abstract class with implementation details that other classes can extend. It is not ideal to have to implement this type of logic (withCredentials) in a separate inline scripts to workaround this issue. h3. Tested with version: jenkins 2.60.3 credentials binding plugin 1.13 credentials plugin 2.1.16 |
Component/s | workflow-cps-global-lib-plugin [ 21714 ] | |
Component/s | pipeline [ 21692 ] |
Resolution | Not A Defect [ 7 ] | |
Status | Open [ 1 ] | Resolved [ 5 ] |
Comment |
[ Hi [~jglick]- Thanks for the feedback! I have tried *{{script.env.PROP}}* but get a *{{groovy.lang.MissingPropertyException: No such property: script for class}}* error. ] |