name: Build and push Docker image - Release

on:
  workflow_dispatch:
  release:
    types: [published]

jobs:
  build_and_push_server_monorepo_release:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          ref: 'main'
          fetch-depth: 0

      - name: 'Get Previous tag'
        id: previoustag
        uses: 'WyriHaximus/github-action-get-previous-tag@v1'
        with:
          fallback: latest

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

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

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

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

      - name: Build and push immich-server release
        uses: docker/build-push-action@v3.2.0
        with:
          context: ./server
          file: ./server/Dockerfile
          platforms: linux/arm/v7,linux/amd64,linux/arm64
          push: ${{ github.event_name != 'pull_request' }}
          cache-from: type=gha
          cache-to: type=gha,mode=max
          tags: |
            altran1502/immich-server:${{ steps.previoustag.outputs.tag }}
            altran1502/immich-server:release
            ghcr.io/${{ github.repository_owner }}/immich-server:${{ steps.previoustag.outputs.tag }}
            ghcr.io/${{ github.repository_owner }}/immich-server:release

  build_and_push_machine_learning_release:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: 'Get Previous tag'
        id: previoustag
        uses: 'WyriHaximus/github-action-get-previous-tag@v1'
        with:
          fallback: latest
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2.1.0
      - name: Set up Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@v2.2.1
      - name: Login to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: Login to GitHub Container Registry
        uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Build and Push Machine Learning
        uses: docker/build-push-action@v3.2.0
        with:
          context: ./machine-learning
          file: ./machine-learning/Dockerfile
          platforms: linux/arm/v7,linux/amd64,linux/arm64
          push: true
          cache-from: type=gha
          cache-to: type=gha,mode=max
          tags: |
            altran1502/immich-machine-learning:${{ steps.previoustag.outputs.tag }}
            altran1502/immich-machine-learning:release
            ghcr.io/${{ github.repository_owner }}/immich-machine-learning:${{ steps.previoustag.outputs.tag }}
            ghcr.io/${{ github.repository_owner }}/immich-machine-learning:release

  build_and_push_web_release:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          ref: 'main'
          fetch-depth: 0

      - name: 'Get Previous tag'
        id: previoustag
        uses: 'WyriHaximus/github-action-get-previous-tag@v1'
        with:
          fallback: latest

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

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

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

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

      - name: Build and push immich-web release
        uses: docker/build-push-action@v3.2.0
        with:
          context: ./web
          file: ./web/Dockerfile
          platforms: linux/arm/v7,linux/amd64,linux/arm64
          push: ${{ github.event_name != 'pull_request' }}
          target: prod
          tags: |
            altran1502/immich-web:${{ steps.previoustag.outputs.tag }}
            altran1502/immich-web:release
            ghcr.io/${{ github.repository_owner }}/immich-web:${{ steps.previoustag.outputs.tag }}
            ghcr.io/${{ github.repository_owner }}/immich-web:release

  build_and_push_nginx_release:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          ref: 'main'
          fetch-depth: 0

      - name: 'Get Previous tag'
        id: previoustag
        uses: 'WyriHaximus/github-action-get-previous-tag@v1'
        with:
          fallback: latest

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

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

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

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

      - name: Build and push immich-proxy release
        uses: docker/build-push-action@v3.2.0
        with:
          context: ./nginx
          file: ./nginx/Dockerfile
          platforms: linux/arm/v7,linux/amd64,linux/arm64
          push: ${{ github.event_name != 'pull_request' }}
          tags: |
            altran1502/immich-proxy:release
            altran1502/immich-proxy:${{ steps.previoustag.outputs.tag }}
            ghcr.io/${{ github.repository_owner }}/immich-proxy:${{ steps.previoustag.outputs.tag }}
            ghcr.io/${{ github.repository_owner }}/immich-proxy:release