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

readFileFromWorkspace does not resolve file path relative to calling script

    • Icon: Improvement Improvement
    • Resolution: Unresolved
    • Icon: Minor Minor
    • job-dsl-plugin
    • None

      It appears that readFileFromWorkspace() treats an input relative path as relative to the workspace root. It would be useful if it could also search for the file relative to the calling script. Specifically, the relative path lookup makes it more difficult to test DSL scripts locally using the User Power Moves command-line method, as the script root there is not necessarily the same as the workspace root in the live build.

          [JENKINS-29135] readFileFromWorkspace does not resolve file path relative to calling script

          In 1.45 the _FILE_ variable was added, see JENKINS-25935. As a workaround you can that variable to do something like this:

          import java.nio.file.Paths
          
          def myFile = 'test.txt'
          def scriptDir = Paths.get(__FILE__).parent
          def relativePath = Paths.get(WORKSPACE).relativize(scriptDir).resolve(myFile).toString()
          def content = readFileFromWorkspace(relativePath)
          

          Daniel Spilker added a comment - In 1.45 the _ FILE _ variable was added, see JENKINS-25935 . As a workaround you can that variable to do something like this: import java.nio.file.Paths def myFile = 'test.txt' def scriptDir = Paths.get(__FILE__).parent def relativePath = Paths.get(WORKSPACE).relativize(scriptDir).resolve(myFile).toString() def content = readFileFromWorkspace(relativePath)

            jamietanna Jamie Tanna
            sumdumgai A C
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: