name: Ubuntu

on:
  workflow_dispatch:
    inputs:
      developer_build:
        description: 'Set to OFF for Release wheels'
        required: false
        default: 'ON'
  push:
    branches:
      - master
  pull_request:
    types: [opened, reopened, synchronize]

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

env:
  NPROC: 2
  GCE_CLI_GHA_VERSION: "302.0.0"

jobs:
  ubuntu:
    runs-on: ubuntu-18.04
    strategy:
      fail-fast: false
      matrix:
        BUILD_SHARED_LIBS: [ON, OFF]
        MLOPS: [ON, OFF]
        exclude:
          - BUILD_SHARED_LIBS: OFF
            MLOPS: ON
    env:
      BUILD_SHARED_LIBS: ${{ matrix.BUILD_SHARED_LIBS }}
      BUILD_CUDA_MODULE: OFF
      BUILD_TENSORFLOW_OPS: ${{ matrix.MLOPS }}
      BUILD_PYTORCH_OPS: ${{ matrix.MLOPS }}
      DEVELOPER_BUILD: ${{ github.event.inputs.developer_build || 'ON' }}
      OPEN3D_CPU_RENDERING: true
    steps:
      - name: Checkout source code
        uses: actions/checkout@v2
      - name: Maximize build space
        run: |
          source util/ci_utils.sh
          maximize_ubuntu_github_actions_build_space
      - name: Docker build
        run: |
          if   [ "${{ matrix.BUILD_SHARED_LIBS }}" = "OFF" ] && [ "${{ matrix.MLOPS }}" = "OFF" ]; then
            docker/docker_build.sh cpu-static
          elif [ "${{ matrix.BUILD_SHARED_LIBS }}" = "ON"  ] && [ "${{ matrix.MLOPS }}" = "OFF" ] && [ "${{ env.DEVELOPER_BUILD }}" = "ON"  ]; then
            docker/docker_build.sh cpu-shared
          elif [ "${{ matrix.BUILD_SHARED_LIBS }}" = "ON"  ] && [ "${{ matrix.MLOPS }}" = "OFF" ] && [ "${{ env.DEVELOPER_BUILD }}" = "OFF" ]; then
            docker/docker_build.sh cpu-shared-release
          elif [ "${{ matrix.BUILD_SHARED_LIBS }}" = "ON"  ] && [ "${{ matrix.MLOPS }}" = "ON"  ] && [ "${{ env.DEVELOPER_BUILD }}" = "ON"  ]; then
            docker/docker_build.sh cpu-shared-ml
          elif [ "${{ matrix.BUILD_SHARED_LIBS }}" = "ON"  ] && [ "${{ matrix.MLOPS }}" = "ON"  ] && [ "${{ env.DEVELOPER_BUILD }}" = "OFF" ]; then
            docker/docker_build.sh cpu-shared-ml-release
          fi
      - name: Docker test
        run: |
          if   [ "${{ matrix.BUILD_SHARED_LIBS }}" = "OFF" ] && [ "${{ matrix.MLOPS }}" = "OFF" ]; then
            docker/docker_test.sh cpu-static
          elif [ "${{ matrix.BUILD_SHARED_LIBS }}" = "ON"  ] && [ "${{ matrix.MLOPS }}" = "OFF" ] && [ "${{ env.DEVELOPER_BUILD }}" = "ON"  ]; then
            docker/docker_test.sh cpu-shared
          elif [ "${{ matrix.BUILD_SHARED_LIBS }}" = "ON"  ] && [ "${{ matrix.MLOPS }}" = "OFF" ] && [ "${{ env.DEVELOPER_BUILD }}" = "OFF" ]; then
            docker/docker_test.sh cpu-shared-release
          elif [ "${{ matrix.BUILD_SHARED_LIBS }}" = "ON"  ] && [ "${{ matrix.MLOPS }}" = "ON"  ] && [ "${{ env.DEVELOPER_BUILD }}" = "ON"  ]; then
            docker/docker_test.sh cpu-shared-ml
          elif [ "${{ matrix.BUILD_SHARED_LIBS }}" = "ON"  ] && [ "${{ matrix.MLOPS }}" = "ON"  ] && [ "${{ env.DEVELOPER_BUILD }}" = "OFF" ]; then
            docker/docker_test.sh cpu-shared-ml-release
          fi
      - name: Upload package to GitHub artifacts
        if: ${{ env.BUILD_SHARED_LIBS == 'ON' }}
        uses: actions/upload-artifact@v2
        with:
          name: open3d-devel-linux-x86_64
          path: open3d-devel-*.tar.xz
          if-no-files-found: error
      - name: GCloud CLI setup
        if: ${{ github.ref == 'refs/heads/master' && env.BUILD_SHARED_LIBS == 'ON' }}
        # https://github.com/GoogleCloudPlatform/github-actions/issues/100#issuecomment-650798308
        uses: google-github-actions/setup-gcloud@v0.2.1
        with:
          version: ${{ env.GCE_CLI_GHA_VERSION }}
          service_account_key: ${{ secrets.GCE_SA_KEY_GPU_CI }}
          project_id: ${{ secrets.GCE_DOCS_PROJECT }}
          export_default_credentials: true
      - name: Upload package to GCS bucket
        if: ${{ github.ref == 'refs/heads/master' && env.BUILD_SHARED_LIBS == 'ON' }}
        run: |
          gsutil cp open3d-devel-*.tar.xz gs://open3d-releases-master/devel/
          echo "Download devel package at: https://storage.googleapis.com/open3d-releases-master/devel/${{ env.DEVEL_PKG_NAME }}"
