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

Dynamic Global Libraries

XMLWordPrintable

      It could be very usefull if you can define Dynamic libraries, the problem that I see now is that you need to set every library on the jenkins configuration, the idea is to only config the base SCM Path and inside the Pipeline define only the name, I did a Proof of concept and work fine.

      Example
      Pipeline:
      @Library("[MyCompanyGitLab]MyLibrary@1.0")

      in the global configuration have the option to define a dynamic LIbrary
      Name: MyCompanyGitLab
      SCM Type GIT
      SCM BaseURL: https://git@stash.intranet.company.com/stash/scm/rda/${library.name}
      SCM Branch ${library.version}
      SCM credentials
      etc...

      SO with this we could only define one configuration in the global configuration and be able to access to all dependencies inside our repository, its like What is doing the Plugin,
      Github folder organitzation, here the commit where is implemented https://github.com/jenkinsci/github-organization-folder-plugin/commit/9367a25b6f7b65c4228ef4f3e12bb947a682e0a5

      I did a basic example that seems work pretty fine but hardcoding my git base repo path

      @Extension(ordinal=0) public static class ForJob extends LibraryResolver {
      
              @Override public boolean isTrusted() {
                  return true;
              }
      
              @Override public Collection<LibraryConfiguration> forJob(Job<?,?> job, Map<String,String> libraryVersions) {
                    List<LibraryConfiguration> libs = new ArrayList<>();
              Pattern p = Pattern.compile("^\\[(.*?)\\].*?");
      
              for (Map.Entry<String, String> entry : libraryVersions.entrySet()) {
                  if (entry.getKey().matches("^\\[(.*?)\\].*?")) {
                      Matcher m = p.matcher(entry.getKey());
                      if (m.find()) {
                          String dynamicLibraryName = m.group(1);
                
                          String name = entry.getKey();
                          String url = "https://git@stash.intranet.company.com/stash/scm/rda/" + entry.getKey().substring(dynamicLibraryName.length()+2) + ".git";
                          // Currently GitHubSCMSource offers no particular advantage here over GitSCMSource.
                          LibraryConfiguration lib = new LibraryConfiguration(name, new SCMSourceRetriever(new GitSCMSource(null, url, "mycredentialsID", "*", "", true)));
                          lib.setDefaultVersion("master");
                          libs.add(lib);
                      }
                  }
              }
              return libs;
      
              }
      

            Unassigned Unassigned
            segator Isaac Aymerich
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: