代码之家  ›  专栏  ›  技术社区  ›  realmikep

`Github Actions工作流中的git commit`失败:未授予对存储库的写入访问权限,403

  •  1
  • realmikep  · 技术社区  · 8 月前

    我有一个。NET核心API项目,并使用Github工作流进行构建和部署。我正在使用Github操作,在每次提交到.csproj时,都会在.csproj中添加版本 dev 支。到目前为止,它工作得很好。

    当我尝试提交更新的.csproj文件时,我得到了错误:

    remote: Write access to repository not granted.
    fatal: unable access 'https:/github/<repo>': The requested URL returned error: 403
    

    Github Action Log

    我已确认仓库的工作流权限为“读写权限”:

    Workflow settings for repo

    这是工作流yaml:

    jobs:
      build:
        runs-on: windows-latest
        permissions:
          contents: read #This is required for actions/checkout
    
        steps:
          - name: 'Support longpaths'
            run: git config --system core.longpaths true
        
          - uses: actions/checkout@v4
    
          - name: Set up .NET Core
            uses: actions/setup-dotnet@v4
            with:
              dotnet-version: '8.x'
    
          - name:  Bump MyProject.Api version
            id: update
            uses: vers-one/[email protected]
            with:
              file: "./server/MyProject.Api/MyProject.Api.csproj"
              version: bump-build
    
    ### THE ERROR IS FROM THIS COMMIT ###
          - run: |
              git config user.name github-actions
              git config user.email [email protected]
              git add "./server/MyProject.Api/MyProject.Api.csproj"
              git commit -m "Update project versions to ${{ steps.update.outputs.newVersion }}"
              git push
    
          - name: Build with dotnet
            run: dotnet build --configuration Development
            working-directory: ./server
    

    此工作流在仓库所有者帐户中运行,因此这不是协作者权限问题。

    我找了又找,尝试了十几条建议,但我已经无计可施了。这似乎是一项简单的任务,花了我一整天的时间,但仍然没有奏效。

    你知道为什么这行不通吗?我该怎么解决?

    非常感谢。

    1 回复  |  直到 8 月前
        1
  •  1
  •   Szymon Maszke    8 月前

    您工作流中的权限比全局权限更严格,即这部分:

    jobs:
      build:
        runs-on: windows-latest
        permissions:
          contents: read #This is required for actions/checkout
    

    您可以删除 permissions 第节(未修订)或更改为 contents: write (如 commit 正在写入您的存储库)。阅读更多关于权限的信息 here