首页 > 解决方案 > 如何使用 terraform 克隆 git repo、更新文件并将提交推送到它?

问题描述

问题

问题

到目前为止,我有以下内容:

# https://stackoverflow.com/questions/36629367/getting-an-environment-variable-in-terraform-configuration/36672931#36672931
variable GITLAB_CLONE_TOKEN {}

locals {
  carCrdInstance = {
    apiVersion = "car.io/v1"
    kind       = "Car"
    metadata = {
      name = "super-car"
    }
    spec = {
      convertible = "true"
      color = "black"
    }
  }

  # https://docs.gitlab.com/ee/user/project/deploy_tokens/#git-clone-a-repository
  clone_location = "${path.module}/.gitops"
  branch = "feature/crds-setup"
}

resource "null_resource" "git_clone" {
  provisioner "local-exec" {
    command = "git clone --branch ${local.branch} https://${var.username}:${var.GITLAB_CLONE_TOKEN}@gitlab.example.com/tanuki/awesome_project.git ${local.clone_location}"
  }
}

resource "local_file" "cert_manager_cluster_issuer_object" {
  content  = yamlencode(local.cert_issuer)
  filename = "${git_repo.configs.destination}/crds/instances/white-convertible.yaml"

  # https://stackoverflow.com/questions/52421656/terraform-execute-script-before-lambda-creation/52422595#52422595
  depends_on = ["null_resource.git_clone"]

  # https://stackoverflow.com/questions/7149984/how-do-i-execute-a-git-command-without-being-in-the-repository/35899275#35899275
  provisioner "local-exec" {
    command = "git -C ${local.clone_location} commit -am ':new: updating cars...'"
  }

  provisioner "local-exec" {
    command = "git -C ${local.clone_location} push origin ${local.branch}'"
  }
}

有这样的吗?

标签: gitkubernetesterraform

解决方案


如何使用 terraform 执行 git clone、提交、推送?

我们应该只使用shell吗?

Terraform 是一个很好的工具——它最适合配置不可变的基础设施。Shell 脚本可能也有它的位置,但如果可以,最好使用更具声明性的方法。

您用“git clone、commit、push”描述的本质上是一些通常在构建或部署管道中完成的步骤。Terraform 可能是在某些步骤中使用的好工具,但在我看来,它并不是编排整个工作流程的最佳工具。

用于编排管道工作流的工具可能是最好的,例如


推荐阅读