@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
Markdown
# 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