git - 如何使用 terraform 克隆 git repo、更新文件并将提交推送到它?
问题描述
问题
- 我们使用 Terraform 为 Kubernetes 集群或应用程序编写配置文件
- 其中一些文件必须推送到不同的 git repos
- 只需关注 Kubernetes 和动态配置存储库的 GitOps
问题
- 如何使用 terraform 执行 git clone、提交、推送?
- 我们应该只使用shell吗?
- 除了https://github.com/ilya-lesikov/terraform-provider-gitfile之外还有其他供应商吗?
- 它与我所拥有的非常接近,但它不受支持,也不支持我正在寻找的用例。
到目前为止,我有以下内容:
- 生成配置:
# 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}'"
}
}
有这样的吗?
- 我没有在上面测试过,但我正在寻找可以让我这样做的东西
解决方案
如何使用 terraform 执行 git clone、提交、推送?
我们应该只使用shell吗?
Terraform 是一个很好的工具——它最适合配置不可变的基础设施。Shell 脚本可能也有它的位置,但如果可以,最好使用更具声明性的方法。
您用“git clone、commit、push”描述的本质上是一些通常在构建或部署管道中完成的步骤。Terraform 可能是在某些步骤中使用的好工具,但在我看来,它并不是编排整个工作流程的最佳工具。
用于编排管道工作流的工具可能是最好的,例如
- Tekton Pipelines - 带有用于 Git 和Terraform的任务,可用作工作流中的步骤。
- 阿尔戈工作流程
- GitHub 操作
- 可能还有Terraform Cloud(没用过,不能说它是否能完全满足你的要求)
推荐阅读
- azure-logic-apps - 在逻辑应用中将变量设置为空字符串
- python - 将 dask Series 列表转换为 dask DataFrame
- javascript - jquery 加载在 addEventListener 中不起作用
- python - 如何提取字符串的哪些句子已更改或不存在于另一个字符串中?
- javascript - 根据年份获取计数
- python - 使用等同于 wget 命令的 Python wget 代码从 LAADS 下载数据
- reactjs - 在函数成功执行后重定向到 React App 的特定部分
- c# - Selenium C#:如何在另一个类中获取一个类的属性/属性?
- javascript - 通过嵌套对象属性映射时如何减少嵌套?
- spring-boot - shiro buji pac4j cas 单点注销不起作用