No such file or dirctory Error

Jenkins setup:
Podman Container Setup for Testing purposes,

  • Current Version of Jenkins Container
  • Agent: Ubuntu node with java 17

Hi,
Im just starting with Jenkins und Testing it for my Usecase. Currently im running into an issue, im not able to resolve.

I get the following error when running the following groovy code:

java.io.FileNotFoundException: /home/jenkins/agent/workspace/tompia_system/.conf/config.json (No such file or directory)

faulty Method:

import groovy.json.JsonSlurper
def call() {
    def inputFile = new File("${WORKSPACE}/.conf/config.json")
	def jsonText = inputFile.text
	def InputJSON = JsonSlurper.parseText(jsonText)
	def branch = env.BRANCH_NAME
	return InputJSON << InputJSON.targets."$branch";
}

Path is correct. Works fine on the console with the same user. The part that crashes is parsing with inputFile.text.

File is a regular Java class, and when Jenkins executes the pipeline, new File() gets resolved on Jenkins controller – not on the agent where you have WORKSPACE variable and can run sh(). Because of this, File methods are considered a security breach, since someone can write a pipeline script to read arbitrary files from Jenkins itself.

You’d need special steps that would get executed in the agent “context”: readFile and writeFile; or in your case, readJson.

Thanks, this is it works fine now.