# Quick Start: Integrating Pipeline Deployment into tbi-agent

This guide provides the fastest path to integrate CircleCI pipeline deployment capabilities into your existing tbi-agent.

## Prerequisites

- tbi-agent running in EKS (namespace: tbi-agent)
- Access to `/Users/iakpabio/Desktop/git-code/tbi-code/tbi-devops-common`
- kubectl access to tbi-agent namespace
- GitHub token with repo access
- Anthropic API key

## 5-Step Integration (30 minutes)

### Step 1: Copy Module and Install Dependencies (5 min)

```bash
# Copy the pipeline deployer module
cp /Users/iakpabio/Desktop/git-code/tbi-code/tbi-devops-environments/integration/tbi-agent-pipeline-integration/code-updates/pipeline-deployer.js \
   /Users/iakpabio/Desktop/git-code/tbi-code/tbi-devops-common/src/tbi-agent/

# Install dependencies
cd /Users/iakpabio/Desktop/git-code/tbi-code/tbi-devops-common/src/tbi-agent
npm install simple-git js-yaml
```

### Step 2: Update devops-agent.js (10 min)

Open `/Users/iakpabio/Desktop/git-code/tbi-code/tbi-devops-common/src/tbi-agent/devops-agent.js`

**Add at the top:**
```javascript
const PipelineDeployer = require('./pipeline-deployer');
```

**In constructor (or initialization):**
```javascript
this.pipelineDeployer = new PipelineDeployer({
  anthropicApiKey: process.env.ANTHROPIC_API_KEY,
  devopsEnvironmentsRepo: process.env.TBI_DEVOPS_ENVIRONMENTS_REPO,
  environmentsRepo: process.env.TBI_ENVIRONMENTS_REPO
});
```

**In initialize method:**
```javascript
await this.pipelineDeployer.initialize();
console.log('Pipeline deployer initialized');
```

**In message processing (add these command handlers):**
```javascript
// Pipeline deployment commands
if (command === 'create' && subCommand === 'pipeline') {
  return await this.handleCreatePipeline(context, args);
}

if (command === 'pipeline' && subCommand === 'help') {
  return await this.handlePipelineHelp(context);
}
```

**Add these methods (copy from `code-updates/devops-agent-updates.js`):**
- `handleCreatePipeline(context, args)`
- `handlePipelineHelp(context)`
- `parseOptions(optionArgs)`

### Step 3: Create Kubernetes ConfigMaps (5 min)

```bash
cd /Users/iakpabio/Desktop/git-code/tbi-code/tbi-devops-common/app/monitor/apps/tbi-agent/

# Create system prompt ConfigMap
kubectl create configmap tbi-agent-pipeline-config \
  --from-file=system-prompt.txt=/Users/iakpabio/Desktop/git-code/tbi-code/tbi-devops-environments/integration/tbi-agent-pipeline-integration/k8s-configmaps/system-prompt.txt \
  -n tbi-agent

# Create templates ConfigMap
kubectl create configmap tbi-agent-pipeline-templates \
  --from-file=/Users/iakpabio/Desktop/git-code/tbi-code/tbi-devops-environments/integration/tbi-agent-pipeline-integration/k8s-configmaps/templates/ \
  -n tbi-agent
```

### Step 4: Update Deployment (5 min)

Edit `/Users/iakpabio/Desktop/git-code/tbi-code/tbi-devops-common/app/monitor/apps/tbi-agent/deployment.yaml`

**Add volumes:**
```yaml
spec:
  template:
    spec:
      volumes:
        - name: pipeline-config
          configMap:
            name: tbi-agent-pipeline-config
        - name: pipeline-templates
          configMap:
            name: tbi-agent-pipeline-templates
```

**Add volume mounts:**
```yaml
      containers:
        - name: tbi-agent
          volumeMounts:
            - name: pipeline-config
              mountPath: /app/config
            - name: pipeline-templates
              mountPath: /app/templates
```

**Add environment variables:**
```yaml
          env:
            - name: TBI_DEVOPS_ENVIRONMENTS_REPO
              value: "https://$(GITHUB_TOKEN)@github.com/redlabelvacations/tbi-devops-environments.git"
            - name: TBI_ENVIRONMENTS_REPO
              value: "https://$(GITHUB_TOKEN)@github.com/redlabelvacations/tbi-environments.git"
            - name: GITHUB_TOKEN
              valueFrom:
                secretKeyRef:
                  name: tbi-agent-secrets
                  key: github-token
```

