2017-12-08 1 views
0

저는 Jenkins에 멀티 브랜드 파이프 라인 프로젝트가 있습니다. Jenkinsfile을 종업원으로 사용하여 실행할 작업과 스크립트를 만듭니다.Jenkins의 메소드는 무엇입니까? 스크립트 .WorkflowScript

이러한 작업 중 하나는 슬레이브에서 실행해야하며 슬레이브에서이 작업에 대한 파일 작업을 수행해야합니다. File 작업은 FilePath 클래스를 사용하여 구현됩니다. 지금 당장 필요한 것은 FilePath를 만드는 채널입니다.

워크 플로 스크립트 내에서 채널을 얻으려면 어떻게해야합니까? WorkflowScript에 대한 API 참조는 어디에서 찾을 수 있습니까? 이 코드

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

모든 인스턴스 채널을 얻을하지만 어떻게 인스턴스 내 스크립트가 실행되는 찾을 수 있습니까 것을 이미 발견 무엇

은?

제안 사항? 내 문제에 대한 API 또는 일부 해결 방법에 대한 포인터.

고맙습니다.

+0

왜 '젠킨스 파일'에서 이것을하고 싶습니까? 전반적으로 달성하려는 것은 무엇입니까? 왜 기존의'node' 단계와'sh' 기능을 사용할 수 없습니까? 나에게이 질문은 [XY 문제] (https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)처럼 들린다. – mkobit

+0

내가 실제로 달성하고자하는 것은 Jenkins 클라이언트에서 사용자 정의 된 (Apache) 웹 서버를 시작하는 것입니다. 이를 위해 웹 서버를 시작하기 전에 https.conf 파일을 변경해야합니다. shov/bat 명령보다 Groovy 클래스를 사용하는 것이 더 편합니다. 왜냐하면 Groovy에 익숙하기 때문에 Groovy 클래스를 테스트 한 다음 실제로 sh/bat 명령을 테스트하는 것이 좋습니다. – 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() 
} 

이 채널을 통해 이제 원격 컴퓨터에서 파일을 만들 수 있습니다.

FilePath remoteWorkspace = new FilePath(JenkinsUtil.getChannel(Jenkins.instance,LABEL_STRING), env.WORKSPACE) 
FilePath webserverConfiguration = new FilePath(remoteWorkspace, 'webserver.conf') 
webserverConfiguration.write('someText', null) 

왜 이렇게 했습니까? 'build'변수는 WorkkflowScript에서 사용할 수 없었습니다.

관련 문제