在Azure Pipelines版本中对Azure Repos git模块源进行身份验证

问题描述 投票:1回答:2

我当前正在为Azure DevOps创建管道,以验证Terraform配置并将其应用于其他订阅。

我的terraform配置使用模块,这些模块与terraform配置“托管”在同一Azure DevOps项目的其他存储库中。

[遗憾的是,当我尝试执行terraform init来获取那些模块时,管道任务“挂起”在那里,等待凭据输入。

按照Pipeline Documentation on Running Git Commands in a script的要求,我尝试使用checkout属性添加persistCredentials:true步骤。

从我在任务日志中看到的信息(请参见下面的信息),凭据信息专门添加到当前存储库中,不能用于其他存储库。

添加persistCredentials:true时执行的命令

2018-10-22T14:06:54.4347764Z ##[command]git config http.https://[email protected]/my-org/my-project/_git/my-repo.extraheader "AUTHORIZATION: bearer ***"

terraform init任务的输出

2018-10-22T14:09:24.1711473Z terraform init -input=false
2018-10-22T14:09:24.2761016Z Initializing modules...
2018-10-22T14:09:24.2783199Z - module.my-module
2018-10-22T14:09:24.2786455Z   Getting source "git::https://[email protected]/my-org/my-project/_git/my-module-repo?ref=1.0.2"

我如何设置git凭据以用于其他存储库?

azure-devops terraform azure-pipelines
2个回答
0
投票

我认为您不能。通常,您创建另一个构建,并链接到该构建中的工件,以在当前定义中使用它。这样,您无需连接到其他Git存储库


0
投票

问题的关键在于,下载模块的Terraform git操作未在管道正在构建的存储库的上下文中发生。为了使Terraform git操作通过Azure Repos进行身份验证,您必须在全局extraheader中使用管道的访问令牌显式设置一个extraheader

尽管令牌仅在构建期间有效,但我不希望.gitconfig配置污染(本地)构建代理用户的全局extraheader,并可能影响后续构建。为了使配置瞬态,我在.gitconfig中创建了一个临时.gitconfig(在构建结束时由代理清理),然后使用全局$env:AGENT_TEMPDIRECTORY中的[include]对其进行引用。 Terraform操作完成后,此[include]不会被设置。

在Terraform操作之前,我在管道中具有以下PowerShell任务:

.gitconfig

然后执行Terraform操作后,另一个PowerShell步骤将include取消设置:

# Build a temp .gitconfig 
$tempGitConfig = Join-Path $env:AGENT_TEMPDIRECTORY ".gitconfig.$($env:BUILD_BUILDID)"

# Set an extraheader for the module sources that uses the pipeline's access token
git config --file $tempGitConfig `
             http.https://my.azd.com/project.extraheader `
             "AUTHORIZATION: bearer $($env:SYSTEM_ACCESSTOKEN)"

# Include temp .gitconfig in global user .gitconfig
git config --global include.path $tempGitConfig

如果您重复执行此操作,那么此[[可以全部打包到一个自定义构建任务中。

© www.soinside.com 2019 - 2024. All rights reserved.