# 🚀 Deployment Guide

> **آخرین بروزرسانی**: January 3, 2026  
> **Environment**: Kubernetes on Stage  
> **Domain**: `*.se.kbs1.ir`

---

## 🌐 آدرس‌های سرویس‌ها

| سرویس | آدرس Stage |
|-------|------------|
| BackOffice UI | `https://backoffice.se.kbs1.ir` |
| BackOffice BFF | `https://backoffice-bff.se.kbs1.ir` |
| FrontOffice UI | `https://frontoffice.se.kbs1.ir` |
| FrontOffice BFF | `https://frontoffice-bff.se.kbs1.ir` |
| CMS | `https://cms.se.kbs1.ir` |
| Git Registry | `git.se.kbs1.ir` |

---

## 📦 Git Repositories

| Repo | Remote | URL |
|------|--------|-----|
| BackOffice | kub-stage | `https://git.se.kbs1.ir/admin/BackOffice.git` |
| BackOffice.BFF | kub-stage | `https://git.se.kbs1.ir/admin/BackOffice.BFF.git` |
| FrontOffice | kub-stage | `https://git.se.kbs1.ir/admin/FrontOffice.git` |
| FrontOffice.BFF | kub-stage | `https://git.se.kbs1.ir/admin/FrontOffice.BFF.git` |
| CMS | gitea | `https://git.se.kbs1.ir/admin/CMS.git` |
| Docs | foursatDocs | `https://git.se.kbs1.ir/FourSat/docs.git` |

---

## 🔄 CI/CD Workflow

### Gitea Actions

هر ریپو دارای workflow در `.gitea/workflows/` است:

```yaml
# kub-deploy.yml - Deploy to Stage
# prod-deploy.yml - Deploy to Production
```

### مراحل Deploy:

```
1. Push to branch → Trigger workflow
2. Build Docker image
3. Push to Registry (git.se.kbs1.ir)
4. Deploy to Kubernetes
```

---

## 🐳 Docker Registry

### تنظیمات:

```yaml
env:
  EXTERNAL_REGISTRY: git.se.kbs1.ir
  IMAGE: backoffice  # or backoffice-bff, cms, etc.

# Docker daemon config
daemon.json: |
  {
    "insecure-registries": ["git.se.kbs1.ir", "gitea-svc:3000"]
  }
```

### Image Names:

| سرویس | Image |
|-------|-------|
| BackOffice | `git.se.kbs1.ir/admin/backoffice:latest` |
| BackOffice.BFF | `git.se.kbs1.ir/admin/backoffice-bff:latest` |
| FrontOffice | `git.se.kbs1.ir/admin/frontoffice:latest` |
| FrontOffice.BFF | `git.se.kbs1.ir/admin/frontoffice-bff:latest` |
| CMS | `git.se.kbs1.ir/admin/cms:latest` |

---

## 📋 BackOffice Deployment

### Prerequisites:

```bash
# 1. Build proto DLLs
cd BackOffice
./build-deps.sh

# 2. Verify DLLs
ls libs/*.dll | wc -l  # Should be 24
```

### Build & Publish:

```bash
cd BackOffice/src
dotnet publish BackOffice/BackOffice.csproj -c Release -o ./publish
```

### Docker Build:

```bash
cd BackOffice
docker build -t backoffice:latest .
```

### Dockerfile:

```dockerfile
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
WORKDIR /src

# Copy pre-built proto DLLs
COPY ["libs/", "libs/"]

COPY ["src/BackOffice/", "BackOffice/"]
RUN dotnet publish "BackOffice/BackOffice.csproj" -c Release -o /app/publish

FROM nginx:alpine
COPY --from=build /app/publish/wwwroot /usr/share/nginx/html
EXPOSE 80
```

---

## 📋 BFF & CMS Deployment

### Standard .NET Web API:

```bash
# Build
cd BackOffice.BFF/src
dotnet publish BackOffice.BFF.WebApi/BackOffice.BFF.WebApi.csproj -c Release -o ./publish

# Docker
docker build -t backoffice-bff:latest .
```

### Dockerfile Template:

```dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS runtime
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build

WORKDIR /src
COPY . .
RUN dotnet publish -c Release -o /app

FROM runtime
WORKDIR /app
COPY --from=build /app .
EXPOSE 80
ENTRYPOINT ["dotnet", "BackOffice.BFF.WebApi.dll"]
```

---

## ⚙️ Configuration

### appsettings.Staging.json (BackOffice):

```json
{
  "Environment": "Staging",
  "GwUrl": "https://backoffice-bff.se.kbs1.ir",
  "AllowedHosts": "*"
}
```

### appsettings.Staging.json (FrontOffice):

```json
{
  "GwUrl": "https://frontoffice-bff.se.kbs1.ir",
  "AllowedHosts": "*"
}
```

### appsettings.json (BFF):

```json
{
  "ConnectionStrings": {
    "DefaultConnection": "Host=postgres;Database=cms;Username=xxx;Password=xxx"
  },
  "CMSMSAddress": "https://cms.se.kbs1.ir",
  "Jwt": {
    "Secret": "xxx",
    "Issuer": "FourSat",
    "Audience": "FourSat"
  }
}
```

---

## 🔧 Kubernetes Resources

### Deployment Template:

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: backoffice
spec:
  replicas: 2
  selector:
    matchLabels:
      app: backoffice
  template:
    spec:
      containers:
      - name: backoffice
        image: git.se.kbs1.ir/admin/backoffice:latest
        ports:
        - containerPort: 80
        env:
        - name: ASPNETCORE_ENVIRONMENT
          value: "Staging"
```

### Service Template:

```yaml
apiVersion: v1
kind: Service
metadata:
  name: backoffice-svc
spec:
  selector:
    app: backoffice
  ports:
  - port: 80
    targetPort: 80
```

### Ingress Template:

```yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: backoffice-ingress
spec:
  rules:
  - host: backoffice.se.kbs1.ir
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: backoffice-svc
            port:
              number: 80
```

---

## 🏥 Health Checks

### CMS Health Endpoints:

| Endpoint | استفاده |
|----------|---------|
| `/health` | وضعیت کلی |
| `/health/ready` | Readiness probe |
| `/health/live` | Liveness probe |

### Kubernetes Probes:

```yaml
livenessProbe:
  httpGet:
    path: /health/live
    port: 80
  initialDelaySeconds: 30
  periodSeconds: 10

readinessProbe:
  httpGet:
    path: /health/ready
    port: 80
  initialDelaySeconds: 5
  periodSeconds: 5
```

---

## 📊 Monitoring

### Hangfire Dashboard:
```
https://cms.se.kbs1.ir/hangfire
```

### Logs:
```bash
# Kubernetes logs
kubectl logs -f deployment/cms -n foursat

# Docker logs
docker logs -f cms
```

---

## 🔄 Deploy Process

### 1. Manual Deploy:

```bash
# BackOffice
cd BackOffice
./build-deps.sh
git add .
git commit -m "Deploy: v1.x.x"
git push kub-stage main

# BFF/CMS
cd BackOffice.BFF
git add .
git commit -m "Deploy: v1.x.x"
git push kub-stage main
```

### 2. Verify Deployment:

```bash
# Check pods
kubectl get pods -n foursat

# Check services
kubectl get svc -n foursat

# Check ingress
kubectl get ingress -n foursat
```

### 3. Rollback:

```bash
# Rollback to previous version
kubectl rollout undo deployment/backoffice -n foursat
```

---

## 🛠️ Troubleshooting

### Build Failed:

```bash
# Check proto DLLs
ls BackOffice/libs/*.dll | wc -l  # Should be 24

# Rebuild
./build-deps.sh
```

### Image Push Failed:

```bash
# Login to registry
docker login git.se.kbs1.ir

# Check daemon.json
cat /etc/docker/daemon.json
```

### Pod CrashLoopBackOff:

```bash
# Check logs
kubectl logs pod-name -n foursat

# Check events
kubectl describe pod pod-name -n foursat
```

---

## 📚 مستندات مرتبط

- [BackOffice Documentation](./01-BACKOFFICE.md)
- [Proto Guide](./02-PROTO-GUIDE.md)
- [CMS Documentation](./04-CMS.md)
