Using FilePath to access workspace on slave in Jenkins pipeline

5,415 views
Skip to first unread message

Harsha Adiga

unread,
Dec 20, 2016, 3:13:40 PM12/20/16
to Jenkins Users
I need to check for the existence of a certain .exe file in my workspace as part of my pipeline build job. I tried to use the below Groovy script from my Jenkinsfile to do the same. But I think the File class by default tries to look for the workspace directory on jenkins master and fails.

@com.cloudbees.groovy.cps.NonCPS
def checkJacoco(isJacocoEnabled) {

new File(pwd()).eachFileRecurse(FILES) { it ->
if (it.name == 'jacoco.exec' || it.name == 'Jacoco.exec') 
isJacocoEnabled = true
}
}

How to access the file system on slave using Groovy from inside the Jenkinsfile? 

I also tried the below code. But I am getting No such property: build for class: groovy.lang.Binding error.  I also tried to use the manager object instead. But get the same error.

@com.cloudbees.groovy.cps.NonCPS
def checkJacoco(isJacocoEnabled) {

channel = build.workspace.channel 
rootDirRemote = new FilePath(channel, pwd()) 
println "rootDirRemote::$rootDirRemote" 
rootDirRemote.eachFileRecurse(FILES) { it -> 
if (it.name == 'jacoco.exec' || it.name == 'Jacoco.exec') { 
println "Jacoco Exists:: ${it.path}" 
isJacocoEnabled = true 
}

Christoph Nenning

unread,
Dec 21, 2016, 3:53:11 AM12/21/16
to jenkins...@googlegroups.com
Hi,


have you tired the findFiles step?

https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/#findfiles-find-files-in-the-workspace


Regards,
Christoph
> --
> You received this message because you are subscribed to the Google
> Groups "Jenkins Users" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to jenkinsci-use...@googlegroups.com.
> To view this discussion on the web visit
https://groups.google.com/
> d/msgid/jenkinsci-users/2b1f6b39-a429-4ea4-ab81-d532561e7065%
> 40googlegroups.com.
> For more options, visit
https://groups.google.com/d/optout.


This Email was scanned by Sophos Anti Virus

Harsha Adiga

unread,
Dec 22, 2016, 9:22:41 AM12/22/16
to Jenkins Users, Christop...@lex-com.net
Hi Christoph,

It worked. I was not aware of this utility apis since I had not installed the pipeline utility plugin on my jenkins. I think it would be nice if this plugin gets included by default  when we install the pipeline plugin. Thanks for your help.

Regards,
Harsha

Rupali Behera

unread,
Jan 19, 2017, 5:19:03 AM1/19/17
to Jenkins Users
I am also facing the same issue, using `findFiles` does not solve the problem as it only searches for the file in the current directory and does not search for it recursively inside other directories. I have posted the same question on stackoverflow https://stackoverflow.com/questions/41720831/unable-to-recognize-workspace-as-directory-using-jenkinsfile-pipline-plugin, any help is greatly appreciated.
Reply all
Reply to author
Forward
0 new messages