Search code examples
jenkinsjenkins-pipelinejgitflow-maven-plugin

Jenkins pipeline - mvn command doesn't read withCredentials variables properly


I am trying to use jgitflow plugin for maven and run a release via Jenkins pipeline.

Plugin configuration:

<plugin>
     <groupId>external.atlassian.jgitflow</groupId>
     <artifactId>jgitflow-maven-plugin</artifactId>
     <version>1.0-m5.1</version>
     <configuration>
        <username>${git.user}</username>
        <password>${git.password}</password>
        <enableSshAgent>true</enableSshAgent>
        <autoVersionSubmodules>true</autoVersionSubmodules>
        <noDeploy>true</noDeploy>
        <releaseBranchVersionSuffix>-RELEASE</releaseBranchVersionSuffix>
     </configuration>
</plugin>

The problem is when I am passing a credentials for jgitflow.

withCredentials([usernamePassword(credentialsId: 'my_credentials_id', passwordVariable: 'USERNAME', usernameVariable: 'PASSWORD')]) {
    sh "git checkout develop"
    sh "mvn -f jgitflow:release-start -B -U -DskipTests -DnoDeploy=true -DpushReleases=false -Dgit.user=$USERNAME -Dgit.password=$PASSWORD"
    sh "mvn -f jgitflow:release-finish -B -U -Dmaven.javadoc.skip=true -DskipTests -DnoDeploy=true -DpushReleases=true -Dgit.user=$USERNAME -Dgit.password=$PASSWORD"
 }

Settings above don't work, but everything is fine if I pass username and password explicitly instead of by variable. Am I using it wrong?


Solution

  • In some cases, the output generated by the pipeline script is tricky cannot be used as it is. Use following code to access the credentials:

    withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: '<CREDENTIAL_ID>', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']])
    

    instead of generated code.