name: Deploy to Remote Server

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Deploy with Docker Compose
        env:
            DEPLOY_KEY: ${{secrets.DEPLOY_KEY}}
            REMOTE_HOST: ${{ secrets.REMOTE_HOST}}
            REMOTE_USER: ${{ secrets.REMOTE_USER}}
            DC_FILE: ${{ secrets.DOCKER_COMPOSE_FILE }}
        run: |
          mkdir -p ~/.ssh
          echo "$DEPLOY_KEY" > ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa
          ssh-keyscan -H $REMOTE_HOST >> ~/.ssh/known_hosts
          CMD="docker compose -f $DC_FILE build --no-cache &&  docker compose -f $DC_FILE up -d"
          echo $CMD
          ssh -i ~/.ssh/id_rsa $REMOTE_USER@$REMOTE_HOST $CMD