2016-12-20 9 views
5

パイプラインビルドジョブの一部としてワークスペースに特定の.exeファイルが存在するかどうかを確認する必要があります。私はJenkinsfileの以下のGroovyスクリプトを使って同じことをしようとしました。しかし、私は、Fileクラスはデフォルトで、jenkins master上のworkspaceディレクトリを探して失敗すると考えています。FilePathを使用してJenkinsパイプラインのスレーブ上のワークスペースにアクセスする

@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 
    } 
} 

Jenkinsfile内からGroovyを使用してスレーブ上のファイルシステムにアクセスするにはどうすればよいですか?

また、以下のコードを試しました。しかし、私はNo such property: build for class: groovy.lang.Bindingエラーを取得しています。私はまたマネージャオブジェクトを代わりに使用しようとしました。しかし、同じエラーを取得します。

@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 
    } 
} 

答えて

10

は同じ問題を抱えていた、この解決策を見つけた:

import hudson.FilePath; 
import jenkins.model.Jenkins; 

node("aSlave") { 
    writeFile file: 'a.txt', text: 'Hello World!'; 
    listFiles(createFilePath(pwd())); 
} 

def createFilePath(path) { 
    if (env['NODE_NAME'] == null) { 
     error "envvar NODE_NAME is not set, probably not inside an node {} or running an older version of Jenkins!"; 
    } else if (env['NODE_NAME'].equals("master")) { 
     return new FilePath(path); 
    } else { 
     return new FilePath(Jenkins.getInstance().getComputer(env['NODE_NAME']).getChannel(), path); 
    } 
} 
@NonCPS 
def listFiles(rootPath) { 
    print "Files in ${rootPath}:"; 
    for (subPath in rootPath.list()) { 
     echo " ${subPath.getName()}"; 
    } 
} 

それはenv変数にアクセスする必要があるので、ここで重要なのはcreateFilePath() ins'tが@NonCPSで注釈を付けることです。 @NonCPSを使用すると、 "パイプラインの良さ"へのアクセスが削除されますが、一方ではすべてのローカル変数がシリアライズ可能である必要はありません。 これで、listFiles()メソッド内のファイルを検索できます。

+2

保存された人生:) ありがとう!! –

関連する問題