name: Deploy Catalogs

on:
  push:
    branches: master
    paths:
      - 'catalogs/**'

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v1

      - name: Create Docker image
        run: |
          docker build -t torrentio-catalogs:latest . -f ./catalogs/Dockerfile
          docker save torrentio-catalogs:latest > torrentio_catalogs_latest.tar

      - name: Upload Docker image
        uses: appleboy/scp-action@master
        with:
          host: ${{ secrets.SSH_HOST_3 }}
          username: ${{ secrets.SSH_USERNAME }}
          port: ${{ secrets.SSH_PORT }}
          key: ${{ secrets.SSH_KEY_3 }}
          source: torrentio_catalogs_latest.tar
          target: /tmp/docker
          overwrite: true

      - name: Deploy Docker image
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.SSH_HOST_3 }}
          username: ${{ secrets.SSH_USERNAME }}
          port: ${{ secrets.SSH_PORT }}
          key: ${{ secrets.SSH_KEY_3 }}
          script: |
            docker load -i /tmp/docker/torrentio_catalogs_latest.tar
            docker stop torrentio-catalogs
            docker rm torrentio-catalogs
            docker run -p 9000:7000 -d --name torrentio-catalogs --restart always --log-opt max-size=100m -e MONGODB_URI=${{ secrets.MONGODB_URI }} -e DATABASE_URI=${{ secrets.DATABASE_URI }} torrentio-catalogs:latest
            docker image prune -f
