[JIRA] (JENKINS-56305) Need some help with init scripts on this plugin

0 views
Skip to first unread message

naresh.rayapati@gmail.com (JIRA)

unread,
Feb 27, 2019, 12:42:02 AM2/27/19
to jenkinsc...@googlegroups.com
Naresh Rayapati created an issue
 
Jenkins / Improvement JENKINS-56305
Need some help with init scripts on this plugin
Issue Type: Improvement Improvement
Assignee: Dominik Bartholdi
Components: config-file-provider-plugin
Created: 2019-02-27 05:41
Environment: Jenkins 2.165
Latest version of this plugin: 3.5
Priority: Minor Minor
Reporter: Naresh Rayapati

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

Add Comment Add Comment
 
This message was sent by Atlassian Jira (v7.11.2#711002-sha1:fdc329d)

naresh.rayapati@gmail.com (JIRA)

unread,
Feb 27, 2019, 12:44:02 AM2/27/19
to jenkinsc...@googlegroups.com
Naresh Rayapati updated an issue
Change By: Naresh Rayapati
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.

{code}

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

{code}

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 this to different files but no luck.

naresh.rayapati@gmail.com (JIRA)

unread,
Feb 27, 2019, 12:45:02 AM2/27/19
to jenkinsc...@googlegroups.com
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 this one of the sections to different files but no luck.

domi@fortysix.ch (JIRA)

unread,
Mar 1, 2019, 2:38:03 AM3/1/19
to jenkinsc...@googlegroups.com
Dominik Bartholdi resolved as Not A Defect
 

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:

 

Change By: Dominik Bartholdi
Status: Open Resolved
Resolution: Not A Defect

naresh.rayapati@gmail.com (JIRA)

unread,
Mar 1, 2019, 7:46:02 PM3/1/19
to jenkinsc...@googlegroups.com
Naresh Rayapati commented on Improvement JENKINS-56305
 
Re: Need some help with init scripts on this plugin

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. 

sisso_@live.com (JIRA)

unread,
Jun 11, 2019, 2:08:03 PM6/11/19
to jenkinsc...@googlegroups.com
sigur commented on Improvement JENKINS-56305

Is there a simple 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)
 }

 

sisso_@live.com (JIRA)

unread,
Jun 11, 2019, 2:10:13 PM6/11/19
to jenkinsc...@googlegroups.com
sigur edited a comment on Improvement JENKINS-56305
Is there a simple way to store the created configs in the jobs config folder?

I'm only able to store then in the global config folder 
{code :java }

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)
}
{code}
 

domi@fortysix.ch (JIRA)

unread,
Jun 12, 2019, 1:35:02 AM6/12/19
to jenkinsc...@googlegroups.com

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");

sisso_@live.com (JIRA)

unread,
Jun 12, 2019, 3:34:08 PM6/12/19
to jenkinsc...@googlegroups.com
sigur commented on Improvement JENKINS-56305

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

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

domi@fortysix.ch (JIRA)

unread,
Jun 14, 2019, 2:55:02 AM6/14/19
to jenkinsc...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages