name: Update Docker Images for Plane on Release

on:
  release:
    types: [released, prereleased]

jobs:
  build_push_backend:
    name: Build and Push Api Server Docker Image
    runs-on: ubuntu-20.04

    steps:
      - name: Check out the repo
        uses: actions/checkout@v3.3.0

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

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

      - name: Extract metadata (tags, labels) for Docker (Docker Hub) from Github Release
        id: metaFrontend
        uses: docker/metadata-action@v4.3.0
        with:
          images: ${{ secrets.DOCKERHUB_USERNAME }}/plane-frontend
          tags: |
            type=ref,event=tag
      
      - name: Extract metadata (tags, labels) for Docker (Docker Hub) from Github Release
        id: metaBackend
        uses: docker/metadata-action@v4.3.0
        with:
          images: ${{ secrets.DOCKERHUB_USERNAME }}/plane-backend
          tags: |
            type=ref,event=tag

      - name: Extract metadata (tags, labels) for Docker (Docker Hub) from Github Release
        id: metaSpace
        uses: docker/metadata-action@v4.3.0
        with:
          images: ${{ secrets.DOCKERHUB_USERNAME }}/plane-space
          tags: |
            type=ref,event=tag

      - name: Extract metadata (tags, labels) for Docker (Docker Hub) from Github Release
        id: metaProxy
        uses: docker/metadata-action@v4.3.0
        with:
          images: ${{ secrets.DOCKERHUB_USERNAME }}/plane-proxy
          tags: |
            type=ref,event=tag

      - name: Build and Push Frontend to Docker Container Registry
        uses: docker/build-push-action@v4.0.0
        with:
          context: .
          file: ./web/Dockerfile.web
          platforms: linux/amd64
          tags: ${{ steps.metaFrontend.outputs.tags }}
          push: true
        env:
          DOCKER_BUILDKIT: 1
          DOCKER_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
          DOCKET_PASSWORD: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Build and Push Backend to Docker Hub
        uses: docker/build-push-action@v4.0.0
        with:
          context: ./apiserver
          file: ./apiserver/Dockerfile.api
          platforms: linux/amd64
          push: true
          tags: ${{ steps.metaBackend.outputs.tags }}
        env:
          DOCKER_BUILDKIT: 1
          DOCKER_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
          DOCKET_PASSWORD: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Build and Push Plane-Deploy to Docker Hub
        uses: docker/build-push-action@v4.0.0
        with:
          context: .
          file: ./space/Dockerfile.space
          platforms: linux/amd64
          push: true
          tags: ${{ steps.metaSpace.outputs.tags }}
        env:
          DOCKER_BUILDKIT: 1
          DOCKER_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
          DOCKET_PASSWORD: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Build and Push Plane-Proxy to Docker Hub
        uses: docker/build-push-action@v4.0.0
        with:
          context: ./nginx
          file: ./nginx/Dockerfile
          platforms: linux/amd64
          push: true
          tags: ${{ steps.metaProxy.outputs.tags }}
        env:
          DOCKER_BUILDKIT: 1
          DOCKER_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
          DOCKET_PASSWORD: ${{ secrets.DOCKERHUB_TOKEN }}
