2017-12-20 15 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クラスがたくさんあり、パイプラインのスクリプトブロックでそれらをラップしなければならないものが混乱します。宣言型パイプラインがスクリプト{}なしで使用できるように、Groovyスクリプトを書くことはできますか?あなたがここにscriptラッパー

せず、宣言piplineでも、それを使用することができます

+0

これを実行しようとすると、Jenkinsfileは検証に失敗します。グローバルなvarとライブラリの呼び出しを '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

はうまく機能の例です:

スクリプト

//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 
関連する問題