14
votes

I have a simple Jenkinsfile where I want to load some data from the workspace. I am using the pipeline plugin to leverage the Jenkinsfile inside of the repository. The build is farmed off to a matching Jenkins agent. When I try to use "readFile" I get the following message:

java.io.FileNotFoundException: /path/to/jenkins/workspace/XXXXX/project/data.json (No such file or directory)

I also get the same message when trying to load a Groovy file from the workspace.

My Jenkinsfile looks like:

node('master') {
    stage "Start"
    echo "Starting"

    stage "Load File"
    def myJson = readFile "data.json"
}

Any ideas why I can't read these files?

Thanks, Tim

1
Is your Configure SystemWorkspace Root Directory: ${JENKINS_HOME}/workspace/${ITEM_FULLNAME}/project or without the /project? - Gerold Broser
It looks like Jenkins is creating 3 workspaces on this build. 998-loops, 998-loops@script, and 998-loops@tmp. 998-loops@script is the only thing with any data in it. However, the other branch builds both XXX-topic and XXX-topic@script. - timcrider
It looks like everything Jenkins tells the Jenkinsfile script is that its running out of the defined $WORKSPACE, without the @script. Even doing a pwd from within native groovy is telling the script that it's working within $WORKSPACE. This may be a bug. - timcrider
User error, just noticed I don't have a "checkout scm" step. Wow, that was fun. Thanks for the help though Gerold! - timcrider
You would assume that once Jenkinsfile start processing that it has a copy of the repository contents. It does not. In order to bring the repo contents into the process you need 'checkout scm' ` node('master') { checkout scm stage "Start" echo "Starting" stage "Load File" def myJson = readFile "data.json" } ` - timcrider

1 Answers

19
votes

When Jenkins processes a Jenkinsfile it does not automatically pull down the entire source repository. You need to execute "checkout scm" to pull down the contents of the repository. If you fail to do so no other files will be available to the pipeline script.