# TBI Agent Pipeline Deployment Integration

This directory contains all artifacts needed to integrate CircleCI pipeline deployment capabilities into the existing tbi-agent running in EKS.

## Overview

The tbi-agent currently runs as a Microsoft Teams bot with Claude AI integration. This integration adds the ability to create complete CircleCI pipelines for TBI microservices through Teams commands.

## Architecture

### Current tbi-agent Stack
- **Location**: `/Users/iakpabio/Desktop/git-code/tbi-code/tbi-devops-mgt/src/`
- **Deployment**: `/Users/iakpabio/Desktop/git-code/tbi-code/tbi-devops-common/app/monitor/apps/tbi-agent/`
- **Technology**: Node.js, Microsoft Teams Bot Framework, Claude AI, Kubernetes client
- **Namespace**: tbi-agent
- **Ingress**: ALB (internal) at `tbi-agent.monitor.tbidevelopment.ca`

### Integration Approach

**Extend existing tbi-agent** with new pipeline deployment module that:
1. Accepts Teams commands like `@tbi-agent create pipeline <service-name> <namespace> <tech-stack>`
2. Uses Claude AI with specialized system prompt to generate pipeline files
3. Creates files in appropriate repositories (tbi-devops-environments, tbi-environments)
4. Returns status and next steps to Teams

## Components

### 1. Pipeline Deployer Module
**File**: `pipeline-deployer.js`
- Core logic for pipeline creation
- Claude AI integration with specialized prompts
- File generation and repository management
- Template processing

### 2. Kubernetes ConfigMaps
**Files**: `k8s-configmaps/`
- `pipeline-agent-config.yaml` - System prompt and configuration
- `pipeline-templates.yaml` - Reference templates from successful deployments
- `pipeline-patterns.yaml` - Steering patterns and best practices

### 3. Updated Deployment Manifests
**Files**: `k8s-manifests/`
- Updated deployment with volume mounts
- Additional secrets for GitHub access
- RBAC updates if needed

### 4. Integration Code
**Files**: `code-updates/`
- `devops-agent-updates.js` - New command handlers
- `package-json-updates.json` - Additional dependencies
- `dockerfile-updates` - Git client and tools

## Installation Steps

### Step 1: Review Current tbi-agent Code
```bash
cd /Users/iakpabio/Desktop/git-code/tbi-code/tbi-devops-mgt/src/
cat devops-agent.js  # Review current command structure
cat index.js         # Review bot initialization
cat package.json     # Review dependencies
```

### Step 2: Add Pipeline Deployer Module
```bash
# Copy the new module
cp integration/tbi-agent-pipeline-integration/code-updates/pipeline-deployer.js \
   /Users/iakpabio/Desktop/git-code/tbi-code/tbi-devops-mgt/src/

# Update devops-agent.js with new command handlers
# (See code-updates/devops-agent-updates.js for changes)
```

### Step 3: Update Dependencies
```bash
cd /Users/iakpabio/Desktop/git-code/tbi-code/tbi-devops-mgt/src/
npm install simple-git js-yaml
```

### Step 4: Create Kubernetes ConfigMaps
```bash
cd /Users/iakpabio/Desktop/git-code/tbi-code/tbi-devops-common/app/monitor/apps/tbi-agent/

# Create ConfigMaps
kubectl create configmap tbi-agent-pipeline-config \
  --from-file=integration/tbi-agent-pipeline-integration/k8s-configmaps/pipeline-agent-config.yaml \
  -n tbi-agent --dry-run=client -o yaml > configmap-pipeline-config.yaml

kubectl create configmap tbi-agent-pipeline-templates \
  --from-file=integration/tbi-agent-pipeline-integration/k8s-configmaps/templates/ \
  -n tbi-agent --dry-run=client -o yaml > configmap-pipeline-templates.yaml
```

### Step 5: Update Deployment
```bash
# Update deployment.yaml with volume mounts
# (See k8s-manifests/deployment-updates.yaml for changes)

# Commit to tbi-devops-common
git add .
git commit -m "Add pipeline deployment capabilities to tbi-agent"
git push
```

### Step 6: ArgoCD Sync
ArgoCD will automatically detect changes and sync the deployment.

### Step 7: Test
Send Teams message:
```
@tbi-agent create pipeline tbi-test-service redtag nodejs
```

## Usage

### Create New Pipeline
```
@tbi-agent create pipeline <service-name> <namespace> <tech-stack>
```

Example:
```
@tbi-agent create pipeline tbi-booking-service redtag nodejs
```

