You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GitHub Actions中maven-release-plugin执行release:perform时Git认证失败

解决release:perform的Git认证问题

问题根源

release:perform执行时,maven-release-plugin会基于pom.xml里的SCM配置克隆代码仓库,但默认的HTTPS URL没有携带GitHub认证凭证,导致克隆时无法通过身份验证。

解决方案

方案1:配置Git Credential Helper自动注入凭证

在workflow的Configure git user步骤后,添加Git凭证配置,让Git在克隆时自动使用GITHUB_TOKEN:

- name: Configure git credential helper
  run: |
    git config --global credential.helper 'store --file ~/.git-credentials'
    echo "https://${GITHUB_TOKEN}:x-oauth-basic@github.com" > ~/.git-credentials

这样Git在克隆HTTPS URL时会自动读取凭证,无需手动输入用户名密码。

方案2:修改pom.xml的SCM连接为带认证的URL

在pom.xml的SCM配置中,将connection和developerConnection修改为包含GITHUB_TOKEN的动态URL:

<scm>
  <connection>scm:git:https://${env.GITHUB_TOKEN}@github.com/mycompany/myproject.git</connection>
  <developerConnection>scm:git:https://${env.GITHUB_TOKEN}@github.com/mycompany/myproject.git</developerConnection>
  <url>https://github.com/mycompany/myproject</url>
  <tag>HEAD</tag>
</scm>

注:你的workflow已在job级env中设置GITHUB_TOKEN,此配置可直接生效。

方案3:在release:perform命令中指定SCM凭证参数

直接在maven命令里传入SCM的认证参数,用GITHUB_TOKEN作为密码:

- name: Maven release:perform
  run: mvn --batch-mode -DdryRun=${{ env.PUBLISH_TO == 'none (dry run)' }} -Dusername=${GITHUB_TOKEN} -Dpassword=x-oauth-basic release:perform
  shell: bash
  env:
    MAVEN_USERNAME: ${{ secrets.MAVEN_CENTRAL_USERNAME }}
    MAVEN_PASSWORD: ${{ secrets.MAVEN_CENTRAL_PASSWORD }}
    GPG_PASSPHRASE: ${{ secrets.PGP_PASSPHRASE }}

其中x-oauth-basic是GitHub OAuth认证的固定用户名,密码使用GITHUB_TOKEN即可。

方案4:升级maven-release-plugin版本

你当前使用的2.5.3版本较旧,可能存在SCM认证兼容性问题,建议升级到最新稳定版(如3.0.1):
在pom.xml的build/plugins段更新插件版本:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-release-plugin</artifactId>
  <version>3.0.1</version>
</plugin>

新版本对Git认证的支持更完善,可自动适配GitHub Actions环境。

推荐组合

优先使用方案1+方案4,既通过凭证助手确保克隆成功,又升级插件避免旧版本的潜在问题。

内容的提问来源于stack exchange,提问作者meva

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 23:55:22