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

Need some help with init scripts on this plugin

    Details

    • Type: Improvement
    • Status: Resolved (View Workflow)
    • Priority: Minor
    • Resolution: Not A Defect
    • Labels:
      None
    • Environment:
      Jenkins 2.165
      Latest version of this plugin: 3.5
    • Similar Issues:

      Description

      I am trying to create a groovy init script to save multiple maven config files and custom config files within a single script, for some reasons it is honoring either one of them. Looks like I am missing something. Appreciate if someone can help me look into this file.

      import org.jenkinsci.plugins.configfiles.maven.MavenSettingsConfig
      import org.jenkinsci.plugins.configfiles.custom.CustomConfig
      import org.jenkinsci.plugins.configfiles.maven.security.ServerCredentialMapping
      
      // Maven config files.
      def mavenProvider = instance.getExtensionList('org.jenkinsci.plugins.configfiles.maven.MavenSettingsConfig$MavenSettingsConfigProvider')[0]
      config?.maven?.each { key, config ->
          println("Adding maven settings with key: ${key}")
          def serverCreds = new ArrayList()
      
          config.servers.each { server ->
              def serverCredentialMapping = new ServerCredentialMapping(server.serverId, server.credentialsId)
              serverCreds.add(serverCredentialMapping)
          }
      
          def mavenConfig = new MavenSettingsConfig(config.id, config.name, config.comment, config.content, Boolean.parseBoolean(config.replaceAll), serverCreds)
          mavenProvider.save(mavenConfig)
      }
      
      // Custom config files.
      def customProvider = instance.getExtensionList('org.jenkinsci.plugins.configfiles.custom.CustomConfig$CustomConfigProvider')[0]
      config?.custom?.each { key, config ->
          println("Adding custom file with key: ${key}")
          def customConfig = new CustomConfig(config.id, config.name, config.comment, config.content)
          customProvider.save(customConfig)
      }
      
      

      I got to remove one of these two like either custom config files section or the maven config files section to get this work, I have also tried moving one of the sections to different files but no luck.

        Attachments

          Activity

          Hide
          imod Dominik Bartholdi added a comment -

          not sure this works, but your probably better off to use `org.jenkinsci.plugins.configfiles.GlobalConfigFiles.save(config)` then any of the providers directly.

          But that sad, you should better use a plugin designed to do this:

           

          Show
          imod Dominik Bartholdi added a comment - not sure this works, but your probably better off to use ` org.jenkinsci.plugins.configfiles.GlobalConfigFiles.save(config) ` then any of the providers directly. But that sad, you should better use a plugin designed to do this:   JobDSL Plugin:  https://github.com/jenkinsci/job-dsl-plugin/wiki/Job-DSL-Commands#config-file   Configuration as Code Plugin: https://github.com/jenkinsci/configuration-as-code-plugin/tree/master/demos/config-file-provider  
          Hide
          nrayapati Naresh Rayapati added a comment -

          Thank you Dominik Bartholdi, we have created an ecosystem internally such that there is dependency on these startup scripts.

          It was a small mistake caught me into this problem, I was using duplicate ids in the config, which is replacing the other config. Thank you again. 

          Show
          nrayapati Naresh Rayapati added a comment - Thank you Dominik Bartholdi , we have created an ecosystem internally such that there is dependency on these startup scripts. It was a small mistake caught me into this problem, I was using duplicate ids in the config, which is replacing the other config. Thank you again. 
          Hide
          sigur sigur added a comment - - edited

          Is there a way to store the created configs in the jobs config folder?
          I'm only able to store then in the global config folder 

          configFileProvider([configFile(fileId: 'DEFAULT_JSON_CONFIG', variable: 'default_config_content')]) {
                  def instance = Jenkins.getInstance()
                  def provider = instance.getExtensionList('org.jenkinsci.plugins.configfiles.json.JsonConfig$JsonConfigProvider')[0]
                  def config = new org.jenkinsci.plugins.configfiles.json.JsonConfig("${BRANCH_NAME}_config", "Config for ${BRANCH_NAME}", "Branch config ${BRANCH_NAME}", "$default_config_content")
                  provider.save(config)
           }
          

           

          Show
          sigur sigur added a comment - - edited Is there a way to store the created configs in the jobs config folder? I'm only able to store then in the global config folder  configFileProvider([configFile(fileId: 'DEFAULT_JSON_CONFIG' , variable: 'default_config_content' )]) {   def instance = Jenkins.getInstance()     def provider = instance.getExtensionList( 'org.jenkinsci.plugins.configfiles.json.JsonConfig$JsonConfigProvider' )[0]     def config = new org.jenkinsci.plugins.configfiles.json.JsonConfig( "${BRANCH_NAME}_config" , "Config for ${BRANCH_NAME}" , "Branch config ${BRANCH_NAME}" , "$default_config_content" )     provider.save(config) }  
          Hide
          imod Dominik Bartholdi added a comment -

          as described above, you should not do it this way and I can't guarantee this will always work! 

          But for sure this words will not stop you from doing so, therefore I show you at least a better way to do it: (Please never interact with the *Provider classes at all):

           

          def folder = Jenkins.instance.getItemByFullName('ios');
          def action = folder.getAction(org.jenkinsci.plugins.configfiles.folder.FolderConfigFileAction.class);
          def store = action.getStore();
          def config = new org.jenkinsci.plugins.configfiles.json.JsonConfig("XXXX_config", "Config for XXXX", "Branch config XXXX", "{A:B}");
          // save the new config
          store.save(config);
          // get the new config
          def jsonConfig = store.getById("XXXX_config");
          // remove the config
          store.remove("XXXX_config");
          Show
          imod Dominik Bartholdi added a comment - as described above, you should not do it this way and I can't guarantee this will always work!  But for sure this words will not stop you from doing so, therefore I show you at least a better way to do it: (Please never interact with the *Provider classes at all):   def folder = Jenkins.instance.getItemByFullName( 'ios' ); def action = folder.getAction(org.jenkinsci.plugins.configfiles.folder.FolderConfigFileAction.class); def store = action.getStore(); def config = new org.jenkinsci.plugins.configfiles.json.JsonConfig( "XXXX_config" , "Config for XXXX" , "Branch config XXXX" , "{A:B}" ); // save the new config store.save(config); // get the new config def jsonConfig = store.getById( "XXXX_config" ); // remove the config store.remove( "XXXX_config" );
          Hide
          sigur sigur added a comment -

          Thank you, I promise this will only be used for good.

          note for future users: 'ios' is the name of the job

          Show
          sigur sigur added a comment - Thank you, I promise this will only be used for good. note for future users: 'ios' is the name of the job
          Hide
          imod Dominik Bartholdi added a comment -

          ios is the name of the folder

          Show
          imod Dominik Bartholdi added a comment - ios is the name of the folder

            People

            • Assignee:
              domi Dominik Bartholdi
              Reporter:
              nrayapati Naresh Rayapati
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: