Home > Blockchain >  How to read file through jenkins from github
How to read file through jenkins from github

Time:01-04

I am trying to read a file from github using readFile,


                        sshagent (credentials: ["${github_cred}"]) {
                        script {
                                sh """
                                    git checkout test_branch
                                """
                               def file = readFile(file: "/myrepo/${params.value}.txt") 

But in other case, this file will not be available for certain parameters passed. So I would like to check if the file exists in github and proceed with the next steps if it is available or else it should skip the stage.

First Try:

When I try to do with the above code, it is throwing NoSuchFileException when it is not available. I tried with fileExists function which actually works only on the controller node, not on the github. Is there any possible to achieve this?

Second Try:

I also tried with git show as below but I got illegal string body or character after dollar sign error, I don't know what is wrong here.

git show HEAD~1:/myrepo/"${params.value}".txt > /dev/null 2>&1

CodePudding user response:

The fileExists should run in the current node the Pipeline is running on. So it should ideally work.

if (fileExists("/myrepo/${params.value}.txt") {
   def file = readFile(file: "/myrepo/${params.value}.txt") 
}

Another easy workaround is to wrap your readFile with a try-cath given you know readFile will fail if the file is not available.

def isSuccess = true
def file = null

try {
 file = readFile(file: "/myrepo/${params.value}.txt") 
}catch(e) {
 isSuccess = false
}

CodePudding user response:

The Jenkins machine is windows or macos or linux?

sh """

pwd
git checkout test_branch
"""

In case it's linux or macos add pwd to see the local full path of the repo And then use this full path in the readfile

  • Related