Azure DevOps Pipelines编译构建把构建文件提交(Push)到git提示权限错误问题解决

发布时间 2023-06-14 10:13:46作者: 罗曼蒂克'

错误日志

remote: 001f# service=git-receive-pack
remote: 0000000000aaTF401027: You need the Git 'GenericContribute' permission to perform this action. Details: identity 'Build\8b517d3f-052b-41e0-a7f6-e811999734c0', scope 'repository'.
remote: TF401027: You need the Git 'GenericContribute' permission to perform this action. Details: identity 'Build\8b517d3f-052b-41e0-a7f6-e811999734c0', scope 'repository'.
fatal: unable to access 'https://dev.azure.com/zuzhi/China-iTwin/_git/testforYanjun/': The requested URL returned error: 403

 

错误如下图:

 

解决:

Azure DevOps Services |Azure DevOps Server 2022 - Azure DevOps Server 2019 |TFS 2018

对于某些工作流,需要生成管道才能运行 Git 命令。 例如,在功能分支上生成 CI 后,团队可能需要将分支合并到主分支。

Git 适用于 Microsoft 托管的代理 和 本地代理

启用脚本以运行 Git 命令

 备注

在开始之前,请确保帐户的默认标识已使用以下代码进行设置。 在签出代码后,必须首先完成此操作。

git config --global user.email "you@example.com"
git config --global user.name "Your Name"

向生成服务授予版本控制权限

  1. 在 “组织设置>常规>项目”中转到组织的项目设置页面。

    选择组织设置。

  2. 选择要编辑的项目。

    选择项目。

  3. 在 “项目设置”中,选择 “存储库”。 选择要在其中运行 Git 命令的存储库。

  4. 选择 “安全性 ”以编辑存储库安全性。

    选择“安全性”以编辑存储库安全性。

  5. 搜索 项目集合生成服务。 选择标识 项目集合生成服务 ({你的组织}) (不是组 Project Collection Build Service Accounts ({your organization}) ) 。 默认情况下,此标识可以从存储库读取,但无法将任何更改推送回存储库。 授予要运行的 Git 命令所需的权限。 通常,你需要授予:

    • 创建分支: 允许
    • 贡献: 允许
    • 读: 允许
    • 创建标记: 允许

允许脚本访问系统令牌

添加一个 checkout 设置为 persistCredentialstrue.

YAML
steps:
- checkout: self
  persistCredentials: true

了解详细信息 checkout

确保清理本地存储库

生成管道不会自动清理对本地存储库的某些类型的更改。 因此,请确保:

  • 删除创建的本地分支。
  • 撤消 git 配置更改。

如果使用本地代理遇到问题,请确保存储库干净:

请确保 checkout 已 clean 设置为 true.

YAML
steps:
- checkout: self
  clean: true

 

 

 

参考链接:Run Git commands in a script - Azure Pipelines | Microsoft Learn