5

I am trying to set up scripted JenkinsFile for our pipeline automation and would like to use configFileProvider for maven. As such i end up defining this block in all maven stages within the scripted JenkinsFile. Is there a way to define it just once in the script and reference it across all stages. My sample JenkinsFile as of now looks something like this :-

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}"
      } 
     }
    }

Any help or suggestions here would be greatly appreciated as always.

Cheers,

Ashley

Ashley
  • 1,447
  • 3
  • 26
  • 52

1 Answers1

0

Since it is scripted you don't have to follow a rigid structure and, although not pretty, you can change the order and put the stages inside the 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}"
    }
}

Best, André