Jenkins API to update library configuration

Viewed 312

I am trying to update a library in "Configuration" we use in Jenkins through Python, but do not see any way to do this through Jenkins API. Is there a way to update the version to 1.1.0 via Jenkins API?

enter image description here

2 Answers

Assuming you are ok with using a groovy script to configure the library version, you can use the following script to set the default version of an Global existing library:

def jenkins = Jenkins.getInstance()
def globalLibraryDescriptor = jenkins.getDescriptor("org.jenkinsci.plugins.workflow.libs.GlobalLibraries")
def config = globalLibraryDescriptor.getLibraries().find {
   it.name == '1.1.0' // Find your existing library by name
}
config.setDefaultVersion("THE_NEW_VERION")  // Update the default version value

If you also want to create library configurations in the global settings via API you can use the following script as a usage example for the groovy API.

If you want to configure a Folder level Existing library you can use the following code:

def jenkins = Jenkins.getInstance()
def folderLibrary = jenkins.getItem("FOLDER_NAME").getProperties().find {it.class =~ 'FolderLibraries'}
def config = folderLibrary.getLibraries().find {
   it.name == '1.1.0' // Find your existing library by name
}
config.setDefaultVersion("THE_NEW_VERION")  // Update the default version value

Update: We got it to work with Shared Library using the following code (if anyone runs into needing this to work):

for (LibraryResolver resolver : ExtensionList.lookup(LibraryResolver.class)) {
    for (LibraryConfiguration cfg : resolver.forJob(currentBuild.rawBuild.getParent(), [:])) {
        if (cfg.getName == libraryName){
            cfg.setDefaultVersion("1.1.0")
        }
    }
}
Related