name: Build and Push Docker Image

on:
  workflow_dispatch:
  release:
    types: [published]

jobs:
  build_matrix:
    name: Build and Push Docker Image
    runs-on: ubuntu-latest

    strategy:
      matrix:
        image: [client, server]

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

      - id: version
        name: App Version
        uses: martinbeentjes/npm-get-version-action@v1.2.3

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2.1.0
        with:
          platforms: amd64

      - id: buildx
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2.3.0

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

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v2.1.0
        with:
          registry: ghcr.io
          username: $GITHUB_REPOSITORY_OWNER
          password: ${{ secrets.GH_TOKEN }}

      - name: Build and Push Docker Image
        uses: docker/build-push-action@v3.3.0
        with:
          context: .
          push: true
          platforms: linux/amd64
          file: ${{ matrix.image }}/Dockerfile
          tags: |
            amruthpillai/reactive-resume:${{ matrix.image }}-latest
            amruthpillai/reactive-resume:${{ matrix.image }}-${{ steps.version.outputs.current-version }}
            ghcr.io/amruthpillai/reactive-resume:${{ matrix.image }}-latest
            ghcr.io/amruthpillai/reactive-resume:${{ matrix.image }}-${{ steps.version.outputs.current-version }}
