name: Branch Build

on:
  workflow_dispatch:
    inputs:
      branch_name:
        description: "Branch Name"
        required: true
        default: "preview"
  push:
    branches:
      - master
      - preview
      - develop
  release:
    types: [released, prereleased]

env:
  TARGET_BRANCH: ${{ inputs.branch_name || github.ref_name || github.event.release.target_commitish }}

jobs:
  branch_build_setup:
    name: Build-Push Web/Space/API/Proxy Docker Image
    runs-on: ubuntu-20.04
    steps:
      - name: Check out the repo
        uses: actions/checkout@v3.3.0
    outputs:
      gh_branch_name: ${{ env.TARGET_BRANCH }}

  branch_build_push_frontend:
    runs-on: ubuntu-20.04
    needs: [branch_build_setup]
    env:
      FRONTEND_TAG: ${{ secrets.DOCKERHUB_USERNAME }}/plane-frontend:${{ needs.branch_build_setup.outputs.gh_branch_name }}
    steps:
      - name: Set Frontend Docker Tag
        run: |
          if [ "${{ needs.branch_build_setup.outputs.gh_branch_name }}" == "master" ] && [ "${{ github.event_name }}" == "release" ]; then
            TAG=${{ secrets.DOCKERHUB_USERNAME }}/plane-frontend:latest,${{ secrets.DOCKERHUB_USERNAME }}/plane-frontend:${{ github.event.release.tag_name }}
          elif [ "${{ needs.branch_build_setup.outputs.gh_branch_name }}" == "master" ]; then
            TAG=${{ secrets.DOCKERHUB_USERNAME }}/plane-frontend:stable
          else
            TAG=${{ env.FRONTEND_TAG }}
          fi
          echo "FRONTEND_TAG=${TAG}" >> $GITHUB_ENV
      - name: Docker Setup QEMU
        uses: docker/setup-qemu-action@v3.0.0

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3.0.0
        with:
          platforms: linux/amd64,linux/arm64
          buildkitd-flags: "--allow-insecure-entitlement security.insecure"

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

      - name: Check out the repo
        uses: actions/checkout@v4.1.1

      - name: Build and Push Frontend to Docker Container Registry
        uses: docker/build-push-action@v5.1.0
        with:
          context: .
          file: ./web/Dockerfile.web
          platforms: linux/amd64
          tags: ${{ env.FRONTEND_TAG }}
          push: true
        env:
          DOCKER_BUILDKIT: 1
          DOCKER_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
          DOCKER_PASSWORD: ${{ secrets.DOCKERHUB_TOKEN }}

  branch_build_push_space:
    runs-on: ubuntu-20.04
    needs: [branch_build_setup]
    env:
      SPACE_TAG: ${{ secrets.DOCKERHUB_USERNAME }}/plane-space:${{ needs.branch_build_setup.outputs.gh_branch_name }}
    steps:
      - name: Set Space Docker Tag
        run: |
          if [ "${{ needs.branch_build_setup.outputs.gh_branch_name }}" == "master" ] && [ "${{ github.event_name }}" == "release" ]; then
            TAG=${{ secrets.DOCKERHUB_USERNAME }}/plane-space:latest,${{ secrets.DOCKERHUB_USERNAME }}/plane-space:${{ github.event.release.tag_name }}
          elif [ "${{ needs.branch_build_setup.outputs.gh_branch_name }}" == "master" ]; then
            TAG=${{ secrets.DOCKERHUB_USERNAME }}/plane-space:stable
          else
            TAG=${{ env.SPACE_TAG }}
          fi
          echo "SPACE_TAG=${TAG}" >> $GITHUB_ENV

      - name: Docker Setup QEMU
        uses: docker/setup-qemu-action@v3.0.0

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3.0.0
        with:
          platforms: linux/amd64,linux/arm64
          buildkitd-flags: "--allow-insecure-entitlement security.insecure"

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

      - name: Check out the repo
        uses: actions/checkout@v4.1.1

      - name: Build and Push Space to Docker Hub
        uses: docker/build-push-action@v5.1.0
        with:
          context: .
          file: ./space/Dockerfile.space
          platforms: linux/amd64
          tags: ${{ env.SPACE_TAG }}
          push: true
        env:
          DOCKER_BUILDKIT: 1
          DOCKER_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
          DOCKER_PASSWORD: ${{ secrets.DOCKERHUB_TOKEN }}

  branch_build_push_backend:
    runs-on: ubuntu-20.04
    needs: [branch_build_setup]
    env:
      BACKEND_TAG: ${{ secrets.DOCKERHUB_USERNAME }}/plane-backend:${{ needs.branch_build_setup.outputs.gh_branch_name }}
    steps:
      - name: Set Backend Docker Tag
        run: |
          if [ "${{ needs.branch_build_setup.outputs.gh_branch_name }}" == "master" ] && [ "${{ github.event_name }}" == "release" ]; then
            TAG=${{ secrets.DOCKERHUB_USERNAME }}/plane-backend:latest,${{ secrets.DOCKERHUB_USERNAME }}/plane-backend:${{ github.event.release.tag_name }}
          elif [ "${{ needs.branch_build_setup.outputs.gh_branch_name }}" == "master" ]; then
            TAG=${{ secrets.DOCKERHUB_USERNAME }}/plane-backend:stable
          else
            TAG=${{ env.BACKEND_TAG }}
          fi
          echo "BACKEND_TAG=${TAG}" >> $GITHUB_ENV

      - name: Docker Setup QEMU
        uses: docker/setup-qemu-action@v3.0.0

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3.0.0
        with:
          platforms: linux/amd64,linux/arm64
          buildkitd-flags: "--allow-insecure-entitlement security.insecure"

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

      - name: Check out the repo
        uses: actions/checkout@v4.1.1

      - name: Build and Push Backend to Docker Hub
        uses: docker/build-push-action@v5.1.0
        with:
          context: ./apiserver
          file: ./apiserver/Dockerfile.api
          platforms: linux/amd64
          push: true
          tags: ${{ env.BACKEND_TAG }}
        env:
          DOCKER_BUILDKIT: 1
          DOCKER_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
          DOCKER_PASSWORD: ${{ secrets.DOCKERHUB_TOKEN }}

  branch_build_push_proxy:
    runs-on: ubuntu-20.04
    needs: [branch_build_setup]
    env:
      PROXY_TAG: ${{ secrets.DOCKERHUB_USERNAME }}/plane-proxy:${{ needs.branch_build_setup.outputs.gh_branch_name }}
    steps:
      - name: Set Proxy Docker Tag
        run: |
          if [ "${{ needs.branch_build_setup.outputs.gh_branch_name }}" == "master" ] && [ "${{ github.event_name }}" == "release" ]; then
            TAG=${{ secrets.DOCKERHUB_USERNAME }}/plane-proxy:latest,${{ secrets.DOCKERHUB_USERNAME }}/plane-proxy:${{ github.event.release.tag_name }}
          elif [ "${{ needs.branch_build_setup.outputs.gh_branch_name }}" == "master" ]; then
            TAG=${{ secrets.DOCKERHUB_USERNAME }}/plane-proxy:stable
          else
            TAG=${{ env.PROXY_TAG }}
          fi
          echo "PROXY_TAG=${TAG}" >> $GITHUB_ENV

      - name: Docker Setup QEMU
        uses: docker/setup-qemu-action@v3.0.0

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3.0.0
        with:
          platforms: linux/amd64,linux/arm64
          buildkitd-flags: "--allow-insecure-entitlement security.insecure"

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

      - name: Check out the repo
        uses: actions/checkout@v4.1.1

      - name: Build and Push Plane-Proxy to Docker Hub
        uses: docker/build-push-action@v5.1.0
        with:
          context: ./nginx
          file: ./nginx/Dockerfile
          platforms: linux/amd64
          tags: ${{ env.PROXY_TAG }}
          push: true
        env:
          DOCKER_BUILDKIT: 1
          DOCKER_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
          DOCKER_PASSWORD: ${{ secrets.DOCKERHUB_TOKEN }}