Skip to main content

Declare configFileProvider only once in JenkinsFile and refer to from all stages

Comments

8 comments

  • Ashley Giles

    Any updates on this?

    0
  • Adg Adg

    HI Ashley 

    You may use each loop to iterate same kind scripts in different stage 

    but for you have to create a  list (JAVA COLLECTION) to doing this a sample is like this 


    node {
    def buildStage=[Build,Sonar,Packaging,Deploy]
         
    buildStage.each {x ->
               println "$x"
    stage('$x') { configFileProvider( [configFile(fileId: '**********', variable: 'MAVEN_SETTINGS')]) { sh "'${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS $x" } }
        }
    }
    0
  • Ashley Giles

    Thanks for the reply.

    Could you please help with a sample script to achieve that using the above details. i tried a few and didn't seem to work , probably my lack of skills in scripting. You may just use Build and Deploy stage for the script for now. I would like to see how the list (Java collection) can be used here.

    Thanks for all the support so far. Appreciate a lot!

    Cheers,

    Ashley

    0
  • Ashley Giles

    Any updates on this? we are almost in the final phase of getting this implementation wrapped up for our cloudbees Jenkins.

    0
  • Ashley Giles

    i was able to get this work using each iteration. But that defeats the purpose of a proper visibility of all defined stages in a Jenkinsfile for the pipeline and at the end of the build the pipeline steps do not show up with the true stage names. Though the script works and does what it needs to , but its more of a code doing all the actions out of a single block.
    Is there a way to still have all stages defined as they usually are in a scripted Jenkinsfile and then just reference this Config File provider wrapper defined once globally for the file

    0
  • Ali Jenkins

    Test

    0
  • Allan Burdajewicz

    Hi Ashley,

    You can factor the code by creating a function like the following that can be used in each stage:

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

    node {

    [...]

    stage('Build') {
    mvn "install"
    }

    stage('Integration Test') {
    mvn "-Dmaven.test.failure.ignore clean verify"
    }

    stage('Sonar') {
    mvn "sonar:sonar"
    }

    [...]
    }

    Note: I would recommend to have a look at the Pipeline Maven Plugin. It enables you define config files globally, at folder level or inside your pipeline.

    Regards,

    0
  • Ashley Giles

    Thanks that worked!

    0

Please sign in to leave a comment.

About CloudBees Support

Our Support Engineers are available to help with any questions or problems you may have with any of our products.