• job-dsl-plugin 1.78, job-dsl-plugin 1.80

      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.

       

       

          [JENKINS-44681] JobDSL Folder Creation Destroys Credentials

          Bruce Smith created issue -

          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
              }
            }
          }
          

          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 } } }
          Daniel Spilker made changes -
          Resolution New: Not A Defect [ 7 ]
          Status Original: Open [ 1 ] New: Resolved [ 5 ]
          Daniel Spilker made changes -
          Status Original: Resolved [ 5 ] New: Closed [ 6 ]

          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.

          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.

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

          Kind regards

          Jürgen

          Jürgen Bobinger added a comment - Thanks daspilker for your solution. That worked out quite well in my case. Kind regards Jürgen

          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!

          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!

          Omit Rathore added a comment -

          Please provision a way to ignore existing folder

          Omit Rathore added a comment - Please provision a way to ignore existing folder
          Omit Rathore made changes -
          Resolution Original: Not A Defect [ 7 ]
          Status Original: Closed [ 6 ] New: Reopened [ 4 ]
          Omit Rathore made changes -
          Comment [ This is a bug , folder meta data should remain as is ]

            jamietanna Jamie Tanna
            smithx10 Bruce Smith
            Votes:
            12 Vote for this issue
            Watchers:
            25 Start watching this issue

              Created:
              Updated:
              Resolved: