【问题标题】:Terraform -replace single resource on AzureTerraform - 替换 Azure 上的单个资源
【发布时间】:2022-07-03 18:21:24
【问题描述】:

Terraform - 我在天蓝色管道 YML 文件中有两个资源

  1. Docker 镜像推送到 Azure Container Registry(docker 镜像是 STRAPI 镜像)
  2. 使用 docker 镜像创建应用服务

我想在 STRAPI 代码(文件系统)中每次有新的或修改的文件时替换 docker 映像,我如何在此处使用 -replace 命令。有什么建议吗?

当前 terraform 计划未识别文件系统的任何更改,并在 TERRAFORM-PLAN 中引发以下消息

没有变化。您的基础架构与配置匹配。

Terraform has compared your real infrastructure against your configuration
and found no differences, so no changes are needed.

**已更新代码:

  1. 下面是我的 main.tf 文件中的代码**
resource "null_resource" "docker_push_strapiaa" {
  provisioner "local-exec" {
    command = <<-EOT
    docker login ${data.azurerm_container_registry.acr.login_server} --username ${data.azurerm_container_registry.acr.admin_username} --password ${data.azurerm_container_registry.acr.admin_password}
    docker build . -t ${data.azurerm_container_registry.acr.login_server}/${var.strapi_image} -f strapi.Dockerfile --platform linux/amd64
    docker push ${data.azurerm_container_registry.acr.login_server}/${var.strapi_image}     
    EOT
    }
}

【问题讨论】:

  • 您必须添加您正在使用的代码,以便我们了解正在发生的事情。理论上,您应该在构建后替换引用的图像,但根据问题我不太了解。
  • 我已将代码添加到主帖
  • 好的,既然您使用的是null_resource,为什么不运行 azure CLI 命令来更新其中的图像?
  • 我使用下面的命令来构建镜像 ---------------------------------- ------------------------------------- 泊坞窗构建。 -t ${data.azurerm_container_registry.acr.login_server}/${var.strapi_image} -f Strapi.Dockerfile --platform linux/amd64
  • @MarkoE 如果没有变化,资源“null_resource”“docker_push_strapiaa”将不会被构建,我想强制部署“null_resource”

标签: azure docker terraform


【解决方案1】:

这是解决这个问题的方法,它工作正常,每次管道工作时都会重新构建 docker 映像

 resource "null_resource" "docker_push_strapi" {
    
      triggers = {
        always_run = "${timestamp()}"
      }
      
      provisioner "local-exec" {
        command = <<-EOT
            docker login ${data.azurerm_container_registry.acr.login_server} --username ${data.azurerm_container_registry.acr.admin_username} --password ${data.azurerm_container_registry.acr.admin_password}
            docker build . -t ${data.azurerm_container_registry.acr.login_server}/${var.strapi_image} -f strapi.Dockerfile --platform linux/amd64
            docker push ${data.azurerm_container_registry.acr.login_server}/${var.strapi_image}
          EOT
      }
    }

【讨论】:

    猜你喜欢
    • 2021-01-08
    • 2018-05-06
    • 2021-01-04
    • 2020-03-26
    • 1970-01-01
    • 2018-11-24
    • 2020-11-18
    • 1970-01-01
    • 2019-11-30
    相关资源
    最近更新 更多