Currently it is only possible to get library resources as a string using

      libraryResource 'foo.txt'

      However, this does not work for binary files and is also awkward to use when you need the resource as a file; you have to use a workaround like

      def fooContent = libraryResource 'foo.txt'
      writeFile file: 'foo.txt', text: fooContent
      

      It would be handy to have a step which provides the resource as file, like this (file could be optional and per default equal to resource):

      libraryResourceAsFile resource: 'foo.txt', file: 'foo-from-resource.txt'
      

          [JENKINS-43686] Provide libraryResourceAsFile step

          Jesse Glick added a comment -

          Would be useful.

          Workaround for binary files would I guess be to Base64-encode them.

          Jesse Glick added a comment - Would be useful. Workaround for binary files would I guess be to Base64-encode them.

          Jose added a comment -

          The following works nicely

              def shResource(String name) {
                  String contents = workflow.libraryResource(resource: name, encoding: "Base64")
                  workflow.sh """
                      echo "${contents}" | base64 -d > /tmp/${name} && chmod +x /tmp/${name} && /tmp/${name}
                  """
              }
          

          that said it'd be great to have a resourceAsFile or maybe even resourceAsPath.

          dnusbaum, sorry to pull you in but I'm hoping to only bother you for a minute. We've been going over the plugin code and are thinking this maybe as simple as adding the resources to the classloader just like src and vars directories. That'd facilitate getting the resource absolute URL through a step. Is that a valid approach, or are we not considering something else?

          Jose added a comment - The following works nicely def shResource( String name) { String contents = workflow.libraryResource(resource: name, encoding: "Base64" ) workflow.sh """ echo "${contents}" | base64 -d > /tmp/${name} && chmod +x /tmp/${name} && /tmp/${name} """ } that said it'd be great to have a resourceAsFile or maybe even resourceAsPath. dnusbaum , sorry to pull you in but I'm hoping to only bother you for a minute. We've been going over the plugin code and are thinking this maybe as simple as adding the resources to the classloader just like src and vars directories. That'd facilitate getting the resource absolute URL through a step. Is that a valid approach, or are we not considering something else?

          Devin Nusbaum added a comment -

          jam01 I don't think that would work. Libraries are checked out on the Jenkins controller, so the URL would only be accessible from the controller, and you wouldn't be able to access the file using sh on a build agent. I think the proposed step would need to explicitly copy the requested file to the current build workspace.

          Devin Nusbaum added a comment - jam01  I don't think that would work. Libraries are checked out on the Jenkins controller, so the URL would only be accessible from the controller, and you wouldn't be able to access the file using sh on a build agent. I think the proposed step would need to explicitly copy the requested file to the current build workspace.

            Unassigned Unassigned
            schulzha Hans Schulz
            Votes:
            4 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated: