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

Storing binary file in shared library resources folder

    XMLWordPrintable

    Details

    • Similar Issues:
    • Released As:
      workflow-cps-global-lib 2.10, workflow-basic-steps 2.8.1, workflow-basic-steps 2.10

      Description

      Trying to store png file in resources (project logo, common for all pipelines using shared library) and deploy it from there using:

       

      writeFile file: "logo-96x96.png", text: libraryResource("logo-96x96.png")

       

      but the file is no longer readable after this operation.

      Is there additional parameter I could use (libraryResource has no documented parameters) or is there other suitable way of achieving this functionality?

       

       

        Attachments

          Activity

          Hide
          dnusbaum Devin Nusbaum added a comment - - edited

          It looks like libraryResource reads the contents of the file into a String using the default system encoding, and writeFile encodes the given text using the system default encoding if no other encoding is specified, so doing this with binary files totally mangles the data. It doesn't look like there are any options that would make those steps work for your use case.

          The best workarounds that I can think of are if the image is available at some static URL that Jenkins has access to you could download and write it to the desired location in a shell step using curl/wget, or if the image is in the repo itself you could use a shell step to copy it as desired.

          Show
          dnusbaum Devin Nusbaum added a comment - - edited It looks like libraryResource reads the contents of the file into a String using the default system encoding , and writeFile encodes the given text using the system default encoding if no other encoding is specified, so doing this with binary files totally mangles the data. It doesn't look like there are any options that would make those steps work for your use case. The best workarounds that I can think of are if the image is available at some static URL that Jenkins has access to you could download and write it to the desired location in a shell step using curl/wget, or if the image is in the repo itself you could use a shell step to copy it as desired.
          Hide
          dnusbaum Devin Nusbaum added a comment -

          If we want to add support for this, the best option to me seems like adding an encoding option to libraryResource, and then updating both libraryResource and writeFile to support Base64 as a valid encoding option to be used for binary data.

          Show
          dnusbaum Devin Nusbaum added a comment - If we want to add support for this, the best option to me seems like adding an encoding option to libraryResource , and then updating both libraryResource and writeFile to support Base64 as a valid encoding option to be used for binary data.
          Hide
          quas Jakub Pawlinski added a comment -

          Would be great if we could support this, the resource (png) in my case have to be versioned, I could have separate project for them and expose as artifacts, but its quite an overhead.

           

          Another idea, as I see readFile/writeFile are exposing the encoding parameter, so maybe there is a way to set it in a way to maintain consistency?

          def fileContents = readFile file: "test.txt", encoding: "UTF-8"
          fileContents = fileContents.replace("hello", "world")
          echo fileContents
          writeFile file: "test.txt", text: fileContents, encoding: "UTF-8"
          
          Show
          quas Jakub Pawlinski added a comment - Would be great if we could support this, the resource (png) in my case have to be versioned, I could have separate project for them and expose as artifacts, but its quite an overhead.   Another idea, as I see readFile/writeFile are exposing the encoding parameter, so maybe there is a way to set it in a way to maintain consistency? def fileContents = readFile file: "test.txt" , encoding: "UTF-8" fileContents = fileContents.replace( "hello" , "world" ) echo fileContents writeFile file: "test.txt" , text: fileContents, encoding: "UTF-8"
          Hide
          dnusbaum Devin Nusbaum added a comment - - edited

          Jakub Pawlinski The problem is that UTF-8 is a variable length encoding so any byte whose highest bit is set will not round-trip correctly. You might be able to use a fixed-length encoding like Windows-1252 in your most recent snippet to get things working, but libraryResource currently doesn't take an encoding parameter so you would have to change your system default to use that as a workaround. It seems better to explicitly support this use case.

          Show
          dnusbaum Devin Nusbaum added a comment - - edited Jakub Pawlinski The problem is that UTF-8 is a variable length encoding so any byte whose highest bit is set will not round-trip correctly. You might be able to use a fixed-length encoding like Windows-1252 in your most recent snippet to get things working, but libraryResource currently doesn't take an encoding parameter so you would have to change your system default to use that as a workaround. It seems better to explicitly support this use case.
          Hide
          dnusbaum Devin Nusbaum added a comment -

          Support for Base64 encoding has been added to Pipeline: Shared Groovy Libraries 2.10 and Pipeline: Basic Steps 2.8.1 (or 2.10 if you are on Jenkins 2.121).

          Show
          dnusbaum Devin Nusbaum added a comment - Support for Base64 encoding has been added to Pipeline: Shared Groovy Libraries 2.10 and Pipeline: Basic Steps 2.8.1 (or 2.10 if you are on Jenkins 2.121).

            People

            • Assignee:
              dnusbaum Devin Nusbaum
              Reporter:
              quas Jakub Pawlinski
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: