Sivakumar Kayaroganam
Sivakumar Kayaroganam

Reputation: 370

Jenkins pipeline - File not found exception

I am getting file not exception in jenkins although the file is available in the expected location. What i am trying to do here is, getting the latest committed file and storing in filescommitted variable later passing that as input to readJSON file. Seems the variable value is passed but unable to find the file in the location.

Error:

java.io.FileNotFoundException: /appl/JenkinsSlave/workspace/SDA_WIP_development/RA agent/java-infra-war-tomcat-piaas.json
 does not exist.

It works only if i hardcode the file name

readJSON file: "java-infra-war-tomcat-piaas.json"

It does not work for the below cases. Can anyone tel me what is the issue please ?

readJSON file: filesCommitted
readJSON file: "${env.WORKSPACE}/${filesCommitted}"
readJSON file: "${WORKSPACE}/${filesCommitted}"

Jenkins pipeline script:

import groovy.json.JsonSlurper
        import groovy.json.*
        import groovy.json.JsonBuilder

        node('java'){
            def gitURL = Git_URL as String
            def branchName = Branch as String
            def GitHubCredentialsId = GitHub_Credentials_Id as String
            def filesCommitted

            stage('Clean')
            {
              deleteDir()   
            }

            stage('SCM')
            {
                git branch: branchName, credentialsId: GitHubCredentialsId, url: gitURL
                filesCommitted = sh(script:'git diff --name-only HEAD^', returnStdout:true)
                def files = sh(script: "find . -name '*.json'", returnStdout:true)    
                println ("new files commited:" + filesCommitted)
                println ("all files commited:" + files)
                sh "pwd"
                sh "ls"
            }

            stage('Add agents in RA'){
                //def infra_file = unstashParam "infrafile"
                //println(infra_file)
                println(env.WORKSPACE)
                println (filesCommitted)
                def projects = readJSON file: "${env.WORKSPACE}/${filesCommitted}"
                println(projects)
             }
}

Complete jenkins console output:

[Pipeline] sh
+ git diff --name-only 'HEAD^'
[Pipeline] sh
+ find . -name '*.json'
[Pipeline] echo
new files commited:java-infra-war-tomcat-piaas.json

[Pipeline] echo
all files commited:./java-infra-ear-jboss-piaas.json
./java-infra-war-tomcat-piaas.json
./java-infra-war-tomcat-piaas_demo.json
./angular-infra-zip-iis-piaas.json
./dotnet-infra-zip-iis-piaas.json

[Pipeline] sh
+ pwd
/appl/JenkinsSlave/workspace/SDA_WIP_development/RA agent
[Pipeline] sh
+ ls
angular-infra-zip-iis-piaas.json
dotnet-infra-zip-iis-piaas.json
java-infra-ear-jboss-piaas.json
java-infra-war-tomcat-piaas_demo.json
java-infra-war-tomcat-piaas.json
README.md
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Add agents in RA)
[Pipeline] echo
java-infra-war-tomcat-piaas.json

[Pipeline] readJSON
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
java.io.FileNotFoundException: /appl/JenkinsSlave/workspace/SDA_WIP_development/RA agent/java-infra-war-tomcat-piaas.json
 does not exist.

Upvotes: 0

Views: 2302

Answers (1)

zett42
zett42

Reputation: 27786

It looks like there is an extraneous linebreak at the end of filesCommitted content.

Try this:

filesCommitted = sh(script:'git diff --name-only HEAD^', returnStdout:true).trim()

def projects = readJSON file: filesCommitted

It is usually a good habit to add a trim() to the sh step when using returnStdout:true. This will remove any extraneous whitespace before and after the returned string.

Upvotes: 3

Related Questions