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

Ant构建因Node/npm的Git公钥权限问题失败,求解决方案

Git公钥权限导致Ant构建resolve-npm目标失败的解决建议

问题背景

Ant执行resolve-npm目标时失败,核心错误为git@mygithub-site.com: Permission denied (publickey),已更新GitHub站点公钥但问题仍存在。相关信息如下:

Ant目标代码

<!-- Task to Install the required node modules -->
<target name="resolve-npm" depends="-detect-resolve-npm-required, require-node" unless="node_modules.uptodate">
    <echo>Installing the node modules specified in package.json</echo>
    <exec dir="." executable="${npm}" logError="yes" failonerror="true">
        <arg value="install" if: set="isDev"/>
        <arg value="clean-install" unless:set="isDev"/>
    </exec>
    <touch file="node_modules/node_modules.uptodate"/>
    <property name="node_modules.uptodate" value="true"/>
</target>

错误日志核心片段

[exec]      [exec] npm error command git --no-replace-objects ls-remote git@mygithub-site.com:urbancode/launch-api-spec-converter.git
 [exec]      [exec] npm error git@mygithub-site.com: Permission denied (publickey).

Node与npm版本

> node -v
v21.2.0

> npm -version
10.6.0

解决建议

  • 验证构建环境的SSH密钥可用性
    直接在构建机器上执行ssh -T git@mygithub-site.com,确认是否能成功认证。如果失败:

    • 检查构建用户的~/.ssh目录权限,必须为700,私钥文件(如id_rsa)权限为600,权限过宽会被SSH拒绝。
    • 确认构建使用的用户和手动操作的用户是否一致,不同用户的SSH配置目录相互独立。
  • 排查npm的Git访问方式

    • 临时切换依赖URL为HTTPS测试:修改package.json中对应依赖的URL为https://mygithub-site.com/urbancode/launch-api-spec-converter.git,执行npm install,如果成功则说明问题出在SSH认证环节。
    • 执行npm config get git,确保npm使用系统默认Git,避免自定义Git路径导致密钥加载异常。
  • 确保SSH Agent正常运行

    • 本地构建时,启动SSH Agent并添加私钥:
      eval "$(ssh-agent -s)"
      ssh-add ~/.ssh/id_rsa
      
      完成后重新运行Ant构建。
    • CI/CD环境(如Jenkins)需确认SSH Agent插件已配置,并正确绑定对应私钥。
  • 重新核对密钥与仓库权限

    • 重新生成SSH密钥对,确保公钥完整复制到GitHub站点的SSH Keys设置中(不要遗漏末尾的用户名@主机名注释)。
    • 确认该SSH密钥绑定的GitHub账号拥有urbancode/launch-api-spec-converter.git仓库的访问权限。
  • 为Ant exec任务传递SSH环境变量
    Ant的exec任务可能丢失SSH相关环境变量,可显式传递:

    <exec dir="." executable="${npm}" logError="yes" failonerror="true">
        <env key="SSH_AUTH_SOCK" value="${env.SSH_AUTH_SOCK}"/>
        <arg value="install" if: set="isDev"/>
        <arg value="clean-install" unless:set="isDev"/>
    </exec>
    

内容的提问来源于stack exchange,提问作者Dev Anand Sadasivam

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 00:06:00