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

Getting MissingPropertyException when running Groovy script in freestyle job

XMLWordPrintable

      I am trying to execute this script as a step in a freestyle job.

      Script was tested in the script console and it ran successfully. But it gets MissingProperty exception during the run. 

      The script is read from a file in workspace.

      The script:

      import hudson.util.Secretimport 
      import com.cloudbees.plugins.credentials.CredentialsScope
      import com.cloudbees.plugins.credentials.domains.Domain
      import org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl
      import com.cloudbees.hudson.plugins.folder.*
      import com.cloudbees.hudson.plugins.folder.properties.FolderCredentialsProvider.FolderCredentialsProperty
      
      credentials = [
          [
              'credential_type': 'StringCredentialsImpl',
              'credentials_id': 'SECRET_ID',
              'description': 'Super secret text',
              'secret': 'secretsecret',
              'folder': 'folder',
              'folder_cred_domain': 'folder_cred_domain'
          ]
      ]
      
      def addCredential(String credentials_id, def credential, String folder_name, String folder_cred_domain) {
          def j = Jenkins.instance
          boolean modified_creds = false
          Domain domain
          for (folder in j.getAllItems(Folder.class)) {
              if (folder.name.equals(folder_name)) {
                  AbstractFolder<?> folderAbs = AbstractFolder.class.cast(folder)
                  Map folder_creds_map = folderAbs.getProperties().get(FolderCredentialsProperty.class).getDomainCredentialsMap()
                	store = folderAbs.getProperties().get(FolderCredentialsProperty.class).getStore()
                	domain = store.getDomains().find { it.getName() == folder_cred_domain }
                  if (!folder_creds_map[domain] || (folder_creds_map[domain].findAll {credentials_id.equals(it.id)}).size() < 1) {
                      if (folder_creds_map[domain] && folder_creds_map[domain].size() > 0) {
                          // Other credentials exist so should only append
                          folder_creds_map[domain] << credential
                      } else {
                          folder_creds_map[domain] = [credential]
                      }
                    	modified_creds = true
                  }
                  // Save any modified credentials
                  if (modified_creds) {
                      println "--> ${credentials_id} credentials added to folder ${folder_name}."
                  } else {
                      println "--> Nothing changed.  ${credentials_id} credentials already configured."
                  }
              }
          }
      }
      
      
      /**
        Supports String credential provided by StringCredentialsImpl class.
        Example:
          [
              'credential_type': 'StringCredentialsImpl',
              'credentials_id': 'some-credential-id',
              'description': 'A description of this credential',
              'secret': 'super secret text'
          ]
        */
      def setStringCredentialsImpl(Map settings) {
          String credentials_id = ((settings['credentials_id'])?:'').toString()
          String description = ((settings['description'])?:'').toString()
          String secret = ((settings['secret'])?:'').toString()
          String folder_name = ((settings['folder'])?:'').toString()
          String folder_cred_domain = ((settings['folder_cred_domain'])?:'').toString()
          addCredential(
                  credentials_id,
                  new StringCredentialsImpl(
                      CredentialsScope.GLOBAL,
                      credentials_id,
                      description,
                      Secret.fromString(secret)
                  ),
                  folder_name,
                  folder_cred_domain
          )
      }
      
      if(!binding.hasVariable('credentials')) {
          credentials = []
      }
      
      
      if(!(credentials instanceof List<Map>)) {
          throw new Exception('Error: credentials must be a list of maps.')
      }
      
      
      //iterate through credentials and add them to Jenkins
      credentials.each {
          if("set${(it['credential_type'])?:'empty credential_type'}".toString() in this.metaClass.methods*.name.toSet()) {
              "set${it['credential_type']}"(it)
          }
          else {
              println "WARNING: Unknown credential type: ${(it['credential_type'])?:'empty credential_type'}.  Nothing changed."
          }
      }
      
      

      I am using Groovy plugin version 2.0 and Jenkins LTS 2.121.3. Stack trace attached. 

       

            vjuranek vjuranek
            ottramst Ott Ramst
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: