name: Branch Build CE

on:
  workflow_dispatch:
    inputs:
      build_type:
        description: "Type of build to run"
        required: true
        type: choice
        default: "Build"
        options:
          - "Build"
          - "Release"
      releaseVersion:
        description: "Release Version"
        type: string
        default: v0.0.0
      isPrerelease:
        description: "Is Pre-release"
        type: boolean
        default: false
        required: true
      arm64:
        description: "Build for ARM64 architecture"
        required: false
        default: false
        type: boolean
      aio_build:
        description: "Build for AIO docker image"
        required: false
        default: false
        type: boolean
  push:
    branches:
      - preview
      - canary

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

env:
  TARGET_BRANCH: ${{ github.ref_name }}
  ARM64_BUILD: ${{ github.event.inputs.arm64 }}
  BUILD_TYPE: ${{ github.event.inputs.build_type }}
  RELEASE_VERSION: ${{ github.event.inputs.releaseVersion }}
  IS_PRERELEASE: ${{ github.event.inputs.isPrerelease }}
  AIO_BUILD: ${{ github.event.inputs.aio_build }}

jobs:
  branch_build_setup:
    name: Build Setup
    runs-on: ubuntu-22.04
    outputs:
      gh_branch_name: ${{ steps.set_env_variables.outputs.TARGET_BRANCH }}
      gh_buildx_driver: ${{ steps.set_env_variables.outputs.BUILDX_DRIVER }}
      gh_buildx_version: ${{ steps.set_env_variables.outputs.BUILDX_VERSION }}
      gh_buildx_platforms: ${{ steps.set_env_variables.outputs.BUILDX_PLATFORMS }}
      gh_buildx_endpoint: ${{ steps.set_env_variables.outputs.BUILDX_ENDPOINT }}

      dh_img_web: ${{ steps.set_env_variables.outputs.DH_IMG_WEB }}
      dh_img_space: ${{ steps.set_env_variables.outputs.DH_IMG_SPACE }}
      dh_img_admin: ${{ steps.set_env_variables.outputs.DH_IMG_ADMIN }}
      dh_img_live: ${{ steps.set_env_variables.outputs.DH_IMG_LIVE }}
      dh_img_backend: ${{ steps.set_env_variables.outputs.DH_IMG_BACKEND }}
      dh_img_proxy: ${{ steps.set_env_variables.outputs.DH_IMG_PROXY }}
      dh_img_aio: ${{ steps.set_env_variables.outputs.DH_IMG_AIO }}

      build_type: ${{steps.set_env_variables.outputs.BUILD_TYPE}}
      build_release: ${{ steps.set_env_variables.outputs.BUILD_RELEASE }}
      build_prerelease: ${{ steps.set_env_variables.outputs.BUILD_PRERELEASE }}
      release_version: ${{ steps.set_env_variables.outputs.RELEASE_VERSION }}
      aio_build: ${{ steps.set_env_variables.outputs.AIO_BUILD }}

    steps:
      - id: set_env_variables
        name: Set Environment Variables
        run: |
          if [ "${{ env.ARM64_BUILD }}" == "true" ] || ([ "${{ env.BUILD_TYPE }}" == "Release" ] && [ "${{ env.IS_PRERELEASE }}" != "true" ]); then
            echo "BUILDX_DRIVER=cloud" >> $GITHUB_OUTPUT
            echo "BUILDX_VERSION=lab:latest" >> $GITHUB_OUTPUT
            echo "BUILDX_PLATFORMS=linux/amd64,linux/arm64" >> $GITHUB_OUTPUT
            echo "BUILDX_ENDPOINT=makeplane/plane-dev" >> $GITHUB_OUTPUT
          else
            echo "BUILDX_DRIVER=docker-container" >> $GITHUB_OUTPUT
            echo "BUILDX_VERSION=latest" >> $GITHUB_OUTPUT
            echo "BUILDX_PLATFORMS=linux/amd64" >> $GITHUB_OUTPUT
            echo "BUILDX_ENDPOINT=" >> $GITHUB_OUTPUT
          fi
          BR_NAME=$( echo "${{ env.TARGET_BRANCH }}" |sed 's/[^a-zA-Z0-9.-]//g')
          echo "TARGET_BRANCH=$BR_NAME" >> $GITHUB_OUTPUT

          echo "DH_IMG_WEB=plane-frontend" >> $GITHUB_OUTPUT
          echo "DH_IMG_SPACE=plane-space" >> $GITHUB_OUTPUT
          echo "DH_IMG_ADMIN=plane-admin" >> $GITHUB_OUTPUT
          echo "DH_IMG_LIVE=plane-live" >> $GITHUB_OUTPUT
          echo "DH_IMG_BACKEND=plane-backend" >> $GITHUB_OUTPUT
          echo "DH_IMG_PROXY=plane-proxy" >> $GITHUB_OUTPUT
          echo "DH_IMG_AIO=plane-aio-community" >> $GITHUB_OUTPUT

          echo "BUILD_TYPE=${{env.BUILD_TYPE}}" >> $GITHUB_OUTPUT
          BUILD_RELEASE=false
          BUILD_PRERELEASE=false
          RELVERSION="latest"

          BUILD_AIO=${{ env.AIO_BUILD }}

          if [ "${{ env.BUILD_TYPE }}" == "Release" ]; then
            FLAT_RELEASE_VERSION=$(echo "${{ env.RELEASE_VERSION }}" | sed 's/[^a-zA-Z0-9.-]//g')
            echo "FLAT_RELEASE_VERSION=${FLAT_RELEASE_VERSION}" >> $GITHUB_OUTPUT

            semver_regex="^v([0-9]+)\.([0-9]+)\.([0-9]+)(-[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*)?$"
            if [[ ! $FLAT_RELEASE_VERSION =~ $semver_regex ]]; then
              echo "Invalid Release Version Format : $FLAT_RELEASE_VERSION"
              echo "Please provide a valid SemVer version"
              echo "e.g. v1.2.3 or v1.2.3-alpha-1"
              echo "Exiting the build process"
              exit 1  # Exit with status 1 to fail the step
            fi
            BUILD_RELEASE=true
            RELVERSION=$FLAT_RELEASE_VERSION

            if [ "${{ env.IS_PRERELEASE }}" == "true" ]; then
              BUILD_PRERELEASE=true
            fi

            BUILD_AIO=true
          fi

          echo "BUILD_RELEASE=${BUILD_RELEASE}" >> $GITHUB_OUTPUT
          echo "BUILD_PRERELEASE=${BUILD_PRERELEASE}" >> $GITHUB_OUTPUT
          echo "RELEASE_VERSION=${RELVERSION}" >> $GITHUB_OUTPUT
          echo "AIO_BUILD=${BUILD_AIO}" >> $GITHUB_OUTPUT

      - id: checkout_files
        name: Checkout Files
        uses: actions/checkout@v6

  branch_build_push_admin:
    name: Build-Push Admin Docker Image
    runs-on: ubuntu-22.04
    needs: [branch_build_setup]
    steps:
      - name: Admin Build and Push
        uses: makeplane/actions/build-push@v1.4.0
        with:
          build-release: ${{ needs.branch_build_setup.outputs.build_release }}
          build-prerelease: ${{ needs.branch_build_setup.outputs.build_prerelease }}
          release-version: ${{ needs.branch_build_setup.outputs.release_version }}
          dockerhub-username: ${{ secrets.DOCKERHUB_USERNAME }}
          dockerhub-token: ${{ secrets.DOCKERHUB_TOKEN }}
          docker-image-owner: makeplane
          docker-image-name: ${{ needs.branch_build_setup.outputs.dh_img_admin }}
          build-context: .
          dockerfile-path: ./apps/admin/Dockerfile.admin
          buildx-driver: ${{ needs.branch_build_setup.outputs.gh_buildx_driver }}
          buildx-version: ${{ needs.branch_build_setup.outputs.gh_buildx_version }}
          buildx-platforms: ${{ needs.branch_build_setup.outputs.gh_buildx_platforms }}
          buildx-endpoint: ${{ needs.branch_build_setup.outputs.gh_buildx_endpoint }}

  branch_build_push_web:
    name: Build-Push Web Docker Image
    runs-on: ubuntu-22.04
    needs: [branch_build_setup]
    steps:
      - name: Web Build and Push
        uses: makeplane/actions/build-push@v1.4.0
        with:
          build-release: ${{ needs.branch_build_setup.outputs.build_release }}
          build-prerelease: ${{ needs.branch_build_setup.outputs.build_prerelease }}
          release-version: ${{ needs.branch_build_setup.outputs.release_version }}
          dockerhub-username: ${{ secrets.DOCKERHUB_USERNAME }}
          dockerhub-token: ${{ secrets.DOCKERHUB_TOKEN }}
          docker-image-owner: makeplane
          docker-image-name: ${{ needs.branch_build_setup.outputs.dh_img_web }}
          build-context: .
          dockerfile-path: ./apps/web/Dockerfile.web
          buildx-driver: ${{ needs.branch_build_setup.outputs.gh_buildx_driver }}
          buildx-version: ${{ needs.branch_build_setup.outputs.gh_buildx_version }}
          buildx-platforms: ${{ needs.branch_build_setup.outputs.gh_buildx_platforms }}
          buildx-endpoint: ${{ needs.branch_build_setup.outputs.gh_buildx_endpoint }}

  branch_build_push_space:
    name: Build-Push Space Docker Image
    runs-on: ubuntu-22.04
    needs: [branch_build_setup]
    steps:
      - name: Space Build and Push
        uses: makeplane/actions/build-push@v1.4.0
        with:
          build-release: ${{ needs.branch_build_setup.outputs.build_release }}
          build-prerelease: ${{ needs.branch_build_setup.outputs.build_prerelease }}
          release-version: ${{ needs.branch_build_setup.outputs.release_version }}
          dockerhub-username: ${{ secrets.DOCKERHUB_USERNAME }}
          dockerhub-token: ${{ secrets.DOCKERHUB_TOKEN }}
          docker-image-owner: makeplane
          docker-image-name: ${{ needs.branch_build_setup.outputs.dh_img_space }}
          build-context: .
          dockerfile-path: ./apps/space/Dockerfile.space
          buildx-driver: ${{ needs.branch_build_setup.outputs.gh_buildx_driver }}
          buildx-version: ${{ needs.branch_build_setup.outputs.gh_buildx_version }}
          buildx-platforms: ${{ needs.branch_build_setup.outputs.gh_buildx_platforms }}
          buildx-endpoint: ${{ needs.branch_build_setup.outputs.gh_buildx_endpoint }}

  branch_build_push_live:
    name: Build-Push Live Collaboration Docker Image
    runs-on: ubuntu-22.04
    needs: [branch_build_setup]
    steps:
      - name: Live Build and Push
        uses: makeplane/actions/build-push@v1.4.0
        with:
          build-release: ${{ needs.branch_build_setup.outputs.build_release }}
          build-prerelease: ${{ needs.branch_build_setup.outputs.build_prerelease }}
          release-version: ${{ needs.branch_build_setup.outputs.release_version }}
          dockerhub-username: ${{ secrets.DOCKERHUB_USERNAME }}
          dockerhub-token: ${{ secrets.DOCKERHUB_TOKEN }}
          docker-image-owner: makeplane
          docker-image-name: ${{ needs.branch_build_setup.outputs.dh_img_live }}
          build-context: .
          dockerfile-path: ./apps/live/Dockerfile.live
          buildx-driver: ${{ needs.branch_build_setup.outputs.gh_buildx_driver }}
          buildx-version: ${{ needs.branch_build_setup.outputs.gh_buildx_version }}
          buildx-platforms: ${{ needs.branch_build_setup.outputs.gh_buildx_platforms }}
          buildx-endpoint: ${{ needs.branch_build_setup.outputs.gh_buildx_endpoint }}

  branch_build_push_api:
    name: Build-Push API Server Docker Image
    runs-on: ubuntu-22.04
    needs: [branch_build_setup]
    steps:
      - name: Backend Build and Push
        uses: makeplane/actions/build-push@v1.4.0
        with:
          build-release: ${{ needs.branch_build_setup.outputs.build_release }}
          build-prerelease: ${{ needs.branch_build_setup.outputs.build_prerelease }}
          release-version: ${{ needs.branch_build_setup.outputs.release_version }}
          dockerhub-username: ${{ secrets.DOCKERHUB_USERNAME }}
          dockerhub-token: ${{ secrets.DOCKERHUB_TOKEN }}
          docker-image-owner: makeplane
          docker-image-name: ${{ needs.branch_build_setup.outputs.dh_img_backend }}
          build-context: ./apps/api
          dockerfile-path: ./apps/api/Dockerfile.api
          buildx-driver: ${{ needs.branch_build_setup.outputs.gh_buildx_driver }}
          buildx-version: ${{ needs.branch_build_setup.outputs.gh_buildx_version }}
          buildx-platforms: ${{ needs.branch_build_setup.outputs.gh_buildx_platforms }}
          buildx-endpoint: ${{ needs.branch_build_setup.outputs.gh_buildx_endpoint }}

  branch_build_push_proxy:
    name: Build-Push Proxy Docker Image
    runs-on: ubuntu-22.04
    needs: [branch_build_setup]
    steps:
      - name: Proxy Build and Push
        uses: makeplane/actions/build-push@v1.4.0
        with:
          build-release: ${{ needs.branch_build_setup.outputs.build_release }}
          build-prerelease: ${{ needs.branch_build_setup.outputs.build_prerelease }}
          release-version: ${{ needs.branch_build_setup.outputs.release_version }}
          dockerhub-username: ${{ secrets.DOCKERHUB_USERNAME }}
          dockerhub-token: ${{ secrets.DOCKERHUB_TOKEN }}
          docker-image-owner: makeplane
          docker-image-name: ${{ needs.branch_build_setup.outputs.dh_img_proxy }}
          build-context: ./apps/proxy
          dockerfile-path: ./apps/proxy/Dockerfile.ce
          buildx-driver: ${{ needs.branch_build_setup.outputs.gh_buildx_driver }}
          buildx-version: ${{ needs.branch_build_setup.outputs.gh_buildx_version }}
          buildx-platforms: ${{ needs.branch_build_setup.outputs.gh_buildx_platforms }}
          buildx-endpoint: ${{ needs.branch_build_setup.outputs.gh_buildx_endpoint }}

  branch_build_push_aio:
    if: ${{ needs.branch_build_setup.outputs.aio_build == 'true' }}
    name: Build-Push AIO Docker Image
    runs-on: ubuntu-22.04
    needs:
      - branch_build_setup
      - branch_build_push_admin
      - branch_build_push_web
      - branch_build_push_space
      - branch_build_push_live
      - branch_build_push_api
      - branch_build_push_proxy
    steps:
      - name: Checkout Files
        uses: actions/checkout@v6

      - name: Prepare AIO Assets
        id: prepare_aio_assets
        run: |
          cd deployments/aio/community

          if [ "${{ needs.branch_build_setup.outputs.build_type }}" == "Release" ]; then
            aio_version=${{ needs.branch_build_setup.outputs.release_version }}
          else
            aio_version=${{ needs.branch_build_setup.outputs.gh_branch_name }}
          fi
          bash ./build.sh --release $aio_version
          echo "AIO_BUILD_VERSION=${aio_version}" >> $GITHUB_OUTPUT

      - name: Upload AIO Assets
        uses: actions/upload-artifact@v7
        with:
          path: ./deployments/aio/community/dist
          name: aio-assets-dist

      - name: AIO Build and Push
        uses: makeplane/actions/build-push@v1.4.0
        with:
          build-release: ${{ needs.branch_build_setup.outputs.build_release }}
          build-prerelease: ${{ needs.branch_build_setup.outputs.build_prerelease }}
          release-version: ${{ needs.branch_build_setup.outputs.release_version }}
          dockerhub-username: ${{ secrets.DOCKERHUB_USERNAME }}
          dockerhub-token: ${{ secrets.DOCKERHUB_TOKEN }}
          docker-image-owner: makeplane
          docker-image-name: ${{ needs.branch_build_setup.outputs.dh_img_aio }}
          build-context: ./deployments/aio/community
          dockerfile-path: ./deployments/aio/community/Dockerfile
          buildx-driver: ${{ needs.branch_build_setup.outputs.gh_buildx_driver }}
          buildx-version: ${{ needs.branch_build_setup.outputs.gh_buildx_version }}
          buildx-platforms: ${{ needs.branch_build_setup.outputs.gh_buildx_platforms }}
          buildx-endpoint: ${{ needs.branch_build_setup.outputs.gh_buildx_endpoint }}
          additional-assets: aio-assets-dist
          additional-assets-dir: ./deployments/aio/community/dist
          build-args: |
            PLANE_VERSION=${{ steps.prepare_aio_assets.outputs.AIO_BUILD_VERSION }}

  upload_build_assets:
    name: Upload Build Assets
    runs-on: ubuntu-22.04
    needs:
      - branch_build_setup
      - branch_build_push_admin
      - branch_build_push_web
      - branch_build_push_space
      - branch_build_push_live
      - branch_build_push_api
      - branch_build_push_proxy
    steps:
      - name: Checkout Files
        uses: actions/checkout@v6

      - name: Update Assets
        run: |
          if [ "${{ needs.branch_build_setup.outputs.build_type }}" == "Release" ]; then
            REL_VERSION=${{ needs.branch_build_setup.outputs.release_version }}
          else
            REL_VERSION=${{ needs.branch_build_setup.outputs.gh_branch_name }}
          fi

          cp ./deployments/cli/community/install.sh deployments/cli/community/setup.sh
          sed -i 's/${APP_RELEASE:-stable}/${APP_RELEASE:-'${REL_VERSION}'}/g' deployments/cli/community/docker-compose.yml
          # sed -i 's/APP_RELEASE=stable/APP_RELEASE='${REL_VERSION}'/g' deployments/cli/community/variables.env

      - name: Upload Assets
        uses: actions/upload-artifact@v7
        with:
          name: community-assets
          path: |
            ./deployments/cli/community/setup.sh
            ./deployments/cli/community/restore.sh
            ./deployments/cli/community/restore-airgapped.sh
            ./deployments/cli/community/docker-compose.yml
            ./deployments/cli/community/variables.env
            ./deployments/swarm/community/swarm.sh

  publish_release:
    if: ${{ needs.branch_build_setup.outputs.build_type == 'Release' }}
    name: Build Release
    runs-on: ubuntu-22.04
    needs:
      [
        branch_build_setup,
        branch_build_push_admin,
        branch_build_push_web,
        branch_build_push_space,
        branch_build_push_live,
        branch_build_push_api,
        branch_build_push_proxy,
      ]
    env:
      REL_VERSION: ${{ needs.branch_build_setup.outputs.release_version }}
    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - name: Update Assets
        run: |
          cp ./deployments/cli/community/install.sh deployments/cli/community/setup.sh
          sed -i 's/${APP_RELEASE:-stable}/${APP_RELEASE:-'${REL_VERSION}'}/g' deployments/cli/community/docker-compose.yml
          # sed -i 's/APP_RELEASE=stable/APP_RELEASE='${REL_VERSION}'/g' deployments/cli/community/variables.env

      - name: Create Release
        id: create_release
        uses: softprops/action-gh-release@v2.6.1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
        with:
          tag_name: ${{ env.REL_VERSION }}
          name: ${{ env.REL_VERSION }}
          target_commitish: ${{ github.sha }}
          draft: false
          prerelease: ${{ env.IS_PRERELEASE }}
          generate_release_notes: true
          files: |
            ${{ github.workspace }}/deployments/cli/community/setup.sh
            ${{ github.workspace }}/deployments/cli/community/restore.sh
            ${{ github.workspace }}/deployments/cli/community/restore-airgapped.sh
            ${{ github.workspace }}/deployments/cli/community/docker-compose.yml
            ${{ github.workspace }}/deployments/cli/community/variables.env
            ${{ github.workspace }}/deployments/swarm/community/swarm.sh
