You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Terragrunt如何指定Git仓库子目录为源码解决目录为空报错

问题修复方案

根因

当前terragrunt.hcl配置的Terraform源指向目标Git仓库的根目录,而main.tf等配置实际存放在仓库的terraform子目录下,Terragrunt默认读取仓库根目录的Terraform文件,因此找不到有效配置抛出目录为空的错误。

修复方法

无需修改目标Git仓库内容,只需调整当前流水线目录下terragrunt.hcl的terraform.source字段,在Git源地址末尾追加//terraform指定读取仓库内的对应子目录即可,该路径语法为Terraform/Terragrunt原生支持:

iam_role = "???"
remote_state {
  backend = "s3"

  config = {
    ??? [this definitely works]
  }
}
terraform {
  # 末尾追加//terraform指向仓库内的terraform子目录
  source = "git::ssh://gitlab@???.git//terraform"
  extra_arguments "bucket" {
    commands = "${get_terraform_commands_that_need_vars()}"
    optional_var_files = ["${get_terragrunt_dir()}/account.tfvars"]
  }
}

其他配置说明

  • 原有Jenkins执行命令无需修改,--terragrunt-include-dir参数作用是指定执行时要包含的terragrunt本地配置目录,和源仓库内部路径无关
  • 原有service.yaml配置可保持不变

内容的提问来源于stack exchange,提问作者Yonatan Huber

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 07:45:02