mvn release 使用特定的私钥推送到 git

Nat*_*ell 6 git ssh maven

我希望有人可以帮助解决这个问题。我正在尝试配置mvn release插件,pom.xml以便将更新的 pom 版本和标签作为release:prepare. 至关重要的是,它需要使用特定用户的 ssh 私钥,因为最终这将成为我们 CI 堆栈的一部分。

pom.xml我目前有一个非常简单的发布插件配置:

<build>
    <plugins>
        <!-- release plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-release-plugin</artifactId>
            <version>2.5.3</version>
        </plugin>
Run Code Online (Sandbox Code Playgroud)

scmpom 中的设置如下所示:

<scm>
    <connection>scm:git:git://ssh@bitbucket.org/**account**/**project**.git</connection>
    <developerConnection>scm:git:ssh://git@bitbucket.org/**account**/**project**.git</developerConnection>
    <url>https://bitbucket.org/**account**/**project**</url>
</scm>
Run Code Online (Sandbox Code Playgroud)

我在 pom 中有以下属性(虽然不确定是否使用了它 - 我在一个松散相关的问题上找到了它的参考):

<properties>
    <project.scm.id>bitbucket.org</project.scm.id>
Run Code Online (Sandbox Code Playgroud)

最后,我在 mvn 中有以下内容settings.xml

<servers>
    <server>
        <id>bitbucket.org</id>
        <privateKey>~/.ssh/bitbucket-read-write-access</privateKey>
        <passphrase></passphrase>
    </server>
Run Code Online (Sandbox Code Playgroud)

私钥文件~/.ssh/bitbucket-read-write-access存在,该文件夹中没有其他密钥(我特意去掉了默认的id_rsa

当我运行时mvn release:perform,它在尝试推送到存储库时失败:

[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] git@bitbucket.org: 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.
Run Code Online (Sandbox Code Playgroud)

我可以让它正确推送的唯一方法是重命名文件~/.ssh/bitbucket-read-write-access~/.ssh/id_rsa这让我相信:

  • 与私钥关联的用户对 repo 具有正确的权限,并且公钥与 repo 用户正确关联
  • 在选择/使用所需的私钥方面,我对pom.xml和/或的配置settings.xml不太正确

(在有人建议之前,我不能简单地将密钥重命名~/.ssh/id_rsa

对此有任何想法或帮助将不胜感激


一些额外的信息:
如果我做export GIT_SSH_COMMAND="ssh -i ~/.ssh/bitbucket-read-write-access"那么git push,它推动精细,更使我相信私有/公共密钥被正确配置它; 这是错误的 mvn 配置。

Nat*_*ell 3

为了实现这一目标,我使用了GIT_SSH_COMMAND我在原始问题中所写的方法的变体。

虽然GIT_SSH_COMMAND在我的开发机器上工作,但我们 CI 堆栈上的版本git相当旧(1.7.1)并且GIT_SSH_COMMAND不受支持!(GIT_SSH_COMMAND2.10中引入)

GIT_SSH我通过环境变量和 shell 脚本的组合来完成此工作:

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

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

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


Run Code Online (Sandbox Code Playgroud)

感觉有点像黑客,但它确实有效。