在JenkinsFile中仅声明一次configFileProvider,并在所有阶段进行引用

时间:2018-11-26 17:06:46

标签: jenkins groovy jenkins-plugins jenkins-pipeline jenkins-groovy

我正在尝试为我们的管道自动化设置脚本化的JenkinsFile,并希望将configFileProvider用于maven。这样,我最终在脚本化的JenkinsFile中的所有Maven阶段中定义了此块。 有没有一种方法可以在脚本中只定义一次并在所有阶段中引用它。到目前为止,我的样本JenkinsFile看起来像这样:-

node {
   
   
   def mvnHome
   def mvnSettings
   
   stage('Prepare') {
      mvnHome = tool 'maven-3.5.4'

   }

   stage('Checkout') {
      checkout scm
   }
   
   stage('Build'){
      configFileProvider(
        [configFile(fileId: '**********', variable: 'MAVEN_SETTINGS')]) {
        sh "'${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS install"
      }
   }
   
   stage('Integration Test') {
   
   
       sh "'${mvnHome}/bin/mvn' -Dmaven.test.failure.ignore clean verify"
   }
   
   stage('Sonar') {
      configFileProvider(
        [configFile(fileId: '**********', variable: 'MAVEN_SETTINGS')]) {
   		sh "'${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS sonar:sonar"
		}
   }

   stage('Packaging') {
      configFileProvider(
        [configFile(fileId: '**********', variable: 'MAVEN_SETTINGS')]) {
        sh "'${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS package"
        }
   }

   stage('Deploy') {
      configFileProvider(
        [configFile(fileId: '**********', variable: 'MAVEN_SETTINGS')]) {
		
        sh "'${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS deploy}"
      } 
     }
    }

这里的任何帮助或建议将一如既往。

干杯

阿什利

1 个答案:

答案 0 :(得分:0)

由于它是脚本编写的,因此您不必遵循严格的结构,尽管它不是很漂亮,但是您可以更改顺序并将阶段放置在configFileProvider中:

configFileProvider(
            [configFile(fileId: '**********', variable: 'MAVEN_SETTINGS')]) {

    stage('Build'){
        sh "'${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS install"
    }

    stage('Integration Test') {
        sh "'${mvnHome}/bin/mvn' -Dmaven.test.failure.ignore clean verify"
    }

    stage('Sonar') {
        sh "'${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS sonar:sonar"
    }

    stage('Packaging') {
        sh "'${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS package"
    }

    stage('Deploy') {   
        sh "'${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS deploy}"
    }
}

最好, 安德烈