groovyjenkins-pipeline

Jenkins pipeline cannot open an existing file


I have the following test code to try to open and modify the contents of a file:

private def updateTagInfoInFile(final String file) {
  final String tmpFile = "tmp.txt"
  sh "ls -al /data/workspace/Utilities/Playground"
  sh "whoami; pwd; ls -al"
  new File("/data/workspace/Utilities/Playground/copy-artifact/$file").eachLine{line->
    if (line.startsWith("Tag")) {
      // Count the number of spaces between "Tag:     <tag>" to preserve the formatting
      final Integer numSpaces = line.length() - line.replaceAll(" ", "").length()
      line = "Tag:"+" ".multiply(numSpaces)+"newTag"
    }
    sh "echo '$line' >> $tmpFile"
  }
  sh "mv $tmpFile $file"
}

updateTagInfoInFile("someFile.txt")

But I get the following error:

+ ls -al /data/workspace/Utilities/Playground
total 0
drwxrwxr-x 4 ubuntu ubuntu  52 Mar 29 16:46 .
drwxrwxr-x 5 ubuntu ubuntu 136 Mar 29 16:38 ..
drwxrwxr-x 2 ubuntu ubuntu  45 Mar 29 16:38 copy-artifact
drwxrwxr-x 3 ubuntu ubuntu  30 Mar 29 17:29 copy-artifact@tmp
+ whoami
ubuntu
+ pwd
/data/workspace/Utilities/Playground/copy-artifact
+ ls -al
total 4
drwxrwxr-x 2 ubuntu ubuntu   45 Mar 29 16:38 .
drwxrwxr-x 4 ubuntu ubuntu   52 Mar 29 16:46 ..
-rw-rw-r-- 1 ubuntu ubuntu 2368 Mar 29 16:24 someFile.txt
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Also:   org.jenkinsci.plugins.workflow.actions.ErrorAction$ErrorId: b41cce20-1d11-416e-8e89-e5aec9147ea5
java.io.FileNotFoundException: /data/workspace/Utilities/Playground/copy-artifact/someFile.txt (No such file or directory)
    at java.base/java.io.FileInputStream.open0(Native Method)
    at java.base/java.io.FileInputStream.open(Unknown Source)
    at java.base/java.io.FileInputStream.<init>(Unknown Source)
    at groovy.util.CharsetToolkit.<init>(CharsetToolkit.java:78)
    at org.codehaus.groovy.runtime.ResourceGroovyMethods.newReader(ResourceGroovyMethods.java:1599)
...

The file is obviously there, so why am I getting the error?


Solution

  • This is a common pitfall that I have also encountered myself in the past. The File class performs operations on the Jenkins master, and not necessarily the build agents (unless the agent is the master). The following line of code (which is definitely throwing the Exception):

    new File("/data/workspace/Utilities/Playground/copy-artifact/$file")
    

    should probably be replaced with:

    import hudson.FilePath
    import jenkins.model.Jenkins
    
    new FilePath(Jenkins.getInstance().getComputer(env['NODE_NAME']).getChannel(), "/data/workspace/Utilities/Playground/copy-artifact/$file")
    

    However, I am not completely sure that code will perfectly replace the functionality you are currently seeking, and I believe I adapted it from an answer I saw years ago by someone more expert in Pipeline than myself. There may still be an issue here, and we can discuss it in the comments if necessary.

    EDIT for full solution

    I added the necessary functions to make Matthew's solution work for me.

    new FilePath(Jenkins.getInstance().getComputer(env['NODE_NAME']).getChannel(), "/data/workspace/Utilities/Playground/copy-artifact/$file").readToString().split("\n").each{line->
      // same code as above
    }