name: build

on:
  push:
    paths-ignore:
      - 'README.md'
      - 'README_zh-CN.md'
      - 'docs/**'
      - 'examples/**'
      - '.dev_scripts/**'
      - 'docker/**'

  pull_request:
    paths-ignore:
      - 'README.md'
      - 'README_zh-CN.md'
      - 'docs/**'
      - 'examples/**'
      - '.dev_scripts/**'
      - 'docker/**'

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

env:
  MMCV_WITH_OPS: 1

jobs:
  build_without_ops:
    runs-on: ubuntu-18.04
    env:
      MMCV_WITH_OPS: 0
    strategy:
      matrix:
        python-version: [3.7]
        torch: [1.7.0, 1.8.0, 1.9.0]
        include:
          - torch: 1.7.0
            torchvision: 0.8.1
          - torch: 1.8.0
            torchvision: 0.9.0
          - torch: 1.9.0
            torchvision: 0.10.0
    steps:
      - uses: actions/checkout@v2
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}
      - name: Install system dependencies
        run: sudo apt-get update && sudo apt-get install -y ffmpeg libturbojpeg openssh-client
      - name: Install PyTorch
        run: pip install torch==${{matrix.torch}}+cpu torchvision==${{matrix.torchvision}}+cpu -f https://download.pytorch.org/whl/torch_stable.html
      - name: Install MMEngine
        run: |
          eval `ssh-agent -s`
          ssh-add - <<< '${{ secrets.TEST_SSH_KEY }}'
          pip install git+ssh://git@github.com/open-mmlab/mmengine.git
      - name: Build and install
        run: rm -rf .eggs && pip install -e .
      - name: Validate the installation
        run: python -c "import mmcv"
      - name: Run unittests
        run: |
          pip install -r requirements/test.txt
          pytest tests/ --ignore=tests/test_ops

  build_cpu:
    runs-on: ubuntu-18.04
    strategy:
      matrix:
        python-version: [3.7]
        torch: [1.5.1, 1.6.0, 1.7.0, 1.8.0, 1.9.0]
        include:
          - torch: 1.5.1
            torchvision: 0.6.1
          - torch: 1.6.0
            torchvision: 0.7.0
          - torch: 1.7.0
            torchvision: 0.8.1
          - torch: 1.8.0
            torchvision: 0.9.0
          - torch: 1.9.0
            torchvision: 0.10.0
    steps:
      - uses: actions/checkout@v2
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}
      - name: Install system dependencies
        run: sudo apt-get update && sudo apt-get install -y ffmpeg libturbojpeg openssh-client
      - name: Install PyTorch
        run: pip install torch==${{matrix.torch}}+cpu torchvision==${{matrix.torchvision}}+cpu -f https://download.pytorch.org/whl/torch_stable.html
      - name: Install MMEngine
        run: |
          eval `ssh-agent -s`
          ssh-add - <<< '${{ secrets.TEST_SSH_KEY }}'
          pip install git+ssh://git@github.com/open-mmlab/mmengine.git
      # pstuil is an optional package to detect the number of CPU for compiling mmcv
      - name: Install psutil
        run: pip install psutil
      - name: Create sdist and untar
        run: |
          MMCV_WITH_OPS=1 python setup.py sdist
          tar zxvf dist/mmcv-full* -C /tmp
          rm -r mmcv
      - name: Build and install from sdist
        run: |
          pushd /tmp/mmcv-full*
          pip install -e .
          popd
      - name: Validate the installation
        run: python -c "import mmcv"
      - name: Run unittests and generate coverage report
        run: |
          pip install -r requirements/test.txt
          coverage run --branch --source=mmcv -m pytest tests/
          coverage xml
          coverage report -m
