繁体   English   中英

使用 Azure Devops 管道运行代码时缺少 Terraform local_file

[英]Terraform local_file missing when running code using Azure Devops pipeline

当我使用 terraform 在Outputs.tf中创建“local_file”资源时:

### The hosts file
resource "local_file" "AnsibleHosts" {
 content = templatefile("${path.module}/hosts.tmpl",
   {
     vm-names                   = [for k, p in azurerm_virtual_machine.vm: p.name],
     private-ip                 = [for k, p in azurerm_network_interface.nic: p.private_ip_address],
     publicvm-names             = [for k, p in azurerm_virtual_machine.publicvm: p.name],
     publicvm-private-ip        = [for k, p in azurerm_network_interface.publicnic: p.private_ip_address],
     public-ip                  = [for k, p in azurerm_public_ip.publicip: p.ip_address],
     public-dns                 = [for k, p in azurerm_public_ip.publicip: p.fqdn],
     }
 )
 filename = "hosts.j2"
}

如果我直接通过 VS Code 运行它,我会看到创建的 hosts.j2 文件。

当我使用 Azure DevOps 管道部署它时,Plan and Apply 阶段显示文件已创建。

在此处输入图像描述

当我检查我的 DevOps 存储库时,文件不存在。

我假设(我可能是错的)这是因为文件是在构建代理上创建的。 有谁知道我如何将文件创建/复制回 Azure DevOps Repo。

你说的对。 这些文件是在构建代理上创建的。 这就是您在 Devops 存储库中看不到它们的原因。 您需要将更改提交回管道中的 Azure devops 存储库。

您可以在管道中的脚本任务中运行git commands ,以将更改推送到 devops 存储库。

如果您使用的是 yaml 管道。 您可以查看以下脚本:

steps:
- checkout: self
  persistCredentials: true  #Allow scripts to access the system token

- powershell: |
       
      git config --global user.email "you@example.com"
      git config --global user.name "username"

      git add .
      git commit -m "add hosts.j2"
      git push origin HEAD:$(Build.SourceBranchName) 

注意:允许脚本通过添加将persistCredentials设置为truecheckout部分来访问系统令牌

如果您使用的是经典管道。 您可以查看以下脚本:

首先,您需要通过启用以下选项来允许脚本访问系统令牌:

在管道编辑页面-->代理作业-->附加选项

在此处输入图像描述

您可以在脚本任务中添加以下内联脚本:

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

git add .
git commit -m "add hosts.j2"
git push https://$(System.AccessToken)@dev.azure.com/yourOrg/yourProj/_git/repoName HEAD:$(Build.SourceBranchName) 

如果您在运行上述脚本以推送到 azure 存储库时遇到权限问题。 您需要 go项目设置下的存储库 点击Git Repositories ,在安全页面,点击加号(+),搜索组{your project name} build service({your org name})并点击添加,并在访问控制摘要页面中,授予贡献并阅读允许

请查看此线程

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM