6

From Jenkins I'm trying to do a maven release, with the code being hosted on github, on a repo. For the build user I generated an OAuth token to access the repo in RW mode.

In Jenkins I configured the repository checkout url like https://token@github.com/username/project without any credentials, as the token in front should be enough.

In my pom, for the I did set any username/password, neither the token. The value is simply:

<developerConnection>scm:git:https://github.com/username/project</developerConnection>

But when maven is trying to push the commits on the pom files, I got an error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-release-plugin:2.5.1:prepare (default-cli) on project cloudstack: Unable to commit files
[ERROR] Provider message:
[ERROR] The git-push command failed.
[ERROR] Command output:
[ERROR] fatal: could not read Username for 'https://github.com': No such device or address
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-release-plugin:2.5.1:prepare (default-cli) on project cloudstack:     Unable to commit files
Provider message:
The git-push command failed.
Command output:
fatal: could not read Username for 'https://github.com': No such device or address

    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:213)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)

Any idea how to fix that?

marcaurele
  • 502
  • 1
  • 6
  • 14
  • I fixed the issue with the creation of an account for Jenkins which has access to the repository thanks to its SSH key. – marcaurele Feb 04 '18 at 20:55

5 Answers5

8

I've managed to solve this for the maven release plugin, with help from the Jenkins Credentials Binding Plugin. Note that this solution isn't needed for ssh authentication. Here's the SCM section in question:

<scm>
    <connection>scm:git:http://${env.GIT_USERNAME}:${env.GIT_PASSWORD}@server.host.name/path/to/project.git</connection>
    <url>http://server.host.name/path/to/project.git</url>
    <tag>HEAD</tag>
</scm>

Then, I use the with credentials plugin, using the following in a Jenkins Pipeline script:

withCredentials([[$class: 'UsernamePasswordMultiBinding', 
    credentialsId: 'id-of-credentials-from-those-set-up-in-Manage-Jenkins', 
    usernameVariable: 'GIT_USERNAME',
    passwordVariable: 'GIT_PASSWORD'
]]) {
        performRelease()
}

Note that the env.GIT_USERNAME and the variable set up in withCredentials are the same, and that is no accident.

I must apologies though, as this solutions assumes familiarity with the Jenkins Pipeline Script.

You may be able to adapt this by setting the git credentials in the environment before running your own custom maven release plugin script.

user3596523
  • 96
  • 1
  • 3
  • I had the same problem with my gitlab pipeline and solved it using the ${env.GIT_USERNAME} and ${env.GIT_PASSWORD} that I configured via Settings -> CI/CD -> Variables – René Link Jan 03 '21 at 14:35
6

You can configure the Maven Release plugin to not pull and push any changes:

...
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-release-plugin</artifactId>
    <version>2.5.3</version>
    <configuration>
        <localCheckout>true</localCheckout>
        <pushChanges>false</pushChanges>
    </configuration>
</plugin>
...

In Jenkins, you can use the Git Publisher post-build action to push your branch and the tag. The Git Publisher will use your OAuth credentials.

Pushing the tag is not so easy because you need to specify the exact tag name as generated by Maven, e.g. foo-2.0.1.

To parse the version from the generated POM, I use XMLStarlet in a "Execute Shell" build step after my Maven build step:

#!/usr/bin/env bash

VERSION=$(xmlstarlet sel -N x="http://maven.apache.org/POM/4.0.0" -t -v "x:project/x:version" target/checkout/pom.xml)

echo "RELEASE_VERSION=$VERSION" > version.properties

The script generates a properties file containing the release version. Use the "Inject environment variables" build step (EnvInject Plugin) to read properties file as build environment variables. Then you can specify the tag name in "Git Publisher" as foo-$RELEASE_VERSION.

This is not great, but it works. Maybe someone has a better (aka simpler) way of doing this.

daspilker
  • 8,154
  • 1
  • 35
  • 49
  • 1
    That's not something I willing to setup, it's over complicated. I'll have a look at the code in the plugin, and see if I could push a fix there if nothing simple is possible. – marcaurele Feb 19 '16 at 06:38
  • Hi,Did you fix this issue? Iam facing similar error for password in the scm connection i have mentioned as scm:git:https://uname@github.developer.io/company/core.git – Ravi Jan 10 '18 at 18:27
  • I've added a new account for Jenkins on github to push changes and authenticated with its private ssh key. – marcaurele Feb 04 '18 at 20:56
2

I was able to figure out another way of handling this. For this you will need


Once Config File Provider Plugin installed:

  • Define settings.xml under Manage Jenkins -> Managed files -> Add a new Config enter image description here

  • Add scm to pom.xml

    <scm>
        <connection>scm:git:https://github.com/user/project.git</connection>
        <developerConnection>scm:git:https://github.com/user/project.git</developerConnection>
        <url>https://github.com/user/project/${project.scm.tag}</url>
        <tag>HEAD</tag>
    </scm>
    
  • Add stage in Jenkinsfile

    stage("Maven release") {
        steps {
            configFileProvider([configFile(fileId: 'maven-settings', variable: 'MAVEN_SETTINGS')]) {
                sh "mvn -B clean release:clean"
                sh "mvn -B release:prepare -s ${env.MAVEN_SETTINGS}"
                sh "mvn -B release:perform -s ${env.MAVEN_SETTINGS}"
            }
        }
    }
    

Working source code example can be found under 'app-rest' project

Paweł Dulęba
  • 1,048
  • 1
  • 13
  • 25
  • The servers in the settings.xml is not only for the repositories and distributionManagement ? I can't find doc relating ir to the SCM server. it is relating the host name of the SCM connection with the ServerID ? – Cristiano Dec 23 '21 at 16:55
0

I just followed the suggestion from user3596523 by adding some variation. This are the steps I followed,

  1. As suggested, I added below line is pom.xml <scm> <connection>scm:git:http://${GIT_USERNAME}:${GIT_PASSWORD}@server.host.name/path/to/project.git</connection> <url>http://server.host.name/path/to/project.git</url> <tag>HEAD</tag> </scm>

  2. Since we can inject secrets into jenkin job (https://support.cloudbees.com/hc/en-us/articles/203802500-Injecting-Secrets-into-Jenkins-Build-Jobs), I have Injected GIT_USERNAME and GIT_PASSWORD using build bindings. Hope it can bypass creating pipeline scripts. Follow the above link to understand it in detail. enter image description here

Vimal VN
  • 26
  • 2
0

This is what worked for me

  • Create access token in gitlab
  • Create credential in Jenkins as username, password. Use a dummy username & token as the password
  • Use the below code block in your script

withCredentials([gitUsernamePassword(credentialsId: 'your-jenkins-token-name', gitToolName: 'Default')]) {

Imran AK
  • 103
  • 10