in src/cli/utils/cicd.py [0:0]
def setup_terraform_state(self, project_dir: Path, env: Environment) -> None:
"""Setup terraform state configuration for dev or prod environment"""
# Determine terraform directories - we need both for full setup
tf_dirs = []
if env == Environment.DEV:
tf_dirs = [project_dir / "deployment" / "terraform" / "dev"]
else:
# For prod/staging, set up both root and dev terraform
tf_dirs = [
project_dir / "deployment" / "terraform",
project_dir / "deployment" / "terraform" / "dev",
]
bucket_name = f"{self.config.cicd_project_id}-terraform-state"
# Ensure bucket exists and is accessible
try:
result = run_command(
["gsutil", "ls", "-b", f"gs://{bucket_name}"],
check=False,
capture_output=True,
)
if result.returncode != 0:
print(f"\n📦 Creating Terraform state bucket: {bucket_name}")
run_command(
[
"gsutil",
"mb",
"-p",
self.config.cicd_project_id,
"-l",
self.config.region,
f"gs://{bucket_name}",
]
)
run_command(
["gsutil", "versioning", "set", "on", f"gs://{bucket_name}"]
)
except subprocess.CalledProcessError as e:
print(f"\n❌ Failed to setup state bucket: {e}")
raise
# Create backend.tf in each required directory
for tf_dir in tf_dirs:
# Use different state prefixes for dev and prod/staging to keep states separate
is_dev_dir = str(tf_dir).endswith("/dev")
state_prefix = "dev" if is_dev_dir else "prod"
backend_file = tf_dir / "backend.tf"
with open(backend_file, "w") as f:
f.write(f'''terraform {{