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

使用GitHub Actions自动化版本管理时遇RSA密钥权限错误

解决GitHub Actions Push无权限问题

你的工作流能正常识别版本并更新文件,但push失败是因为权限配置存在问题,以下是具体修复方案:

方案一:修复SSH配置

原步骤仅启动了SSH agent,但未将私钥添加到agent中,导致SSH认证失效。合并并修改相关步骤:

- name: Configure SSH and start agent
  run: |
    mkdir -p ~/.ssh
    echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
    chmod 600 ~/.ssh/id_rsa
    ssh-keyscan github.com >> ~/.ssh/known_hosts
    eval $(ssh-agent -s)
    ssh-add ~/.ssh/id_rsa
    git config --global user.email "jv0488598@gmail.com"
    git config --global user.name "joaovitorkc"

核心是添加ssh-add ~/.ssh/id_rsa命令,将私钥加载到agent中完成认证。

方案二:改用GitHub内置GITHUB_TOKEN(更推荐)

无需额外配置SSH密钥,直接使用GitHub Actions提供的GITHUB_TOKEN,修改后的完整工作流如下:

name: Update Version

on:
  push:
    branches:
      - main

jobs:
  update-version:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          persist-credentials: false # 禁用默认HTTPS凭证,避免干扰

      - name: Configure git user
        run: |
          git config --global user.email "jv0488598@gmail.com"
          git config --global user.name "joaovitorkc"

      - name: Install Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '14'

      - name: Get last commit message
        id: last_commit
        run: echo "::set-output name=message::$(git log -1 --pretty=%B)"

      - name: Extract version from commit message
        id: extract_version
        run: echo "::set-output name=version::$(echo ${{ steps.last_commit.outputs.message }} | grep -oP 'NEW_VERSION:\K\d+\.\d+\.\d+')"

      - name: Update version constant in code
        run: |
          if [ -n "${{ steps.extract_version.outputs.version }}" ]; then
            echo "export const VERSION = '${{ steps.extract_version.outputs.version }}';" > src/libs/version.ts
            echo "Version updated to ${{ steps.extract_version.outputs.version }}"
          else
            echo "No new version found in commit message."
            exit 0 # 无版本时直接退出,避免后续错误
          fi

      - name: Commit and push updated version
        run: |
          git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git
          git commit -am "Update version to ${{ steps.extract_version.outputs.version }}"
          git push origin main

关键修改说明:

  1. Checkout步骤添加persist-credentials: false,避免默认凭证干扰自定义认证
  2. 移除SSH相关配置,改用GITHUB_TOKEN设置远程仓库地址
  3. 无版本时增加exit 0,防止空版本触发无效commit

注意:若main分支开启了分支保护规则,需确保GITHUB_TOKEN拥有绕过规则的权限,或在规则中允许Actions推送代码。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 02:14:53