[英]Terraform local_file missing when running code using Azure Devops pipeline
When I create a "local_file" resource in the Outputs.tf using terraform:当我使用 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"
}
If i run this directly via VS Code, I see the hosts.j2 file created.如果我直接通过 VS Code 运行它,我会看到创建的 hosts.j2 文件。
When I deploy this using Azure DevOps pipelines, the Plan and Apply stage show that the files are created.当我使用 Azure DevOps 管道部署它时,Plan and Apply 阶段显示文件已创建。
When I check my DevOps repository, the files are not there.当我检查我的 DevOps 存储库时,文件不存在。
I am assuming (I can be wrong) this is because the files are created on the build agent.我假设(我可能是错的)这是因为文件是在构建代理上创建的。 Does anyone know how I can get the file created/copied back to the Azure DevOps Repo.
有谁知道我如何将文件创建/复制回 Azure DevOps Repo。
You are right.你说的对。 The files are created on the build agent.
这些文件是在构建代理上创建的。 That is the reason you cannot see them in your Devops Repository.
这就是您在 Devops 存储库中看不到它们的原因。 You need to commit the changes back to the Azure devops repo in your pipeline.
您需要将更改提交回管道中的 Azure devops 存储库。
You can run the git commands
in a script task in the pipeline to push the changes to devops repo.您可以在管道中的脚本任务中运行
git commands
,以将更改推送到 devops 存储库。
If you are using yaml pipeline.如果您使用的是 yaml 管道。 You can check out below script:
您可以查看以下脚本:
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)
Note: Allow scripts to access the system token by adding a checkout
section with persistCredentials
set to true
注意:允许脚本通过添加将
persistCredentials
设置为true
的checkout
部分来访问系统令牌
If you are using Classic pipeline.如果您使用的是经典管道。 You can check out below scripts:
您可以查看以下脚本:
First, you need to allow scripts to access the system token by enabling the below option:首先,您需要通过启用以下选项来允许脚本访问系统令牌:
In pipeline edit page-->Agent job-->Additional options在管道编辑页面-->代理作业-->附加选项
You can add below inline scripts in the script task:您可以在脚本任务中添加以下内联脚本:
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)
If you encounter permission issue when running above scripts to push to azure repo.如果您在运行上述脚本以推送到 azure 存储库时遇到权限问题。 You need to go the Repositories under project settings .
您需要 go项目设置下的存储库。 Click Git Repositories , in the security page, click plus(+) and search for group
{your project name} build service({your org name})
and click to add it, and In the access control summary page, grant contribute and read permission点击Git Repositories ,在安全页面,点击加号(+),搜索组
{your project name} build service({your org name})
并点击添加,并在访问控制摘要页面中,授予贡献并阅读允许
Please check out this thread .请查看此线程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.