2017-12-08 15 views
0

私はJenkinsにマルチブランチパイプラインプロジェクトを持っています。 Jenkinsfileを実行するジョブとスクリプトを作成するシードジョブとして使用します。Jenkinsにはどのようなメソッドがありますか.scripts.WorkflowScript

これらのジョブの1つはスレーブで実行する必要があり、スレーブでこのジョブのファイル操作を行う必要があります。 File操作は、FilePathクラスを使用して実装されます。私が今必要とするのは、FilePathを構築するチャンネルだけです。

ワークフロースクリプトからチャンネルを取得するにはどうすればよいですか? WorkflowScriptのAPIリファレンスはどこにありますか。私はすでに見つけた何

は、私はこのコード

println Jenkins.instance.slaves 
for(def slave : Jenkins.instance.slaves){ 
    println slave.channel   
} 

しかし、どのように私はどのインスタンスで見つけるか私のスクリプトが実行されていることによって、すべてのインスタンスのチャンネルを取得するということですか?

何か提案がありますか? APIへのポインターまたは私の問題に対するいくつかの回避策。

は、これは私が思い付いた(安っぽい)ソリューションである

+0

なぜあなたは 'Jenkinsfile'でこれをしたいのですか?あなたは全体的に何を達成しようとしていますか?既存の 'node'ステップと' sh'機能を使用できないのはなぜですか?私にとって、この質問は[XY問題](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)のように聞こえます。 – mkobit

+0

私が実際に達成しようとしているのは、Jenkinsクライアントでカスタマイズされた(Apache)Webサーバーをカスタマイズすることです。このためには、Webサーバーを起動する前にhttps.confファイルを変更する必要があります。 shov/batコマンドよりGroovyクラスを使用するほうが好きです。なぜなら私はGroovyにもっと精通していて、実際にsh/batコマンドを実行してからgroovyクラスをテストする方が良いと思うからです。 – genmad

答えて

0

ありがとうございました。

私は仕事に使ったラベルのチャンネルを取得するために自分自身にUtilクラスを書きました。

import jenkins.model.Jenkins 
import hudson.FilePath 
import hudson.remoting.Channel  
/** 
* returns the channel of the given label, might be null 
* 
* realy bad solution why: when multiple JenkinsSlaves have the same label, this will probably not work anymore, 
* because the channel of the first node is returned 
* For empty labels it does not work either 
* @param jenkins the jenkins instance 
* @param label the associated label for the running node, not empty, not null 
* @return a channel of a Node associated with given label, might be null 
*/ 
@com.cloudbees.groovy.cps.NonCPS 
public static Channel getChannel(Jenkins jenkins, String label) { 
    assert jenkins !=null : 'JenkinsUtil.getChannel() does not work for null Jenkins' 
    assert label!=null : 'JenkinsUtil.getChannel() does not work for null Labels' 
    assert !label.isEmpty() : 'JenkinsUtil.getChannel() does not work for empty Labels' 
    Set<Node> nodes = jenkins.getLabel(label).getNodes() 
    assert nodes.size() < 2 : 'JenkinsUtil.getChannel() might not work correctly for Label which are associated with multiple nodes. Fix Me!' 
    assert nodes.size() > 0 : "JenkinsUtil.getChannel(): No node found for Label ${label}" 
    return nodes[0].getChannel() 
} 

このチャンネルでは、リモートマシン上でファイルを作成できるようになりました。

なぜ私はこのようにしましたか:WorkkflowScriptでは 'build'変数が利用できませんでした。

関連する問題