在 Azure Pipelines 构建中使用 Azure Repos git 模块源进行身份验证

2024-04-26

我目前正在为 Azure DevOps 创建一个管道,以验证 Terraform 配置并将其应用到不同的订阅。

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

可悲的是,当我尝试表演时terraform init为了获取这些模块,管道任务“挂”在那里等待凭据输入。

正如推荐的我尝试添加一个checkout步与persistCredentials:true属性。

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

添加时执行的命令persistCredentials:true

2018-10-22T14:06:54.4347764Z ##[command]git config http.https://[email protected] /cdn-cgi/l/email-protection/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://my-org@dev.azure.com/my-org/my-project/_git/my-module-repo?ref=1.0.2"

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


我遇到了同样的问题,我最终所做的是标记化SYSTEM_ACCESSTOKEN在地形配置中。我在 Azure DevOps 中使用了 Tokenzization 任务,其中__前缀和后缀用于识别标记并将其替换为实际变量(它是可定制的,但我发现双下划线最适合不干扰我拥有的任何代码)

- task: qetza.replacetokens.replacetokens-task.replacetokens@3
    displayName: 'Replace tokens'
    inputs:
      targetFiles: |
       **/*.tfvars
       **/*.tf
      tokenPrefix: '__'
      tokenSuffix: '__'

就像是find $(Build.SourcesDirectory)/ -type f -name 'main.tf' -exec sed -i 's~__SYSTEM_ACCESSTOKEN__~$(System.AccessToken)~g' {} \;如果您无法将自定义扩展安装到您的 DevOps 组织,那么也可以使用。

我的 terraform main.tf 如下所示:

module "app" {
  source = "git::https://token:[email protected] /cdn-cgi/l/email-protection/actualOrgName/actualProjectName/_git/TerraformModules//azure/app-service?ref=__app-service-module-ver__"
  ....
}

它并不漂亮,但它可以完成工作。模块源(在撰写本文时)不支持来自 terraform 的变量输入。因此,我们可以做的是使用 Terrafile,它是一个开源项目,通过在代码旁边保留一个简单的 YAML 文件,帮助您跟上可能使用的模块和同一模块的不同版本。它似乎不再被积极维护,但它仍然有效:https://github.com/coretech/terrafile https://github.com/coretech/terrafile我的 Terrafile 示例:

app:
    source:  "https://token:[email protected] /cdn-cgi/l/email-protection/actualOrgName/actualProjectName/_git/TerraformModules"
    version: "feature/handle-twitter"
app-stable:
    source:  "https://token:[email protected] /cdn-cgi/l/email-protection/actualOrgName/actualProjectName/_git/TerraformModules"
    version: "1.0.5"

Terrafile 默认情况下将模块下载到 ./vendor 目录,以便您可以将模块源指向如下所示:

module "app" {
  source = "./vendor/modules/app-stable/azure/app_service"
  ....
}

现在你只需要弄清楚如何执行terrafileTerrafile 所在目录中的命令。 我的 azure.pipelines.yml 示例:

- script: curl -L https://github.com/coretech/terrafile/releases/download/v0.6/terrafile_0.6_Linux_x86_64.tar.gz | tar xz -C $(Agent.ToolsDirectory)
  displayName: Install Terrafile

- script: |
    cd $(Build.Repository.LocalPath)
    $(Agent.ToolsDirectory)/terrafile
  displayName: Download required modules
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Azure Pipelines 构建中使用 Azure Repos git 模块源进行身份验证 的相关文章

随机推荐