我该如何使用Maven发布插件将代码发布到Github?

3

我能够使用SSH成功地推送到我的Github代码库,但是当我尝试在Maven发布插件的准备阶段进行推送时,出现了错误:

Failed to execute goal org.apache.maven.plugins:maven-release-plugin:3.0.0-M1:prepare (default-cli) on project netbeans-visual-diff-standalone: Unable to commit file

Caused by: org.apache.maven.shared.release.scm.ReleaseScmCommandException: Unable to commit files
Provider message:
The git-push command failed.
Command output:
nbauma109@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

我按照以下步骤创建了公私钥,将公钥上传到我的github账户,并将私钥添加到ssh代理中:

https://docs.github.com/en/github/authenticating-to-github/adding-a-new-ssh-key-to-your-github-account

我更新了maven POM的发布插件配置和scm连接设置:
<scm>
    <connection>scm:git:https://github.com/nbauma109/netbeans-visual-diff-standalone.git</connection>
    <developerConnection>scm:git:ssh://git@github.com/nbauma109/netbeans-visual-diff-standalone.git</developerConnection>
    <url>https://github.com/nbauma109/netbeans-visual-diff-standalone</url>
    <tag>HEAD</tag>
</scm>
<distributionManagement>
   <repository>
     <id>pkg.github.com</id>
     <name>GitHub nbauma109 Apache Maven Packages</name>
     <url>https://maven.pkg.github.com/nbauma109/mvn-repo</url>
   </repository>
</distributionManagement>
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-release-plugin</artifactId>
      <version>3.0.0-M1</version>
      <configuration>
        <tagBase>ssh://git@github.com/nbauma109/netbeans-visual-diff-standalone/tags</tagBase>
      </configuration>
    </plugin>
  </plugins>
</build>
<properties>
    <project.scm.id>github.com</project.scm.id>
</properties>

在MAVEN用户的~/.m2/settings.xml文件中,我包含了SSH密钥信息,包括私钥位置、密码和将公钥内容粘贴在“contents”标签内:
  <servers>
    <server>
        <id>github.com</id>
        <username>git</username>
        <privateKey>${user.home}/.ssh/id_ed25519</privateKey>
        <passphrase>censored</passphrase>
        <filePermissions>664</filePermissions>
        <directoryPermissions>775</directoryPermissions>
        <configuration>
          <knownHostsProvider implementation="org.apache.maven.wagon.providers.ssh.knownhost.SingleKnownHostProvider">
            <hostKeyChecking>yes</hostKeyChecking>
            <contents>ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDqdqJcRAZvJvTVWRXBlFB/c+w8pZPFRoWNXKFp6CSTV nbauma109@github.com</contents>
          </knownHostsProvider>
        </configuration>
    </server>
    <server>
        <id>pkg.github.com</id>
        <username>nbauma109</username>
        <password>github_personal_token</password>
    </server>
  </servers>

在known_hosts文件中,我添加了本地主机和我的IP地址。 我在~/.ssh目录下有3个文件:
  • id_ed25519(私钥)
  • id_ed25519.pub(公钥)
  • known_hosts
在将我的密钥添加到ssh代理后,我使用以下命令在git-bash中启动插件:ssh-add ~/.ssh/id_ed25519。
mvn -X -Dusername=git release:clean release:prepare release:perform

当我被提示输入版本和标签信息时,我只需按Enter键选择默认值。
- 发布版本:2.0.1 - 下一个开发版本:2.0.2-SNAPSHOT
另外,我看到了一些类似的帖子(如何解决使用Maven发布时出现的“权限被拒绝(publickey)”问题),其中OP说他通过将公钥添加到POM文件中的developerConnection/push部分来解决了该问题,但我从未见过这样的做法,也不认为那是应该放置公钥的地方。
编辑(在@VonC的回答后):在从nbauma109更改用户并更新POM和maven settings.xml之后,现在我有了两个仓库,github.com使用私钥身份验证,pkg.github.com使用个人令牌身份验证。现在我可以将标签推送到github.com,同时我可以将包推送到Github packages(pkg.github.com),前提是我的个人令牌github_personal_token具有“写入包”权限。
现在我的问题是,我已经推送了一个没有进行身份认证就无法访问的包,并且我的发布标签为空,其他项目无法使用二进制文件。难道我没有其他选项,只能通过Github网页界面手动重新上传我的二进制文件到发布标签吗?
1个回答

2
只要你看到 nbauma109@github.com,你就可以确定无论是从命令行还是 Maven 中进行 SSH push 都会失败。
GitHub 的 SSH URL 总是使用远程用户“git”: git@github.com:...,而不是实际的 GitHub 用户帐户名:你注册到该 GitHub 帐户的公钥应该用于身份验证。
因此,可以从以下开始测试:
<username>git</username>

根据 评论,由 OP Sybuser 提供的解决方案是:

我有一个不需要使用 GitHub Packages 的解决方法,即:


好的,现在它成功推送了一个标签,但是现在卡在了向 GitHub packages 推送(没有错误)。我会相应地更新我的问题。 - undefined
1
@Sybuser 这完全是一个独立的问题。 - undefined
@Sybuser你解决了这个问题吗?我也遇到了类似的困境。 - undefined
我有一个解决方案,不需要使用GitHub Packages。可以通过配置Maven Release插件的<goals>install</goals>来实现,并使用GitHub Action进行Maven发布(例如qcastel),以及使用GitHub Action进行GitHub发布(例如marvinpinto)。如果你仍然想使用GitHub Packages,抱歉,我无法帮助你。 - undefined
1
@Sybuser感谢您的反馈。我已将您的评论包含在答案中以增加可见性,并附加了其他链接。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接