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

GitHub Workflows中Personal Token失效,无法推送代码至部署仓库

解决GitHub Action推送.github.io仓库时的"could not read Password"错误

问题场景

拥有两个GitHub仓库:主代码仓库和<name>.github.io部署仓库,此前配置的Workflow可将主代码构建产物推送到部署仓库的GitHub Pages分支,2-3个月前突然失效,报错如下:

fatal: could not read Password for 'https://***@github.com': No such device or address
Error: Process completed with exit code 128.

对应的Workflow片段(最后一行报错):

git init
git config --local user.name "github-actions[bot]"
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git remote add origin "https://${{ secrets.AUTH_TOKEN_FOR_DEPLOYMENT }}@github.com/<name>/<name>.github.io.git"

已尝试刷新Token、使用ad-m/github-push-action(仅强制推送有效)、谷歌搜索及Reddit求助,均未解决。

有效解决方案

1. 禁用Git凭证助手

Action环境中默认的Git凭证助手可能干扰Token认证,在git init后添加以下命令,强制Git直接使用URL中的Token:

git init
git config --local credential.helper ""  # 添加这行
git config --local user.name "github-actions[bot]"
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git remote add origin "https://${{ secrets.AUTH_TOKEN_FOR_DEPLOYMENT }}@github.com/<name>/<name>.github.io.git"

2. 改用SSH协议推送

HTTPS认证在部分Action环境中易出问题,换成SSH方式更稳定:

  • 步骤1:本地生成SSH密钥对:
    ssh-keygen -t ed25519 -C "github-actions[bot]@users.noreply.github.com" -f deploy_key
    
  • 步骤2:在<name>.github.io仓库的「Settings → Deploy keys」中添加公钥(deploy_key.pub内容),勾选Allow write access。
  • 步骤3:将私钥(deploy_key内容)添加到主代码仓库的「Settings → Secrets and variables → Actions → Secrets」,命名为DEPLOY_SSH_KEY。
  • 步骤4:修改Workflow,先加载SSH密钥再配置remote:
    - name: Setup SSH Agent
      uses: webfactory/ssh-agent@v0.7.0
      with:
        ssh-private-key: ${{ secrets.DEPLOY_SSH_KEY }}
    
    - name: Configure Git and Push
      run: |
        git init
        git config --local user.name "github-actions[bot]"
        git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
        git remote add origin git@github.com:<name>/<name>.github.io.git
        # 后续add/commit/push命令不变
    

3. 检查Token权限与URL编码

  • 确保AUTH_TOKEN_FOR_DEPLOYMENT是Fine-grained个人访问令牌,至少拥有部署仓库的「Contents → Read and write」权限,且未过期。
  • 如果Token包含@、:等特殊字符,需对其进行URL编码(如@替换为%40,:替换为%3A),避免URL解析错误。

4. 使用官方actions/checkout优化环境

在Workflow开头添加官方checkout action,确保Git环境配置正确:

- name: Checkout code
  uses: actions/checkout@v4
  with:
    persist-credentials: false  # 避免默认凭证干扰

内容的提问来源于stack exchange,提问作者Alpha Wolf Gamer

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 19:58:08