Using Jenkins Config File Provider Plugin to allow Jenkins slave to access Maven’s global settings.xml


This week I had to write a Jenkins pipeline script (Jenkinsfile) that involved publish build artifacts to Nexus. The project is a Java Maven based Spring Boot application. The build script uses Maven Nexus plugin to publish build artifacts to Nexus. The build pipeline was executed on slaves running on OpenShift container platform. The build pipeline was failing when deploying to Nexus. The build was failing with Return code is: 401, ReasonPhrase: Unauthorized.

It was clear that issue is related to Nexus credentials not available to Maven. The Nexus credentials were available in Maven global settings.xml that resides under ~/.m2/settings.xml. In our case, settings.xml was available on the Jenkins master. As build pipeline ran on slave it does not had access to Jenkins settings.xml.

One solution to this problem is to use Jenkins Config File Provider plugin. It is easy to use this plugin from the Jenkins UI but I was using Jenkinsfile so I had to spend time figuring out the right syntax to get it working.

There are two steps involved in using the plugin. First, you have to create a file of type Global Maven settings.xml using the Jenkins UI. This file will then be made available to your slave. You can use the credentials drop down to select the Nexus credentials that will be added to the generated settings.xml as shown below.

You don’t have to edit the content. The plugin will add two entries to the settings.xml – one for each server. You have to make sure that ServerId is the same as the one you have used in your pom.xml.

The second step is to edit the Jenkinsfile to use the configFileProvider as shown below.

pipeline {
    agent 'maven'
    options {
        disableConcurrentBuilds()
    }

    stages {
        stage('Nexus Deploy') {
            steps {
                configFileProvider([configFile(fileId: 'UUID', variable: 'MAVEN_GLOBAL_SETTINGS')]) {
                    sh 'mvn -gs $MAVEN_GLOBAL_SETTINGS deploy'
                }
            }
        }
    }
}

After making the following change, I was able to successfully deploy artifacts to Nexus.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s