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

Script runs in Script Console but not with Job DSL groovy

    XMLWordPrintable

    Details

    • Similar Issues:

      Description

      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.

        Attachments

          Issue Links

            Activity

            Hide
            daspilker Daniel Spilker added a comment -

            Duplicates JENKINS-27517.

            Show
            daspilker Daniel Spilker added a comment - Duplicates JENKINS-27517 .
            Hide
            daspilker 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()
            
            Show
            daspilker 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()
            Hide
            sag47 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.

            Show
            sag47 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.
            Hide
            sag47 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!

            Show
            sag47 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!
            Hide
            daspilker 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.

            Show
            daspilker 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 .

              People

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

                Dates

                Created:
                Updated:
                Resolved: