Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mvn release to push to git using a specific private key

Tags:

git

ssh

maven

I hope someone can help with this. I'm trying to configure the mvn release plugin in pom.xml so that the updated pom version and tag are pushed to the git repo as part of release:prepare. Crucially, it needs to use the ssh private key of a specific user, as ultimately this will be part of our CI stack.

In the pom.xml I currently have a very simple config for the release plugin:

<build>
    <plugins>
        <!-- release plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-release-plugin</artifactId>
            <version>2.5.3</version>
        </plugin>

The scm settings in the pom look like this:

<scm>
    <connection>scm:git:git://[email protected]/**account**/**project**.git</connection>
    <developerConnection>scm:git:ssh://[email protected]/**account**/**project**.git</developerConnection>
    <url>https://bitbucket.org/**account**/**project**</url>
</scm>

And I have the following property in the pom (though not sure if this is used or not - I found reference to it on a loosely related problem):

<properties>
    <project.scm.id>bitbucket.org</project.scm.id>

Lastly, I have the following in mvn's settings.xml :

<servers>
    <server>
        <id>bitbucket.org</id>
        <privateKey>~/.ssh/bitbucket-read-write-access</privateKey>
        <passphrase></passphrase>
    </server>

The private key file ~/.ssh/bitbucket-read-write-access exists, and there are no other keys in that folder (I have deliberately removed the default id_rsa)

When I run mvn release:perform, it fails when it tries to push to the repo:

[INFO] Executing: /bin/sh -c cd /home/nathanrussell/projects/**project** && git push ssh:********@bitbucket.org/**account**/**project**.git refs/heads/master:refs/heads/master
[INFO] Working directory: /home/nathanrussell/projects/**project**
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 13.162 s
[INFO] Finished at: 2018-11-09T15:25:36Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-release-plugin:2.5.3:prepare (default-cli) on project **project**: Unable to commit files
[ERROR] Provider message:
[ERROR] The git-push command failed.
[ERROR] Command output:
[ERROR] [email protected]: Permission denied (publickey).
[ERROR] fatal: Could not read from remote repository.
[ERROR] 
[ERROR] Please make sure you have the correct access rights
[ERROR] and the repository exists.

The only way I can get it to push correctly is to rename the file ~/.ssh/bitbucket-read-write-access to ~/.ssh/id_rsa which leads me to believe that:

  • The user associated with the private key has the correct permissions on the repo and the public key is correctly associated with the repo user
  • My configuration of the pom.xml and/or settings.xml is not quite right in respect of choosing/using the desired private key

(Before anyone suggests it, I cannot simply rename the key to ~/.ssh/id_rsa because when we run this on the CI stack, it already has a default ssh key for another purpose)

Any thoughts or help with this would be very much appreciated


Some additional info:
If I do export GIT_SSH_COMMAND="ssh -i ~/.ssh/bitbucket-read-write-access" then git push, it pushes fine, which further leads me to believe the private/public key are correctly configured; and it's the mvn config that is wrong.

like image 595
Nathan Russell Avatar asked Nov 09 '18 16:11

Nathan Russell


1 Answers

To get this working, I used a variant of the GIT_SSH_COMMAND approach that I wrote about in the original question.

Whilst GIT_SSH_COMMAND worked on my development machine, the version of git on our CI stack is quite old (1.7.1) and GIT_SSH_COMMAND is not supported! (GIT_SSH_COMMAND was introduced in 2.10)

I got this working with a combination of the GIT_SSH environment variable, and a shell script:

export GIT_SSH=/var/home/teamcity/.ssh/ssh-using-bitbucket-read-write-access.sh

and

$ cat /var/home/teamcity/.ssh/ssh-using-bitbucket-read-write-access.sh
#!/bin/bash

ssh -i ~/.ssh/bitbucket-read-write-access $*


It feels a bit of a hack, but it works.

like image 131
Nathan Russell Avatar answered Oct 13 '22 00:10

Nathan Russell