# Deployment Scripts

All scripts are in `scripts/` at the project root. Each script is POSIX-compatible (`bash` with `set -euo pipefail`), sources `.env` from the project root, and supports `--help`.

## Script Reference

| Script | Purpose | Key Env Vars |
|--------|---------|-------------|
| `deploy.sh` | Orchestrates full deployment | REGISTRY, IMAGE_TAG, DEPLOY_HOST, DEPLOY_USER |
| `pull-images.sh` | Pulls Docker images from registry | REGISTRY, IMAGE_TAG |
| `start-services.sh` | Starts the detections container | REGISTRY, IMAGE_TAG, LOADER_URL, ANNOTATIONS_URL |
| `stop-services.sh` | Gracefully stops and removes container | — |
| `health-check.sh` | Verifies `/health` endpoint | HEALTH_CHECK_HOST, HEALTH_CHECK_PORT |

## Usage

### Full Deployment

```bash
REGISTRY=registry.example.com IMAGE_TAG=a1b2c3d bash scripts/deploy.sh
```

### Remote Deployment (via SSH)

```bash
REGISTRY=registry.example.com \
IMAGE_TAG=a1b2c3d \
DEPLOY_HOST=production-server.example.com \
DEPLOY_USER=deploy \
bash scripts/deploy.sh
```

### Rollback

```bash
bash scripts/deploy.sh --rollback
```

Redeploys the previous image tag (saved in `.deploy-previous-tag` by `stop-services.sh`).

### Local Development

```bash
docker compose -f docker-compose.yml up
```

Or using the e2e compose for testing:

```bash
cd e2e && COMPOSE_PROFILES=cpu docker compose -f docker-compose.test.yml up --build
```

### Jetson Orin Nano Deployment (AZ-180)

Jetson deployment uses `docker-compose.jetson.yml` directly — the standard CPU/GPU scripts are not used on Jetson.

**Prerequisites on target Jetson device**:
- JetPack 6.x installed
- NVIDIA Container Runtime configured (`runtime: nvidia` support in Docker)
- `docker compose` v2+

**Start service**:
```bash
docker compose -f docker-compose.jetson.yml up -d
```

**Stop service**:
```bash
docker compose -f docker-compose.jetson.yml down
```

**INT8 calibration cache**: Upload `azaion.int8_calib.cache` to the Loader service before first start to enable INT8 precision. If absent, the service falls back to FP16 automatically.

## Deployment Flow

```
deploy.sh
  ├── pull-images.sh    → docker pull
  ├── stop-services.sh  → docker stop (30s grace) + save previous tag
  ├── start-services.sh → docker run
  └── health-check.sh   → curl /health (10 retries, 3s interval)
```

## Environment Variables

All scripts source `.env` from the project root if it exists. Variables can also be passed directly via the environment. See `.env.example` for the full list.
