簡體   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