UNPKG

@cloudkinetix/bmad-enhanced

Version:

Cloud-Kinetix enhanced fork of BMAD-METHOD - Breakthrough Method of Agile AI-driven Development with robust versioning and unified validation.

564 lines (461 loc) โ€ข 16.6 kB
# debug-ci-configuration ## Task: GitLab CI Configuration Debugging and Optimization **Purpose**: Comprehensive analysis and debugging of GitLab CI/CD configuration files with intelligent recommendations for optimization and error resolution. **When to Use**: - CI configuration syntax errors or validation issues - Pipeline performance optimization - Best practices compliance review - New CI setup validation and guidance --- ## Task Configuration ### Input Parameters - `config_file` (optional): CI config file path (default: .gitlab-ci.yml) - `validation_level` (optional): basic, standard, comprehensive (default: standard) - `optimization_focus` (optional): performance, security, maintainability (default: performance) - `generate_suggestions` (optional): Enable improvement suggestions (default: true) ### Expected Outputs - Configuration syntax validation results - Best practices compliance assessment - Performance optimization recommendations - Security and maintainability improvements - Fixed configuration suggestions --- ## Task Execution ### Phase 1: Configuration Discovery and Validation ```bash echo "๐Ÿ”ง GitLab CI Configuration Debug Analysis" echo "========================================" # Locate CI configuration file CI_CONFIG_FILE=${config_file:-".gitlab-ci.yml"} if [ ! -f "$CI_CONFIG_FILE" ]; then # Check alternative locations if [ -f "gitlab-ci.yml" ]; then CI_CONFIG_FILE="gitlab-ci.yml" elif [ -f ".gitlab-ci.yaml" ]; then CI_CONFIG_FILE=".gitlab-ci.yaml" else echo "โŒ No GitLab CI configuration file found" echo " Searched for: .gitlab-ci.yml, gitlab-ci.yml, .gitlab-ci.yaml" echo " ๐Ÿ’ก Create a .gitlab-ci.yml file to enable CI/CD" exit 1 fi fi echo "๐Ÿ“ Configuration file: $CI_CONFIG_FILE" echo "๐Ÿ“Š File size: $(wc -l < "$CI_CONFIG_FILE") lines" # Basic syntax validation using GitLab CLI echo "" echo "๐Ÿ” Syntax Validation:" echo "====================" LINT_RESULT=$(glab ci lint 2>&1) LINT_EXIT_CODE=$? if [ $LINT_EXIT_CODE -eq 0 ]; then echo "โœ… Configuration syntax is valid" else echo "โŒ Configuration has syntax errors:" echo "$LINT_RESULT" | sed 's/^/ /' echo "" echo "๐Ÿ’ก Fix syntax errors before proceeding with detailed analysis" fi ``` ### Phase 2: Structure and Best Practices Analysis ```bash echo "" echo "๐Ÿ“‹ Configuration Structure Analysis:" echo "===================================" # Analyze YAML structure echo "๐Ÿ” Analyzing configuration structure..." # Check for required sections if grep -q "^stages:" "$CI_CONFIG_FILE"; then echo "โœ… Stages defined" DEFINED_STAGES=$(grep -A 10 "^stages:" "$CI_CONFIG_FILE" | grep "^ -" | wc -l) echo " ๐Ÿ“Š Number of stages: $DEFINED_STAGES" else echo "โš ๏ธ No explicit stages defined (using default: build, test, deploy)" fi # Count job definitions JOB_COUNT=$(grep -c "^[a-zA-Z0-9_-]*:" "$CI_CONFIG_FILE" | grep -v "stages\|variables\|before_script\|after_script\|image\|services") echo "๐Ÿ“Š Approximate job count: $JOB_COUNT" # Check for common sections echo "" echo "๐Ÿ“‹ Configuration Sections:" echo "-------------------------" if grep -q "^variables:" "$CI_CONFIG_FILE"; then echo "โœ… Global variables defined" VAR_COUNT=$(grep -A 20 "^variables:" "$CI_CONFIG_FILE" | grep "^ [A-Z_]*:" | wc -l) echo " ๐Ÿ“Š Variable count: $VAR_COUNT" else echo "โš ๏ธ No global variables section" echo " ๐Ÿ’ก Consider using variables for repeated values" fi if grep -q "^before_script:" "$CI_CONFIG_FILE"; then echo "โœ… Global before_script defined" else echo "โ„น๏ธ No global before_script" fi if grep -q "^after_script:" "$CI_CONFIG_FILE"; then echo "โœ… Global after_script defined" else echo "โ„น๏ธ No global after_script" fi if grep -q "^cache:" "$CI_CONFIG_FILE"; then echo "โœ… Cache configuration present" else echo "โš ๏ธ No cache configuration" echo " ๐Ÿ’ก Consider adding cache to improve pipeline performance" fi ``` ### Phase 3: Performance Analysis ```bash echo "" echo "โšก Performance Analysis:" echo "======================" # Analyze performance-related configurations echo "๐Ÿ” Analyzing performance characteristics..." # Check for parallel execution opportunities if grep -q "parallel:" "$CI_CONFIG_FILE"; then echo "โœ… Parallel execution configured" PARALLEL_JOBS=$(grep "parallel:" "$CI_CONFIG_FILE" | wc -l) echo " ๐Ÿ“Š Jobs with parallel execution: $PARALLEL_JOBS" else echo "โš ๏ธ No parallel execution detected" echo " ๐Ÿ’ก Consider using 'parallel:' for CPU-intensive jobs" fi # Check for image optimization echo "" echo "๐Ÿณ Docker Image Analysis:" echo "-------------------------" if grep -q "image:" "$CI_CONFIG_FILE"; then echo "โœ… Docker images specified" # Extract unique images IMAGES=$(grep "image:" "$CI_CONFIG_FILE" | sed 's/.*image: *//' | sort -u) echo " ๐Ÿ“‹ Images in use:" echo "$IMAGES" | while read image; do echo " - $image" # Check for image size optimization opportunities case "$image" in *:latest) echo " โš ๏ธ Using 'latest' tag - consider specific versions" ;; *alpine) echo " โœ… Using Alpine-based image (good for size)" ;; *ubuntu*|*centos*) echo " ๐Ÿ’ก Consider Alpine-based alternatives for smaller size" ;; esac done else echo "โš ๏ธ No explicit image configuration" echo " ๐Ÿ’ก Specify images to ensure consistent environments" fi # Check for cache optimization echo "" echo "๐Ÿ“ฆ Cache Configuration Analysis:" echo "-------------------------------" if grep -q "cache:" "$CI_CONFIG_FILE"; then # Analyze cache configuration if grep -q "key:" "$CI_CONFIG_FILE"; then echo "โœ… Cache keys configured" else echo "โš ๏ธ Cache without explicit keys" echo " ๐Ÿ’ก Use cache keys for better cache hit rates" fi if grep -q "paths:" "$CI_CONFIG_FILE"; then echo "โœ… Cache paths specified" CACHE_PATHS=$(grep -A 5 "paths:" "$CI_CONFIG_FILE" | grep "^ -" | wc -l) echo " ๐Ÿ“Š Cached path count: $CACHE_PATHS" else echo "โš ๏ธ Cache without paths specified" fi else echo "๐Ÿ’ก Performance improvement opportunity: Add cache configuration" echo " Example cache configuration:" echo " cache:" echo " key: \$CI_COMMIT_REF_SLUG" echo " paths:" echo " - node_modules/" echo " - .pip-cache/" fi ``` ### Phase 4: Security Analysis ```bash echo "" echo "๐Ÿ”’ Security Analysis:" echo "===================" # Check for security best practices echo "๐Ÿ” Analyzing security configurations..." # Check for secrets management if grep -qE "(password|token|key|secret)" "$CI_CONFIG_FILE"; then echo "โš ๏ธ Potential hardcoded secrets detected" echo " ๐Ÿ” Lines with potential secrets:" grep -n -iE "(password|token|key|secret)" "$CI_CONFIG_FILE" | sed 's/^/ /' | head -5 echo " ๐Ÿ’ก Use GitLab CI/CD variables for sensitive data" else echo "โœ… No obvious hardcoded secrets detected" fi # Check for variable usage if grep -q "\$CI_" "$CI_CONFIG_FILE"; then echo "โœ… Using GitLab CI variables" CI_VAR_COUNT=$(grep -o "\$CI_[A-Z_]*" "$CI_CONFIG_FILE" | sort -u | wc -l) echo " ๐Ÿ“Š Unique CI variables used: $CI_VAR_COUNT" else echo "โ„น๏ธ Not using GitLab CI built-in variables" echo " ๐Ÿ’ก Consider using CI variables for dynamic configuration" fi # Check for only/except vs rules if grep -qE "^ (only|except):" "$CI_CONFIG_FILE"; then echo "โš ๏ธ Using legacy only/except syntax" echo " ๐Ÿ’ก Consider migrating to 'rules:' syntax" fi if grep -q "^ rules:" "$CI_CONFIG_FILE"; then echo "โœ… Using modern 'rules:' syntax" fi # Check for privileged mode if grep -q "privileged.*true" "$CI_CONFIG_FILE"; then echo "โš ๏ธ Privileged mode detected" echo " ๐Ÿ”’ Review if privileged access is truly necessary" fi ``` ### Phase 5: Maintainability Analysis ```bash echo "" echo "๐Ÿ”ง Maintainability Analysis:" echo "===========================" # Check for DRY principles echo "๐Ÿ” Analyzing code reuse and maintainability..." # Check for includes/extends usage if grep -qE "(include|extends):" "$CI_CONFIG_FILE"; then echo "โœ… Using includes/extends for reusability" if grep -q "include:" "$CI_CONFIG_FILE"; then INCLUDE_COUNT=$(grep "include:" "$CI_CONFIG_FILE" | wc -l) echo " ๐Ÿ“Š Include statements: $INCLUDE_COUNT" fi if grep -q "extends:" "$CI_CONFIG_FILE"; then EXTENDS_COUNT=$(grep "extends:" "$CI_CONFIG_FILE" | wc -l) echo " ๐Ÿ“Š Extends usage: $EXTENDS_COUNT" fi else echo "๐Ÿ’ก Maintainability opportunity: Use includes/extends for common configurations" fi # Check for repeated patterns echo "" echo "๐Ÿ”„ Pattern Analysis:" echo "-------------------" # Look for repeated script patterns COMMON_SCRIPTS=$(grep -E "^ - " "$CI_CONFIG_FILE" | sort | uniq -c | sort -nr | head -3) if [ -n "$COMMON_SCRIPTS" ]; then echo "๐Ÿ“Š Most common script commands:" echo "$COMMON_SCRIPTS" | sed 's/^/ /' echo " ๐Ÿ’ก Consider extracting common scripts to before_script or includes" fi # Check for job naming consistency JOB_NAMES=$(grep "^[a-zA-Z0-9_-]*:" "$CI_CONFIG_FILE" | grep -v "stages\|variables\|before_script\|after_script") echo "" echo "๐Ÿ“‹ Job Naming Analysis:" echo "----------------------" if echo "$JOB_NAMES" | grep -q "_"; then echo "โ„น๏ธ Using underscore naming convention" fi if echo "$JOB_NAMES" | grep -q "-"; then echo "โ„น๏ธ Using hyphen naming convention" fi # Suggest consistency if mixed if echo "$JOB_NAMES" | grep -q "_" && echo "$JOB_NAMES" | grep -q "-"; then echo "โš ๏ธ Mixed naming conventions detected" echo " ๐Ÿ’ก Consider consistent naming (either underscores or hyphens)" fi ``` ### Phase 6: Optimization Recommendations ```bash echo "" echo "๐ŸŽฏ Optimization Recommendations:" echo "===============================" # Generate focused recommendations based on analysis case "$optimization_focus" in "performance") echo "โšก Performance-Focused Recommendations:" echo "------------------------------------" if ! grep -q "cache:" "$CI_CONFIG_FILE"; then echo "1. ๐Ÿš€ ADD CACHING: Implement cache configuration" echo " - Cache dependencies (node_modules, .pip-cache, etc.)" echo " - Use appropriate cache keys for optimal hit rates" fi if ! grep -q "parallel:" "$CI_CONFIG_FILE"; then echo "2. โšก PARALLEL EXECUTION: Consider parallel jobs" echo " - Use parallel: keyword for CPU-intensive tasks" echo " - Split test suites across multiple parallel jobs" fi if grep -q ":latest" "$CI_CONFIG_FILE"; then echo "3. ๐Ÿณ IMAGE OPTIMIZATION: Use specific image versions" echo " - Replace :latest with specific versions" echo " - Consider smaller Alpine-based images" fi ;; "security") echo "๐Ÿ”’ Security-Focused Recommendations:" echo "----------------------------------" if grep -qE "(password|token|key|secret)" "$CI_CONFIG_FILE"; then echo "1. ๐Ÿ” SECRETS MANAGEMENT: Remove hardcoded secrets" echo " - Move sensitive data to GitLab CI/CD variables" echo " - Use masked and protected variables appropriately" fi if grep -q "privileged.*true" "$CI_CONFIG_FILE"; then echo "2. ๐Ÿ›ก๏ธ PRIVILEGE REVIEW: Minimize privileged access" echo " - Review necessity of privileged mode" echo " - Use least-privilege principle" fi echo "3. ๐Ÿ” REGULAR AUDITS: Implement security scanning" echo " - Add security scanning jobs" echo " - Use GitLab's built-in security features" ;; "maintainability") echo "๐Ÿ”ง Maintainability-Focused Recommendations:" echo "-----------------------------------------" if ! grep -qE "(include|extends):" "$CI_CONFIG_FILE"; then echo "1. ๐Ÿ“ฆ MODULARIZATION: Use includes/extends" echo " - Extract common configurations" echo " - Create reusable job templates" fi if echo "$JOB_NAMES" | grep -q "_" && echo "$JOB_NAMES" | grep -q "-"; then echo "2. ๐Ÿ“ NAMING CONSISTENCY: Standardize naming conventions" echo " - Choose either underscores or hyphens" echo " - Apply consistently across all jobs" fi echo "3. ๐Ÿ“š DOCUMENTATION: Add inline comments" echo " - Document complex job configurations" echo " - Explain non-obvious design decisions" ;; esac ``` ### Phase 7: Generate Improved Configuration ```bash if [ "$generate_suggestions" = "true" ]; then echo "" echo "๐Ÿ“„ Configuration Improvement Suggestions:" echo "========================================" IMPROVED_CONFIG="${CI_CONFIG_FILE}.improved" echo "๐Ÿ’ก Generating improved configuration: $IMPROVED_CONFIG" # Create improved configuration with suggestions cat > "$IMPROVED_CONFIG" << 'EOF' # Improved GitLab CI Configuration # Generated by GitLab CI/CD Automation debug-ci-configuration task # Define stages explicitly for clarity stages: - build - test - security - deploy # Global variables for consistency variables: # Add your global variables here PIP_CACHE_DIR: "$CI_PROJECT_DIR/.pip-cache" NPM_CONFIG_CACHE: "$CI_PROJECT_DIR/.npm-cache" # Global cache configuration for performance cache: key: ${CI_COMMIT_REF_SLUG} paths: - .pip-cache/ - .npm-cache/ - node_modules/ policy: pull-push # Global before script for common setup before_script: - echo "Starting job $CI_JOB_NAME in stage $CI_JOB_STAGE" # Example optimized job configurations # (Replace with your actual jobs) build_job: stage: build image: node:16-alpine # Specific version, smaller image script: - npm ci --cache .npm-cache --prefer-offline - npm run build artifacts: paths: - dist/ expire_in: 1 hour test_job: stage: test image: node:16-alpine parallel: 3 # Run tests in parallel script: - npm ci --cache .npm-cache --prefer-offline - npm run test:parallel coverage: '/Coverage: \d+\.\d+%/' security_scan: stage: security image: registry.gitlab.com/security-products/sast:latest script: - /analyzer run artifacts: reports: sast: gl-sast-report.json only: - merge_requests - main deploy_job: stage: deploy image: alpine:latest script: - echo "Deploy to production" rules: - if: $CI_COMMIT_BRANCH == "main" when: manual - when: never EOF echo "โœ… Improved configuration generated" echo "" echo "๐Ÿ“‹ Key improvements included:" echo " - Explicit stages definition" echo " - Global cache configuration" echo " - Specific image versions" echo " - Parallel test execution example" echo " - Security scanning integration" echo " - Modern rules syntax" echo "" echo "๐Ÿ’ก Review and adapt the improved configuration to your needs" fi ``` ### Phase 8: Validation and Next Steps ```bash echo "" echo "๐ŸŽฏ Next Steps:" echo "=============" if [ $LINT_EXIT_CODE -ne 0 ]; then echo "๐Ÿšจ URGENT: Fix syntax errors first" echo " 1. Address the syntax errors shown above" echo " 2. Validate with: glab ci lint" echo " 3. Re-run this analysis after fixes" else echo "โœ… Configuration syntax is valid" echo "" echo "๐Ÿ“ˆ Optimization Actions:" echo "1. ๐Ÿ“Š Review the analysis findings above" echo "2. ๐Ÿ”ง Implement recommended improvements" echo "3. ๐Ÿงช Test changes in a feature branch" echo "4. ๐Ÿ“‹ Monitor pipeline performance after changes" if [ "$generate_suggestions" = "true" ]; then echo "5. ๐Ÿ“„ Review generated improved configuration" echo "6. ๐Ÿ”„ Gradually migrate to improved patterns" fi fi echo "" echo "๐Ÿ” Validation Commands:" echo " - Syntax check: glab ci lint" echo " - Performance monitoring: monitor-pipeline-status --branch <branch>" echo " - Failure analysis: analyze-pipeline-failures" echo "" echo "โœ… CONFIGURATION DEBUG COMPLETE" echo "๐ŸŽฏ Use the insights above to optimize your CI/CD pipeline" ``` --- ## Success Criteria - โœ… Successfully validates CI configuration syntax - โœ… Provides comprehensive analysis of performance, security, and maintainability - โœ… Generates actionable optimization recommendations - โœ… Creates improved configuration suggestions when requested - โœ… Identifies common CI/CD anti-patterns and issues - โœ… Offers next steps for implementation of improvements ## Dependencies - **GitLab CLI** (`glab`) with authentication - **YAML configuration file** (.gitlab-ci.yml or similar) - **File system access** for reading and writing configuration files