运行 代码使用 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 管道部署它时,计划和应用阶段显示文件已创建。
当我检查我的 DevOps 存储库时,文件不在那里。
我假设(我可能是错的)这是因为文件是在构建代理上创建的。有谁知道如何将文件 created/copied 返回到 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)
注意:Allow scripts to access the system token 通过添加 checkout
部分并将 persistCredentials
设置为 true
如果您使用的是经典管道。您可以查看以下脚本:
首先,您需要启用以下选项以允许脚本访问系统令牌:
管道编辑页面-->代理作业-->其他选项
您可以在脚本任务中添加以下内联脚本:
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 存储库时遇到权限问题。您需要转到 项目设置 下的 存储库 。点击Git Repositories,在安全页面点击加号(+)搜索组{your project name} build service({your org name})
点击添加,在访问控制摘要页面, 授予 贡献 并阅读 权限
请查看this thread。
当我使用 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 管道部署它时,计划和应用阶段显示文件已创建。
当我检查我的 DevOps 存储库时,文件不在那里。
我假设(我可能是错的)这是因为文件是在构建代理上创建的。有谁知道如何将文件 created/copied 返回到 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)
注意:Allow scripts to access the system token 通过添加 checkout
部分并将 persistCredentials
设置为 true
如果您使用的是经典管道。您可以查看以下脚本:
首先,您需要启用以下选项以允许脚本访问系统令牌:
管道编辑页面-->代理作业-->其他选项
您可以在脚本任务中添加以下内联脚本:
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 存储库时遇到权限问题。您需要转到 项目设置 下的 存储库 。点击Git Repositories,在安全页面点击加号(+)搜索组{your project name} build service({your org name})
点击添加,在访问控制摘要页面, 授予 贡献 并阅读 权限
请查看this thread。