Declare configFileProvider only once in JenkinsFile and refer to from all stages
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
-
Any updates on this?
0 -
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 -
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 -
Any updates on this? we are almost in the final phase of getting this implementation wrapped up for our cloudbees Jenkins.
0 -
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 file0 -
Test
0 -
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 -
Thanks that worked!
0
Please sign in to leave a comment.
Comments
8 comments