# Registry and CI/CD Setup Guide

This guide walks you through setting up the container registry and CI/CD pipeline for the SQDC Dashboard.

## Registry Information

- **Registry URL**: `gitea.vidoks.fr`
- **Repository**: `sortifal/pfee`
- **Full Image Path**: `gitea.vidoks.fr/sortifal/pfee:latest`

## Prerequisites

1. Gitea account with access to `sortifal/pfee` repository
2. Kubernetes cluster (k3s) with kubectl configured
3. Registry credentials (username and password/token)

## Step 1: Configure Gitea Secrets

Go to your Gitea repository: **Settings → Secrets → Actions**

Add the following secrets:

### Required Secrets

| Secret Name | Description | Example Value |
|------------|-------------|---------------|
| `KUBE_CONFIG` | Plain text kubeconfig for k3s | Contents of `~/.kube/config` |
| `REGISTRY_URL` | Container registry URL | `gitea.vidoks.fr` |
| `REGISTRY_USER` | Registry username | Your Gitea username |
| `REGISTRY_PASSWORD` | Registry password or token | Your Gitea password/token |

### How to get KUBE_CONFIG

```bash
# Display your kubeconfig
cat ~/.kube/config

# Copy the entire output and paste it as the KUBE_CONFIG secret
```

### How to create a Gitea Token

1. Go to your Gitea profile → Settings → Applications
2. Create a new token with `write:package` permission
3. Use this token as `REGISTRY_PASSWORD`

## Step 2: Verify Workflow Configuration

The workflow file at [.gitea/workflows/build-deploy.yml](.gitea/workflows/build-deploy.yml) is already configured with:

- Image path: `gitea.vidoks.fr/sortifal/pfee`
- Triggers: Push to `main` or `dashboard` branches
- Build, deploy, and notify jobs

## Step 3: Test Local Build (Optional)

Before pushing, you can test the Docker build locally:

```bash
# Build the image
docker build -t gitea.vidoks.fr/sortifal/pfee:test .

# Test run locally
docker run -p 8080:80 -p 3001:3001 gitea.vidoks.fr/sortifal/pfee:test
```

## Step 4: Manual Registry Push (Optional)

If you want to manually push to the registry:

```bash
# Login to registry
docker login gitea.vidoks.fr -u <username>

# Build and tag
docker build -t gitea.vidoks.fr/sortifal/pfee:latest .

# Push to registry
docker push gitea.vidoks.fr/sortifal/pfee:latest
```

## Step 5: Deploy to Kubernetes

### Option A: Using the Deployment Script

```bash
./scripts/deploy.sh gitea.vidoks.fr <username> <password> sortifal/pfee
```

### Option B: Manual Deployment

```bash
# Create namespace
kubectl apply -f k8s/namespace.yaml

# Create registry credentials secret
kubectl create secret docker-registry registry-credentials \
  --docker-server=gitea.vidoks.fr \
  --docker-username=<your-username> \
  --docker-password=<your-password> \
  -n sqdc-dashboard

# Apply manifests
kubectl apply -f k8s/deployment.yaml
kubectl apply -f k8s/service.yaml
kubectl apply -f k8s/ingress.yaml

# Wait for rollout
kubectl rollout status deployment/sqdc-dashboard -n sqdc-dashboard
```

## Step 6: Trigger CI/CD Pipeline

Once secrets are configured, simply push to trigger the pipeline:

```bash
git add .
git commit -m "feat: Configure CI/CD with registry"
git push origin dashboard
```

The workflow will:
1. Install dependencies and run tests
2. Build the React application
3. Build Docker image and push to `gitea.vidoks.fr/sortifal/pfee`
4. Deploy to Kubernetes cluster
5. Update deployment with the new image
6. Verify deployment status

## Monitoring Deployment

### View Workflow Logs

1. Go to your Gitea repository
2. Click on "Actions" tab
3. Select the workflow run
4. View logs for each job (build, deploy, notify)

### Check Kubernetes Status

```bash
# Check pods
kubectl get pods -n sqdc-dashboard

# Check deployment
kubectl get deployment -n sqdc-dashboard

# Check service
kubectl get svc -n sqdc-dashboard

# Check ingress
kubectl get ingress -n sqdc-dashboard

# View logs
kubectl logs -f deployment/sqdc-dashboard -n sqdc-dashboard
```

## Troubleshooting

### Image Pull Errors

If pods show `ImagePullBackOff`:

```bash
# Check if secret exists
kubectl get secret registry-credentials -n sqdc-dashboard

# Describe the secret
kubectl describe secret registry-credentials -n sqdc-dashboard

# Recreate the secret
kubectl delete secret registry-credentials -n sqdc-dashboard
kubectl create secret docker-registry registry-credentials \
  --docker-server=gitea.vidoks.fr \
  --docker-username=<username> \
  --docker-password=<password> \
  -n sqdc-dashboard
```

### Workflow Authentication Errors

If the workflow fails during image push:

1. Verify `REGISTRY_USER` and `REGISTRY_PASSWORD` secrets are correct
2. Ensure the token has `write:package` permission
3. Check registry URL matches exactly: `gitea.vidoks.fr`

### Kubectl Connection Errors

If deployment step fails:

1. Verify `KUBE_CONFIG` secret contains valid kubeconfig
2. Ensure the config is in plain text (not base64 encoded)
3. Check cluster is accessible from Gitea Actions runner

## Next Steps

Once deployment is successful:

1. Access the application via the ingress URL
2. Set up monitoring and alerts
3. Configure backup procedures for the database
4. Review and adjust resource limits based on usage

For detailed documentation, see [DEPLOYMENT.md](DEPLOYMENT.md)
