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

GitHub Actions推送代码遇认证错误,请求解决推送失败问题

解决GitHub Actions推送代码时的"fatal: could not read Password"错误

推送代码时出现错误:fatal: could not read Password for 'https://***@github.com': No such file or directory,结合你提供的YAML配置,问题出在几个不合理的配置和命令上,以下是修复方案:

问题分析

  1. Git并没有user.password这个全局配置项,你添加的git config --global user.password完全无效,反而可能干扰Git的认证流程。
  2. 原配置中部分命令的写法在Windows环境下存在路径和变量解析的潜在问题。
  3. 没有处理空提交的情况,当构建产物无变更时,commit操作会失败。

修复后的完整YAML配置

jobs:
  build:
    name: Build
    runs-on: windows-latest
    steps:
    - name: Checkout Code
      uses: actions/checkout@v3
      with:
        fetch-depth: 0 # 获取完整提交历史,避免提交时的权限或历史问题
    - name: Install Node.js, NPM and Yarn
      uses: actions/setup-node@v3
      with:
          node-version: 12.18.3
          cache: 'npm' # 缓存npm依赖,加快构建速度
    - name: Install Dependencies
      run: npm i
    - name: Hexo Build
      run: |
            npm install hexo-cli -g
            hexo g
    - name: Push Build
      env:
        GH_TOKEN: ${{ secrets.GH_TOKEN }}
        GH_REF: ${{ secrets.GH_REF }}
        P_BRANCH: ${{ secrets.P_BRANCH }}
        U_NAME: ${{ secrets.U_NAME }}
        U_EMAIL: ${{ secrets.U_EMAIL }}
      run: |
            git config --global user.name "$U_NAME"
            git config --global user.email "$U_EMAIL"
            # 克隆目标仓库到指定目录
            git clone "https://$GH_TOKEN@$GH_REF" ./deploy-repo
            # 复制Hexo构建产物到克隆的仓库
            Copy-Item "public\*" "./deploy-repo/" -Recurse -Force
            cd ./deploy-repo
            # 检查是否有文件变更,避免空提交导致失败
            if (git status --porcelain) {
              git add .
              git commit -m 'GitHub Actions auto build yolk-blog'
              git push origin "$P_BRANCH"
            } else {
              echo "无变更内容,跳过推送步骤。"
            }

关键修改说明

  • 更新Action版本:将actions/checkout和actions/setup-node升级到v3,提升兼容性和安全性,同时添加缓存配置加快依赖安装速度。
  • 移除无效配置:删除了git config --global user.password,Git不需要这个配置项,HTTPS方式的认证已经通过URL中的GH_TOKEN完成。
  • 优化命令逻辑:
    • 使用env块统一管理环境变量,让代码更易读和维护。
    • 添加空提交检查,当构建产物没有变化时直接跳过推送,避免不必要的错误。
    • 明确克隆目录的路径,避免Windows环境下的路径解析问题。
  • Token权限验证:确保你在仓库Secrets中配置的GH_TOKEN是拥有仓库写入权限的Personal Access Token,创建Token时需要勾选repo相关权限(包括public_repo或repo,根据你的仓库类型选择)。

内容的提问来源于stack exchange,提问作者黄天润

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 11:56:34