### Step 5: Deploy (5 min)

```bash
# Commit code changes
cd /Users/iakpabio/Desktop/git-code/tbi-code/tbi-devops-common
git add src/tbi-agent/ app/monitor/apps/tbi-agent/
git commit -m "Add pipeline deployment capabilities to tbi-agent"
git push

# ArgoCD will auto-sync (or manually sync in ArgoCD UI)

# Verify deployment
kubectl get pods -n tbi-agent
kubectl logs -n tbi-agent -l app=tbi-agent --tail=50 | grep "Pipeline deployer initialized"
```

## Test It

Send this command in Microsoft Teams:

```
@tbi-agent create pipeline tbi-test-service redtag nodejs
```

Expected response (30-60 seconds):
```
✅ Successfully created CircleCI pipeline for tbi-test-service

Configuration:
- Namespace: redtag
- Technology: nodejs
- Files created: 15

Next Steps:
1. Review generated files in repositories
2. Update secrets file with actual values
3. Create VERSION file in service repository
...
```

## Verify Generated Files

```bash
# Check tbi-devops-environments
cd /Users/iakpabio/Desktop/git-code/tbi-code/tbi-devops-environments
git pull
ls -la dev/tbi-test-service/

# Check tbi-environments
cd /Users/iakpabio/Desktop/git-code/tbi-code/tbi-environments
git pull
cat dev/values/tbi-test-service.yaml
```

## Troubleshooting

### "Pipeline deployer initialized" not in logs
- Check ConfigMaps exist: `kubectl get configmap -n tbi-agent`
- Check volume mounts: `kubectl describe pod -n tbi-agent -l app=tbi-agent`
- Check pod logs for errors: `kubectl logs -n tbi-agent -l app=tbi-agent`

### "Module not found: simple-git"
- Ensure npm install ran successfully
- Check package.json has the dependencies
- Rebuild Docker image if needed

### "Permission denied: git clone"
- Verify GitHub token in secret: `kubectl get secret tbi-agent-secrets -n tbi-agent -o yaml`
- Ensure token has repo access
- Check token format in environment variable

### No response from bot
- Check bot is running: `kubectl get pods -n tbi-agent`
- Check bot logs: `kubectl logs -n tbi-agent -l app=tbi-agent --tail=100`
- Verify Teams connection is active

## Commands Reference

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

# Examples
@tbi-agent create pipeline tbi-booking-service redtag nodejs
@tbi-agent create pipeline tbi-api-gateway access python --port=8000

# Help
@tbi-agent pipeline help

# Valid namespaces: redtag, access, privatelabel, copolo
# Valid tech stacks: nodejs, python, java
```

## What Gets Created

For each pipeline, the following files are generated:

**tbi-devops-environments:**
- `.circleci/config.yml` - CircleCI pipeline
- `docker/Dockerfile` - Multi-stage Dockerfile
- `scripts/deploy-services.sh` - Deployment script with all fixes
- `scripts/setup-kubectl.sh` - kubectl setup script
- `charts/[service]/Chart.yaml` - Helm chart metadata
- `charts/[service]/templates/` - All Kubernetes manifests

**tbi-environments:**
- `dev/values/[service].yaml` - Service configuration
- `dev/secrets/secrets.[service].yaml` - Secrets template
- `dev/requirements.yaml` - Updated with new service

## Next Steps

1. Test with a real service
2. Share with team
3. Monitor usage and quality
4. Add more features:
   - Pipeline status checking
   - List all pipelines
   - Update existing pipelines
   - Validation before creation

## Support

- Check logs: `kubectl logs -n tbi-agent -l app=tbi-agent`
- Review integration files: `/Users/iakpabio/Desktop/git-code/tbi-code/tbi-devops-environments/integration/tbi-agent-pipeline-integration/`
- Contact DevOps team

---

**Total Time**: 30 minutes
**Difficulty**: Easy
**Impact**: High - enables fast pipeline creation for all services
