#!/bin/bash set -e OUTPUT_FILE=".gitlab-ci.generated.yml" cat > "$OUTPUT_FILE" << 'HEADER' workflow: rules: - if: '$CI_PIPELINE_SOURCE == "pipeline"' - when: always variables: AWS_EC2_METADATA_DISABLED: "true" TF_CLI_ARGS: "-no-color" TG_LOG_LEVEL: "error" stages: - validate - plan - apply HEADER find live -name "terragrunt.hcl" -not -path "*/.terragrunt-cache/*" | sort | while read -r config_file; do component_dir=$(dirname "$config_file") relative_path=$(echo "$component_dir" | sed 's|^live/||') env=$(echo "$relative_path" | cut -d'/' -f1) component=$(echo "$relative_path" | cut -d'/' -f2) if [ -z "$component" ] || [ "$component" = "$env" ]; then continue fi job_prefix="${env}-${component}" full_component_dir="${component_dir}" needs_section="" if [ "$component" = "secrets" ]; then needs_section=" needs: - validate-${env}-namespace - validate-${env}-s3 - validate-${env}-database dependencies: - validate-${env}-namespace - validate-${env}-s3 - validate-${env}-database" fi case "$env" in stage) folder_var="YC_STAGE_FOLDER_ID" ;; prod) folder_var="YC_PROD_FOLDER_ID" ;; preprod) folder_var="YC_PREPROD_FOLDER_ID" ;; *) folder_var="YC_FOLDER_ID" ;; esac cat >> "$OUTPUT_FILE" << VALIDATE_JOB validate-${job_prefix}: stage: validate image: \$TERRAFORM_IMAGE_NAME variables: TG_ROOT: "${full_component_dir}" ENVIRONMENT: "${env}" YC_FOLDER_ID: "\$${folder_var}" before_script: - cd \$TG_ROOT - echo "Working directory:" && pwd - ls -la script: - terragrunt init -upgrade -reconfigure - terragrunt validate rules: - if: '\$CI_PIPELINE_SOURCE == "merge_request_event"' - if: '\$CI_COMMIT_BRANCH == "master" || \$CI_COMMIT_BRANCH == "main"' - if: '\$CI_COMMIT_BRANCH =~ /^feature\/.*/' - if: '\$CI_PIPELINE_SOURCE == "pipeline"' interruptible: true artifacts: paths: - ${full_component_dir}/.terragrunt-cache/ expire_in: 1 hour when: always ${needs_section} VALIDATE_JOB cat >> "$OUTPUT_FILE" << PLAN_JOB plan-${job_prefix}: stage: plan image: \$TERRAFORM_IMAGE_NAME variables: TG_ROOT: "${full_component_dir}" ENVIRONMENT: "${env}" YC_FOLDER_ID: "\$${folder_var}" needs: - validate-${job_prefix} before_script: - cd \$TG_ROOT - ls -la script: - terragrunt init -upgrade -reconfigure - terragrunt plan -out=tfplan rules: - if: '\$CI_COMMIT_BRANCH == "master" || \$CI_COMMIT_BRANCH == "main"' - if: '\$CI_PIPELINE_SOURCE == "merge_request_event"' - if: '\$CI_COMMIT_BRANCH =~ /^feature\/.*/' - if: '\$CI_PIPELINE_SOURCE == "pipeline"' interruptible: true artifacts: paths: - ${full_component_dir}/tfplan - ${full_component_dir}/.terragrunt-cache/ expire_in: 1 week when: always PLAN_JOB if [ "$env" = "prod" ]; then when_clause="manual" else when_clause="on_success" fi apply_needs="plan-${job_prefix}" if [ "$component" = "secrets" ]; then apply_needs="plan-${job_prefix} - apply-${env}-namespace - apply-${env}-s3 - apply-${env}-database" fi cat >> "$OUTPUT_FILE" << APPLY_JOB apply-${job_prefix}: stage: apply image: \$TERRAFORM_IMAGE_NAME variables: TG_ROOT: "${full_component_dir}" ENVIRONMENT: "${env}" YC_FOLDER_ID: "\$${folder_var}" needs: - ${apply_needs} before_script: - cd \$TG_ROOT - ls -la script: - terragrunt init -upgrade -reconfigure - terragrunt apply -auto-approve rules: - if: '\$CI_COMMIT_BRANCH == "master" || \$CI_COMMIT_BRANCH == "main"' when: ${when_clause} - if: '\$CI_COMMIT_BRANCH =~ /^feature\/.*/' when: ${when_clause} - if: '\$CI_PIPELINE_SOURCE == "pipeline"' when: ${when_clause} interruptible: true APPLY_JOB done echo "Pipeline generated successfully: $OUTPUT_FILE" echo "Found components:" find live -name "terragrunt.hcl" -not -path "*/.terragrunt-cache/*" | sed 's|live/||; s|/terragrunt.hcl||' | grep '/' | sort