<h1>Problem</h1>
<ul>
<li>We write config files using Terraform for both our Kubernetes Cluster or Apps</li>
<li>Some of these files must be pushed to different git repos
<ul>
<li>Just following GitOps for kubernetes and dynamic config repos</li>
</ul>
</li>
</ul>
<h1>Question</h1>
<ul>
<li>How can I perform a git clone, commit, push using terraform?
<ul>
<li>Should we just use shell?</li>
<li>Is there any provider other than <a href="https://github.com/ilya-lesikov/terraform-provider-gitfile" rel="nofollow noreferrer">https://github.com/ilya-lesikov/terraform-provider-gitfile</a>?
<ul>
<li>It's very close to what I have, but it hasn't been supported nor it supports the use cases I'm looking for.</li>
</ul>
</li>
</ul>
</li>
</ul>
<h1>So far, I have the following:</h1>
<ul>
<li>Generate the configs:</li>
</ul>
<pre><code># https://stackoverflow.com/questions/36629367/getting-an-environment-variable-in-terraform-configuration/36672931#36672931
variable GITLAB_CLONE_TOKEN {}

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

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

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

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

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

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

  provisioner &quot;local-exec&quot; {
    command = &quot;git -C ${local.clone_location} push origin ${local.branch}'&quot;
  }
}
</code></pre>
<h1>Is there anything like that?</h1>
<ul>
<li>I haven't tested this above, but I'm looking for something that allows me to do that</li>
</ul>

Marcello DeSales
<blockquote>
<p>How can I perform a git clone, commit, push using terraform?</p>
</blockquote>
<blockquote>
<p>Should we just use shell?</p>
</blockquote>
<p>Terraform is a good tool - it is best for <em>provisioning immutable infrastructure</em>. Shell script might also have its place, but when you can, it is preferably to use a more declarative approach.</p>
<p>What you describe with &quot;git clone, commit, push&quot; is essentially some of the steps that is commonly done in something like a Build or Deployment Pipeline. Terraform might be a good tool to use in some of the steps, but it is not the best tool to orchestrate the full workflow, in my point of view.</p>
<p>A tool made for orchestrating pipeline workflows might be best for this, like e.g.</p>
<ul>
<li><a href="https://github.com/tektoncd/pipeline" rel="nofollow noreferrer">Tekton Pipelines</a> - with Tasks for Git and <a href="https://github.com/tektoncd/catalog/tree/master/task/terraform-cli/0.1" rel="nofollow noreferrer">Terraform</a> to be used as steps in a workflow.</li>
<li><a href="https://github.com/argoproj/argo" rel="nofollow noreferrer">Argo Workflows</a></li>
<li><a href="https://github.com/features/actions" rel="nofollow noreferrer">GitHub Actions</a></li>
<li>And possibly <a href="https://www.terraform.io/cloud" rel="nofollow noreferrer">Terraform Cloud</a> (haven't used it, can't say if it can do exact what you ask)</li>
</ul>
