name: Build and Push WEB Image

on:
  push:
    branches:
      - 'main'
      - 'deploy/dev'
  pull_request:
    types: [synchronize, opened, reopened, ready_for_review]

jobs:
  build-and-push:
    runs-on: ubuntu-latest
    if: github.event.pull_request.draft == false
    steps:
    - name: "Checkout ${{ github.ref }} ( ${{ github.sha }} )"
      uses: actions/checkout@v2
      with:
        persist-credentials: false

    - name: Login to Docker Hub
      uses: docker/login-action@v2
      with:
        username: ${{ secrets.DOCKERHUB_USER }}
        password: ${{ secrets.DOCKERHUB_TOKEN }}

    - name: Build and push Docker image
      shell: bash
      env:
        DOCKERHUB_USER: ${{ secrets.DOCKERHUB_USER }}
        DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
      run: |
        /bin/bash .github/workflows/build-web-image.sh

    - name: Deploy to server
      if: github.ref == 'refs/heads/deploy/dev'
      uses: appleboy/ssh-action@v0.1.8
      with:
        host: ${{ secrets.SSH_HOST }}
        username: ${{ secrets.SSH_USER }}
        key: ${{ secrets.SSH_PRIVATE_KEY }}
        script: |
          ${{ secrets.SSH_SCRIPT }}
