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

JobDSL Folder Creation Destroys Credentials

    XMLWordPrintable

    Details

    • Similar Issues:

      Description

      When using job-dsl-plugin to create a folder 

      folder(my_folder) { 

        displayname("my_folder_name")

      }

      If the folder exists with Credentials attached to it, they are lost on recreation  . Is there anyway to wrap this in a conditional? if!(folder(my_folder) {blah..blah....}.  I believe the desired behaviour here is to not blow away the credentials attached at the folder level.

       

       

        Attachments

          Activity

          Hide
          daspilker Daniel Spilker added a comment -

          The credentials on folder level are stored in the folder configuration. When a folder is generated by Job DSL, the whole configuration is replaced by the configuration described in Job DSL. So the credentials get lost, when running the seed job.

          I do not recommend to specify credentials in DSL scripts. But you can use the Jenkins API to copy the existing credentials configuration to the Job DSL definition:

          import jenkins.model.Jenkins
          import hudson.model.Item
          import hudson.model.Items
          
          String FOLDER_CREDENTIALS_PROPERTY_NAME = 'com.cloudbees.hudson.plugins.folder.properties.FolderCredentialsProvider$FolderCredentialsProperty'
          
          Node folderCredentialsPropertyNode
          Item myFolder = Jenkins.instance.getItem('my-folder')
          if (myFolder) {
            def folderCredentialsProperty = myFolder.getProperties().getDynamic(FOLDER_CREDENTIALS_PROPERTY_NAME)
            if (folderCredentialsProperty) {
              String xml = Items.XSTREAM2.toXML(folderCredentialsProperty)
              folderCredentialsPropertyNode = new XmlParser().parseText(xml)
            }
          }
          
          folder('my-folder') {
            if (folderCredentialsPropertyNode) {
              configure { project ->
                project / 'properties' << folderCredentialsPropertyNode
              }
            }
          }
          
          Show
          daspilker Daniel Spilker added a comment - The credentials on folder level are stored in the folder configuration. When a folder is generated by Job DSL, the whole configuration is replaced by the configuration described in Job DSL. So the credentials get lost, when running the seed job. I do not recommend to specify credentials in DSL scripts. But you can use the Jenkins API to copy the existing credentials configuration to the Job DSL definition: import jenkins.model.Jenkins import hudson.model.Item import hudson.model.Items String FOLDER_CREDENTIALS_PROPERTY_NAME = 'com.cloudbees.hudson.plugins.folder.properties.FolderCredentialsProvider$FolderCredentialsProperty' Node folderCredentialsPropertyNode Item myFolder = Jenkins.instance.getItem( 'my-folder' ) if (myFolder) { def folderCredentialsProperty = myFolder.getProperties().getDynamic(FOLDER_CREDENTIALS_PROPERTY_NAME) if (folderCredentialsProperty) { String xml = Items.XSTREAM2.toXML(folderCredentialsProperty) folderCredentialsPropertyNode = new XmlParser().parseText(xml) } } folder( 'my-folder' ) { if (folderCredentialsPropertyNode) { configure { project -> project / 'properties' << folderCredentialsPropertyNode } } }
          Hide
          marcus_phi Marcus Philip added a comment -

          I think it's incorrect to close this as not a defect. It's not how JobDSL works in general in other cases. Normally it's idempotent. Also the jobDSL folder docs in api viewer says: "Creates or updates a folder".

          Please reconsider.

          Show
          marcus_phi Marcus Philip added a comment - I think it's incorrect to close this as not a defect . It's not how JobDSL works in general in other cases. Normally it's idempotent. Also the jobDSL folder docs in api viewer says: "Creates or updates a folder". Please reconsider.
          Hide
          jbob_jenkins Jürgen Bobinger added a comment -

          Thanks Daniel Spilker for your solution. That worked out quite well in my case.

          Kind regards

          Jürgen

          Show
          jbob_jenkins Jürgen Bobinger added a comment - Thanks Daniel Spilker for your solution. That worked out quite well in my case. Kind regards Jürgen
          Hide
          jedi_lewis Jedi Lewis added a comment -

          We're running into this bug as well, can this be re-opened please? Running "folder()" on an existing folder shouldn't wipe out all metadata attached to the folder. This includes views and folder scoped credentials. 

          Thanks!

          Show
          jedi_lewis Jedi Lewis added a comment - We're running into this bug as well, can this be re-opened please? Running "folder()" on an existing folder shouldn't wipe out all metadata attached to the folder. This includes views and folder scoped credentials.  Thanks!
          Hide
          orathore Omit Rathore added a comment -

          Please provision a way to ignore existing folder

          Show
          orathore Omit Rathore added a comment - Please provision a way to ignore existing folder

            People

            • Assignee:
              daspilker Daniel Spilker
              Reporter:
              smithx10 Bruce Smith
            • Votes:
              0 Vote for this issue
              Watchers:
              9 Start watching this issue

              Dates

              • Created:
                Updated: