在Azure Pipelines中实现Git远程访问

69次阅读
没有评论

问题描述

在Azure Pipelines中的构建管道中,希望实现以下操作:
1. 默认分支为SIT(来自Azure Repos存储库)。
2. 当在DEV分支中进行更改时触发构建。
3. 在Powershell任务中执行以下操作:
– 将最新的更改从DEV合并到当前的SIT分支。
– 部署SIT分支中的最新更改。
– 将更新后的SIT分支推送到上游存储库。

用户的Powershell脚本如下:

git checkout -b temp
git merge origin/DEV
git branch -f SIT temp
git checkout SIT
git status
git branch -d temp
git remote add neworigin $(RemoteOrigin)
git push -u origin --all

在尝试使用git push -u origin SIT推送更新后的SIT分支时,遇到以下错误:

remote: 0000000000aaTF401027: You need the Git 'GenericContribute'  permission to perform this action. Details: identity  'Build\d073f6c7-4fa2-4bda-8694-8a41fb808fff', scope 'repository'.
remote: TF401027: You need the Git 'GenericContribute' permission to perform this action. Details: identity  'Build\d073f6c7-4fa2-4bda-8694-8a41fb808fff', scope 'repository'.
fatal: unable to access '': The requested URL returned  error: 403

用户请求帮助解决这个问题。

解决方案

请注意以下操作可能存在版本差异或权限问题,请谨慎操作并确保有相关权限。
用户遇到的问题是由于Azure Pipelines中的Powershell任务以Project Collection Build Service (yourProjectName)用户上下文运行,该用户默认没有权限对存储库进行贡献操作。
以下是解决该问题的步骤:
1. 首先,确保Azure Pipelines中的构建代理(Agent)的凭据拥有对目标存储库的贡献权限。这通常涉及到在Azure DevOps中配置代理的身份验证凭据,确保凭据具有足够的权限来执行git推送操作。
2. 在Powershell脚本中,我们需要将git操作封装在使用具有适当权限的用户凭据的上下文中。可以通过配置git凭据来实现这一点。以下是示例脚本的修改版,使用了git的凭据管理工具(git credential manager)来配置凭据,确保在推送时使用正确的凭据:

# 配置git凭据,确保推送时使用正确的凭据
git config --global credential.helper wincred

# 执行git操作
git checkout -b temp
git merge origin/DEV
git branch -f SIT temp
git checkout SIT
git status
git branch -d temp
git remote add neworigin $(RemoteOrigin)

# 使用配置的凭据推送更新后的SIT分支
git push -u origin SIT

这样,通过配置git凭据,确保Powershell脚本在推送操作时使用具有贡献权限的凭据,即可解决权限问题。

若你的Azure Pipelines代理运行在不同的操作系统上,你可能需要使用适合该操作系统的凭据管理工具,例如在Linux上使用Gnome Keyring或KWallet。

这样,用户在构建管道中的Powershell任务中推送更新后的SIT分支时,将使用配置的凭据,从而避免权限问题。

正文完