### Supported Tech Stacks
- `nodejs` - Node.js/NestJS/Express services
- `python` - Python/FastAPI/Flask services
- `java` - Java/Spring Boot services

### Supported Namespaces
- `redtag` - RedTag mobile services
- `access` - Package Travelbrands services
- `privatelabel` - Private label services

## How It Works

1. **User sends Teams command** with service details
2. **tbi-agent receives message** and routes to pipeline deployment handler
3. **Pipeline deployer module**:
   - Loads system prompt and templates from ConfigMaps
   - Constructs detailed prompt for Claude AI
   - Calls Claude API with specialized pipeline deployment context
4. **Claude AI generates**:
   - CircleCI config.yml
   - Dockerfile
   - Deployment scripts
   - Helm charts
   - Values and secrets templates
5. **Pipeline deployer**:
   - Parses AI response
   - Creates files in appropriate repositories
   - Commits and pushes changes
6. **tbi-agent responds** with:
   - Summary of created files
   - Next steps for deployment
   - Links to CircleCI pipeline

## Configuration

### Environment Variables (Add to deployment)
```yaml
- name: TBI_DEVOPS_ENVIRONMENTS_REPO
  value: "git@github.com:travelbrands/tbi-devops-environments.git"
- name: TBI_ENVIRONMENTS_REPO
  value: "git@github.com:travelbrands/tbi-environments.git"
- name: GITHUB_TOKEN
  valueFrom:
    secretKeyRef:
      name: tbi-agent-secrets
      key: github-token
- name: ANTHROPIC_API_KEY
  valueFrom:
    secretKeyRef:
      name: tbi-agent-secrets
      key: anthropic-api-key
```

### Secrets (Update tbi-agent-secrets)
```bash
kubectl create secret generic tbi-agent-secrets \
  --from-literal=github-token=<token> \
  --from-literal=anthropic-api-key=<key> \
  -n tbi-agent --dry-run=client -o yaml | kubectl apply -f -
```

## File Structure

```
integration/tbi-agent-pipeline-integration/
├── README.md                          # This file
├── code-updates/
│   ├── pipeline-deployer.js          # New module to add
│   ├── devops-agent-updates.js       # Changes for devops-agent.js
│   ├── package-json-updates.json     # New dependencies
│   └── dockerfile-updates            # Dockerfile changes
├── k8s-configmaps/
│   ├── pipeline-agent-config.yaml    # System prompt ConfigMap
│   ├── pipeline-templates.yaml       # Templates ConfigMap
│   └── templates/                    # Individual template files
│       ├── circleci-config.yml
│       ├── dockerfile-nodejs
│       ├── deploy-script.sh
│       └── helm-chart-templates/
├── k8s-manifests/
│   ├── deployment-updates.yaml       # Deployment changes
│   ├── secrets-template.yaml         # Secrets template
│   └── rbac-updates.yaml            # RBAC updates (if needed)
└── docs/
    ├── INTEGRATION-GUIDE.md          # Detailed integration steps
    ├── TESTING-GUIDE.md              # Testing procedures
    └── TROUBLESHOOTING.md            # Common issues
```

## Benefits

1. **Unified Interface**: Use Teams bot for all DevOps operations
2. **AI-Powered**: Leverages Claude AI for intelligent pipeline generation
3. **Template-Based**: Uses proven patterns from successful deployments
4. **Consistent**: Ensures all pipelines follow TBI standards
5. **Fast**: Creates complete pipeline in seconds
6. **Auditable**: All operations logged through Teams
7. **Kubernetes Native**: Runs in EKS with proper RBAC

## Next Steps

1. Review current tbi-agent code structure
2. Test pipeline-deployer module locally
3. Create ConfigMaps with templates
4. Update deployment manifests
5. Deploy via ArgoCD
6. Test with sample service
7. Document for team

## Support

For issues or questions:
1. Check TROUBLESHOOTING.md
2. Review tbi-agent logs: `kubectl logs -n tbi-agent -l app=tbi-agent`
3. Check CircleCI pipeline logs
4. Contact DevOps team

## References

- CircleCI Pipeline Agent: `.kiro/agents/circleci-pipeline-deployment-agent.json`
- Pipeline Patterns: `.kiro/steering/circleci-pipeline-patterns.md`
- Reference Service: `dev/tbi-acc-hotel-mapping-service/`
- tbi-agent Deployment: `/Users/iakpabio/Desktop/git-code/tbi-code/tbi-devops-common/app/monitor/apps/tbi-agent/`
