Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-28701

Script runs in Script Console but not with Job DSL groovy

      The following script runs fine in the Script Console but not in a Job DSL script.

      import jenkins.*
      import jenkins.model.*
      import hudson.*
      import hudson.model.*
      
      def credentials = Jenkins.instance.getExtensionList(com.cloudbees.jenkins.GitHubPushTrigger.DescriptorImpl.class)[0].getCredentials()
      
      if(credentials.size() > 0) {
        credentials.each {
          if(it.apiUrl.length() > 0) {
            println("apiUrl: " + it.apiUrl)
          }
          else {
            println("apiUrl: https://api.github.com/")
          }
          println("username: " + it.username)
          println("oauthAccessToken: " + it.oauthAccessToken)
        }
      }
      

      I recall that the Job DSL plugin executes groovy scripts in the Jenkins runtime. I'm getting info out of the configured github plugin (specifically the automatically manage hooks section).

      Can you see anything that I'm obviously doing wrong? Also, there's this text from the script console: "All the classes from all the plugins are visible." The same does not appear to apply in Job DSL scripts.

      Any advice is appreciated.

          [JENKINS-28701] Script runs in Script Console but not with Job DSL groovy

          Duplicates JENKINS-27517.

          Daniel Spilker added a comment - Duplicates JENKINS-27517 .

          The Job DSL plugin can't access classes from other plugins.

          But Groovy and the Jenkins API allow to avoid that by using reflection and dynamic access. In your case, use Jenkins.getExtensionList(String), e.g.

          def credentials = Jenkins.instance.getExtensionList("com.cloudbees.jenkins.GitHubPushTrigger.DescriptorImpl")[0].getCredentials()
          

          Daniel Spilker added a comment - The Job DSL plugin can't access classes from other plugins. But Groovy and the Jenkins API allow to avoid that by using reflection and dynamic access. In your case, use Jenkins.getExtensionList(String), e.g. def credentials = Jenkins.instance.getExtensionList( "com.cloudbees.jenkins.GitHubPushTrigger.DescriptorImpl" )[0].getCredentials()

          Sam Gleske added a comment -

          Do you have a donation button? This is the second time you've given me great advice and I'd like to buy you a coffee.

          Sam Gleske added a comment - Do you have a donation button? This is the second time you've given me great advice and I'd like to buy you a coffee.

          Sam Gleske added a comment - - edited

          Just following up. The solution you gave wasn't entirely correct but it was a great hint for what I needed to do. Here's the working string.

          Jenkins.instance.getExtensionList('com.cloudbees.jenkins.GitHubPushTrigger$DescriptorImpl')[0].getCredentials()
          

          Thanks again!

          Sam Gleske added a comment - - edited Just following up. The solution you gave wasn't entirely correct but it was a great hint for what I needed to do. Here's the working string. Jenkins.instance.getExtensionList( 'com.cloudbees.jenkins.GitHubPushTrigger$DescriptorImpl' )[0].getCredentials() Thanks again!

          Always glad to help. You can donate for the Jenkins project (https://wiki.jenkins-ci.org/display/JENKINS/Donation) and there is a donation button on my page.

          Daniel Spilker added a comment - Always glad to help. You can donate for the Jenkins project ( https://wiki.jenkins-ci.org/display/JENKINS/Donation ) and there is a donation button on my page .

            daspilker Daniel Spilker
            sag47 Sam Gleske
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: