2017-12-20 2 views
1

나는 사용자 정의 단계를 만들려면 : https://jenkins.io/doc/book/pipeline/shared-libraries/#defining-custom-steps선언적 파이프 라인에 대한 사용자 지정 단계를 정의 할 수 있습니까? 여기에 설명 된대로

스크립트는 다음과 같습니다

// vars/buildPlugin.groovy 
def call(body) { 
    // evaluate the body block, and collect configuration into the object 
    def config = [:] 
    body.resolveStrategy = Closure.DELEGATE_FIRST 
    body.delegate = config 
    body() 
... 

그리고 나는 스크립트 파이프 라인에서 다음과 같이 실행할 수 있습니다 :

buildPlugin { 
    name = 'git' 
} 

이는 선언적 파이프 라인에서 스크립트 블록으로 마무리해야 함을 의미합니다.

script { 
    buildPlugin { 
     name = 'git' 
    } 
} 

커스텀 스크립트와 Groovy 클래스가 많아서 스크립트를 내 파이프 라인의 스크립트 블록으로 감싸야만합니다. 선언적 파이프 라인이 스크립트 {}없이 사용할 수있는 방법으로 그루비 스크립트를 작성할 수 있습니까?

+0

이렇게하면 Jenkinsfile의 유효성 검사가 실패합니다. 'script {}'내에 전역 변수와 라이브러리 호출을 배치하는 것은 현재 어려운 요구 사항입니다. Jenkins 선언 구문이 Jenkinsfile에 혼란을 야기한다고 생각하는 의견에 동의하십시오. –

+0

'script'없이 이것을 시도 했습니까? 어떤 오류가 발생 했습니까? [이 기사] (https://jenkins.io/blog/2017/02/15/declarative-notifications/)에 따르면이 작업을 수행 할 수 있어야합니다. –

+0

@VitaliiVitrenko 그 글은 다음과 같이 작성되었습니다 : https://issues.jenkins-ci.org/browse/JENKINS-42360 –

답변

1

당신은 잘 작동 예입니다 script 래퍼 여기

없이 선언 pipline도 사용할 수 있습니다 :

스크립트가

//vars/shOut.groovy 
def call(shellScript) { 
    return sh(returnStdout: true, script: shellScript).trim() 
} 

Jenkinsfile

@Library('modelsLib') _ 

pipeline { 
    agent { label 'master' } 

    stages { 
    stage('some stage') { 
     steps { 
     echo "hello!" 
     shOut 'touch xxyyzz' 
     sh 'ls -otr' 
     } 
    } 
    } 
} 

출력

[Pipeline] { 
[Pipeline] stage 
[Pipeline] { (some stage) 
[Pipeline] echo 
hello! 
[Pipeline] sh 
[test-pipeline] Running shell script 
+ touch xxyyzz 
[Pipeline] sh 
[test-pipeline] Running shell script 
+ ls -otr 
total 32 
-rw-r--r-- 1 jenkins  0 Dec 20 17:59 xxyyzz 
[Pipeline] } 
[Pipeline] // stage 
[Pipeline] } 
[Pipeline] // node 
[Pipeline] End of Pipeline 
관련 문제