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

additional library() call not possible after failure

    XMLWordPrintable

Details

    Description

      When trying to load a shared library from a non existing branch it is not possible to load from default or master branch afterwards.

      Consider the following code:

      try {
        // try loading from branch
        library("sharedlib@${env.BRANCH_NAME}")
      }
      catch(Throwable e) {
        library("sharedlib")
      }

      Running this pipeline will result in something like

      [Pipeline] library
      Loading library sharedlib@feature/my-branch
      Attempting to resolve feature/my-branch from remote references...
      ...
      ERROR: Could not resolve feature/my-branch
      ...
      [Pipeline] library
      Only using first definition of library sharedlib

      It seems that "sharedlib" is recorded as "already loaded" despite the error.

       

      Attachments

        Activity

          gordin Christoph Vogtländer added a comment - - edited

          As a workaround it is possible to remove the LibraryRecord from the LibrariesAction in the catch block:

          try {
            library("sharedlib@${env.BRANCH_NAME}")
          }
          catch(Throwable e) {
            def libs = currentBuild.rawBuild.getAction(LibrariesAction.class).getLibraries()
            libs.remove(libs.find { it.name == 'sharedlib' })
            currentBuild.rawBuild.replaceAction(new LibrariesAction(libs));
            libs = null
            library("sharedlib")
          }
          
          gordin Christoph Vogtländer added a comment - - edited As a workaround it is possible to remove the LibraryRecord from the LibrariesAction in the catch block: try { library( "sharedlib@${env.BRANCH_NAME}" ) } catch (Throwable e) { def libs = currentBuild.rawBuild.getAction(LibrariesAction.class).getLibraries() libs.remove(libs.find { it.name == 'sharedlib' }) currentBuild.rawBuild.replaceAction( new LibrariesAction(libs)); libs = null library( "sharedlib" ) }
          gordin Christoph Vogtländer added a comment - - edited

          Since the latest update to workflow-cps-global-lib 2.18 the workaround is no longer working (see JENKINS-41037): 

           

          java.lang.UnsupportedOperationException
           	at java.base/java.util.Collections$UnmodifiableCollection.remove(Collections.java:1063)
          

           

           

          gordin Christoph Vogtländer added a comment - - edited Since the latest update to workflow-cps-global-lib 2.18 the workaround is no longer working (see JENKINS-41037 ):    java.lang.UnsupportedOperationException at java.base/java.util.Collections$UnmodifiableCollection.remove(Collections.java:1063)    

          New workaround which is compatible to getLibraries() returning an unmodifiable collection:

          try {
            library("sharedlib@${env.BRANCH_NAME}")
          }
          catch(Throwable e) {
            def libs = currentBuild.rawBuild.getAction(LibrariesAction.class).getLibraries()
            currentBuild.rawBuild.replaceAction(new LibrariesAction(libs.findAll { it.name != name }));
            libs = null
            library("sharedlib")
          }
          
          gordin Christoph Vogtländer added a comment - New workaround which is compatible to getLibraries() returning an unmodifiable collection: try { library( "sharedlib@${env.BRANCH_NAME}" ) } catch (Throwable e) { def libs = currentBuild.rawBuild.getAction(LibrariesAction.class).getLibraries() currentBuild.rawBuild.replaceAction( new LibrariesAction(libs.findAll { it.name != name })); libs = null library( "sharedlib" ) }

          People

            Unassigned Unassigned
            gordin Christoph Vogtländer
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: