2017-10-09 13 views
0

プログラミングは豊富ではありませんが、習得するのは楽しいです。jenkinsfile groovyスクリプトでローカル変数をロードする

質問:jenkinsfileビルドのエラーデータは、ファイル内でローカルで正常に動作しますが、.groovyスクリプトとしてロードされると、「サイレント」に失敗します。ロードされたGroovyスクリプトのチャンネルにこの情報を投稿する理由/方法に関するガイダンスはありますか?

[cure-deploy] Running shell script 
+ introduce error here 
/home/jenkins/remote/workspace/blablabla/script.sh: line 2: introduce: command not found 
[Pipeline] } 
[Pipeline] // dir 
[Pipeline] } 
[Pipeline] // timeout 
[Pipeline] load 
[Pipeline] { (ci/curlBuildFailed.groovy) 
[Pipeline] } 
[Pipeline] // load 
[Pipeline] } 
[Pipeline] // wrap 
[Pipeline] } 
[Pipeline] // node 
[Pipeline] } 
[Pipeline] // load 
[Pipeline] } 
[Pipeline] // stage 
[Pipeline] } 
[Pipeline] // node 
[Pipeline] End of Pipeline 
ERROR: script returned exit code 127 
Finished: FAILURE 

注:上記のランニング

はJenkinsfile

#!groovy 
properties([[$class: 'jenkins.model.BuildDiscarderProperty', strategy: [$class: 'LogRotator', numToKeepStr: '5', artifactNumToKeepStr: '5']]]) 

node('node_name'){ 

    def err = null 
    currentBuild.result = "SUCCESS" 
    role_name = "deploy" 

    try { 
     timeout(60){ 
      stage "${role_name}" 
       deleteDir() 
       checkout scm 
       sh 'introduce error here' 
     } 
    } 
    catch (error){ 
     err = error 
     currentBuild.result = "FAILURE" 
     load "ci/curlBuildFailed.groovy" 
    } 
    finally { 
     if (err){ 
      throw err 
     } 
    } 
} 

curlBuildFailed.groovy

sh "curl -i -X POST -d \'payload={" + 
"\"text\": \"${env.JOB_NAME}: ${env.BUILD_URL} - build failed with ${err}!\", " + 
"\"username\": \"JenkinsBot\", " + 
"\"icon_url\": \"$FAIL_BUILD\"}\' $DEVOPS_NOTIFY_URL " 

これを生成

  • curlBuildFailed.groovyの内容を同じ 位置にドロップすると、load "ci/curlBuildFailed.groovy"という出力が作成され、目的の 出力が作成されます。
  • 犯人を で${err}に絞り込みました。その変数を削除すると、ただちに が問題なく表示されます。
  • (これに類似した約30の役割がありますので、重複したコードを抽象的な構造に維持しようとしています)
  • これは数時間前から検索されていますが、 Aまだ解決:
  • jenkins plain catch blocks
  • don't think I need a 'returns this;' but who knows
  • Passing Variables in Jenkinsfile Closure

はお時間をいただき、ありがとうございます! - Sam

答えて

0

メッセージをMattermostに送信するためにgroovyスクリプトを使用する必要はありません。 Mattermostに通知メッセージを送信するのに役立つJenkins Pluginがあります。 https://github.com/jenkinsci/mattermost-plugin

インストールおよび設定後、mattermostSendを使用して通知を送信できます。

その後、あなたのパイプラインはこれに類似します:

node('node_name') {def err = null 
    currentBuild.result = "SUCCESS" 
    role_name = "deploy" 

    try { 
     timeout(60){ 
      stage "${role_name}" 
       deleteDir() 
       checkout scm 
       sh 'introduce error here' 
     } 
    } 
    catch (error){ 
     err = error 
     currentBuild.result = "FAILURE" 
     mattermostSend color: 'danger', message: 'Message from Jenkins Pipeline', text: \"${env.JOB_NAME}: ${env.BUILD_URL} - build failed with ${err}!\" 
    } 
    finally { 
     if (err){ 
      throw err 
     } 
    } 
} 
関連する問題