when:
  - event: [push, pull_request]

steps:
  backend:
    image: golang:1.25
    commands:
      - cd backend
      - go test ./...
      - go build -o ../dist/osaet-admin ./cmd/osaet-admin

  admin:
    image: node:22-bookworm
    commands:
      - cd frontend/admin
      - npm ci
      - npm run build

  site:
    image: node:22-bookworm
    commands:
      - cd frontend/site
      - npm ci
      - npm run build

  deploy:
    image: alpine:3.20
    when:
      - event: push
        branch: main
    environment:
      SSH_HOST:
        from_secret: deploy_ssh_host
      SSH_USER:
        from_secret: deploy_ssh_user
      SSH_PORT:
        from_secret: deploy_ssh_port
      SSH_PRIVATE_KEY:
        from_secret: deploy_ssh_key
      OSAET_ROOT:
        from_secret: deploy_osaet_root
    commands:
      - apk add --no-cache openssh-client
      - mkdir -p ~/.ssh
      - printf '%s\n' "$SSH_PRIVATE_KEY" > ~/.ssh/id_ed25519
      - chmod 600 ~/.ssh/id_ed25519
      - ssh-keyscan -p "${SSH_PORT:-22}" "$SSH_HOST" >> ~/.ssh/known_hosts
      - ssh -p "${SSH_PORT:-22}" "$SSH_USER@$SSH_HOST" "OSAET_ROOT='${OSAET_ROOT:-/opt/osaet}' bash '${OSAET_ROOT:-/opt/osaet}/app/deploy/deploy.sh'"
