name: Master Build

on:
  push:
    branches: [ "master" ]

jobs:
  build_amd64:
    name: Build and Push Docker Image (amd64)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Login to Docker Hub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_ACCESS_TOKEN }}

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v1

      - name: Set up Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@v1

      - name: Available platforms
        run: echo ${{ steps.buildx.outputs.platforms }}

      - name: Build Docker image and push
        id: docker_build
        uses: docker/build-push-action@v2
        with:
          context: ./
          file: ./Dockerfile
          push: true
          tags: ${{ secrets.DOCKER_USERNAME }}/kasbot:latest
          platforms: linux/amd64

      - name: Image digest
        run: echo ${{ steps.docker_build.outputs.digest }}
        
  build_arm64:
    name: Build and Push Docker Image (arm64)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Login to Docker Hub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_ACCESS_TOKEN }}

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v1

      - name: Set up Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@v1

      - name: Available platforms
        run: echo ${{ steps.buildx.outputs.platforms }}

      - name: Build Docker image and push
        id: docker_build
        uses: docker/build-push-action@v2
        with:
          context: ./
          file: ./Dockerfile
          push: true
          tags: ${{ secrets.DOCKER_USERNAME }}/kasbot:latest
          platforms: linux/arm64

      - name: Image digest
        run: echo ${{ steps.docker_build.outputs.digest }}

        
  deploy:
    name: Update running container
    runs-on: ubuntu-latest
    needs: build_arm64

    steps:
    - name: Recreate container
      uses: appleboy/ssh-action@v0.1.7
      with:
        host: ${{ secrets.HOST }}
        username: ${{ secrets.USERNAME }}
        key: ${{ secrets.KEY }}
        port: ${{ secrets.PORT }}
        script: |
          cd docker/kasbot
          docker-compose down
          docker-compose rm
          docker-compose pull
          docker-compose up -d
        