name: Build and push docker image and deploy

on:
  push:
    branches:
      - main

jobs:
  build_and_push:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Log in to Github Container registry
        uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.DOCKER_REGISTRY_TOKEN }}

      - name: Build and push Docker image
        uses: docker/build-push-action@v4
        with:
          context: .
          push: true
          tags: ghcr.io/${{ github.actor }}/skyjo-fe:latest

  deploy:
    needs: build_and_push
    if: ${{ success() }}
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - uses: appleboy/ssh-action@v1.0.0
      name: Execute deploy script on host
      with:
        host: ${{ secrets.DEPLOY_HOST }}
        username: ${{ secrets.DEPLOY_USER }}
        key: ${{ secrets.DEPLOY_KEY }}
        port: 22
        script: ~/deployments/github-pb-coding-skyjo-fe-deploy.sh ${{ secrets.DOCKER_REGISTRY_TOKEN }}
