Problem
- We write config files using Terraform for both our Kubernetes Cluster or Apps
- Some of these files must be pushed to different git repos
- Just following GitOps for kubernetes and dynamic config repos
Question
- How can I perform a git clone, commit, push using terraform?
- Should we just use shell?
- Is there any provider other than https://github.com/ilya-lesikov/terraform-provider-gitfile?
- It's very close to what I have, but it hasn't been supported nor it supports the use cases I'm looking for.
So far, I have the following:
- Generate the configs:
# https://stackguides.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://stackguides.com/questions/52421656/terraform-execute-script-before-lambda-creation/52422595#52422595
depends_on = ["null_resource.git_clone"]
# https://stackguides.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}'"
}
}
Is there anything like that?
- I haven't tested this above, but I'm looking for something that allows me to do that