If you have a file called "a.txt" in your workspace, as well as a script called "sysgvy.groovy" that you want to run as a groovy script system. Suppose your "sysgvy.groovy" script should read the file "a.txt".
The problem with this question is that if your script reads "a.txt" directly without providing any path, "sysgvy.groovy" executes and throws an error saying that it cannot find "a.txt".
I tested and found that the following method works well.
def build = Thread.currentThread().executable
Then use
build.workspace.toString()+"\\a.txt"
as a complete location string to replace "a.txt".
It is also important to start the Jenkins master machine by placing "a.txt" and "sysgvy.groovy" in the workspace of the Jenkins master machine. Execution on a slave machine does not work.
Try it, you need to find and read the file in the script without any problems.
If there is a problem with the Thread variable, you just need to import some modules. Therefore, add these lines to the beginning of the code:
import jenkins.* import jenkins.model.* import hudson.* import hudson.model.*
source share