2017-04-26 16 views
14

Jenkins宣言型パイプラインからビルド名と説明を設定したいが、適切な方法が見つからない。私はパイプラインの後に、環境変数を使ってみました。エージェントブラケットなどのノードブラケットを使っていました。私はいつも構文エラーが出ます。Jenkins宣言型パイプラインからビルド名と説明を設定します

私Jenkinsfileの最後のバージョンはそうのようになります:

pipeline { 
    stages { 
     stage("Build") { 
      steps { 
       echo "Building application..." 
       bat "%ANT_HOME%/bin/ant.bat clean compile" 
       currentBuild.name = "MY_VERSION_NUMBER" 
       currentBuild.description = "MY_PROJECT MY_VERSION_NUMBER" 
      } 
     } 
     stage("Unit Tests") { 
      steps { 
       echo "Testing (JUnit)..." 
      echo "Testing (pitest)..." 
       bat "%ANT_HOME%/bin/ant.bat run-unit-tests" 
      } 
     } 
     stage("Functional Test") { 
      steps { 
       echo "Selenium..." 
      } 
     } 
     stage("Performance Test") { 
      steps { 
       echo "JMeter.." 
      } 
     } 
     stage("Quality Analysis") { 
      steps { 
       echo "Running SonarQube..." 
       bat "%ANT_HOME%/bin/ant.bat run-sonarqube-analysis" 
      } 
     } 
     stage("Security Assessment") { 
      steps { 
       echo "ZAP..." 
      } 
     } 
     stage("Approval") { 
      steps { 
      echo "Approval by a CS03" 
      } 
     } 
     stage("Deploy") { 
      steps { 
       echo "Deploying..." 
      } 
     } 
    } 
    post {  
     always { 
      junit '/test/reports/*.xml' 
     } 
     failure { 
      emailext attachLog: true, body: '', compressLog: true, recipientProviders: [[$class: 'CulpritsRecipientProvider'], [$class: 'DevelopersRecipientProvider']], subject: '[JENKINS] MY_PROJECT build failed', to: '...recipients...' 
     } 
     success { 
      emailext attachLog: false, body: '', compressLog: false, recipientProviders: [[$class: 'DevelopersRecipientProvider']], subject: '[JENKINS] MY_PROJECT build succeeded', to: '...recipients...' 
     } 
    }  
} 

エラーは次のとおりです。理想的

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: 
WorkflowScript: 11: Expected a step @ line 11, column 5. 
       currentBuild.name = "MY_VERSION_NUMBER" 
    ^

WorkflowScript: 12: Expected a step @ line 12, column 5. 
       currentBuild.description = "MY_PROJECT MY_VERSION_NUMBER" 
    ^

、私はbuild.propertiesからMY_PROJECTとMY_VERSION_NUMBERを読むことができるようにしたいのですがファイル、またはJenkinsビルドログから取得できます。その要件に関するガイダンスも高く評価されます。

UPDATE

私は以下の持っていた回答に基づいて、次のように働いた:

stage("Build") { 
    steps { 
     echo "Building application..." 
     bat "%ANT_HOME%/bin/ant.bat clean compile" 

     script { 
      def props = readProperties file: 'build.properties' 
      currentBuild.displayName = "v" + props['application.version'] 
     } 
    } 

ビルドバージョンが自動的にbuild.propertiesファイルを読み出すことにより、パイプラインの間に設定されています。

答えて

21

私はこれがあなたが望むことをすると思います。私はscriptブロックの内側にそれを行うことができました:

pipeline { 
    stages { 
     stage("Build"){ 
      steps { 
       script { 
        currentBuild.displayName = "The name." 
        currentBuild.description = "The best description." 
       } 
       ... do whatever. 
      } 
     } 
    } 
} 

スクリプトは、宣言型のパイプラインから抜け出すためにエスケープハッチの一種です。おそらくそれを行う宣言的な方法がありますが、私はそれを見つけることができませんでした。もう1つのメモ。私はあなたがcurrentBuild.nameの代わりにcurrentBuild.displayNameを望んでいると思う。Jenkinsグローバルのドキュメントでは、currentBuildの下に名前プロパティが表示されませんでした。

+0

作品不思議。ありがとうございました! –

関連する問題