version: 2
jobs:
  # =======================================================================================
  # Python 3.6 testing.
  # =======================================================================================
  test-3.6:
    docker:
      - image: python:3.6
    working_directory: ~/spleeter
    steps:
      - checkout
      - restore_cache:
          key: models-{{ checksum "spleeter/model/__init__.py" }}
      - run: apt-get update && apt-get install -y ffmpeg
      - run: pip install -r requirements.txt && pip install pytest pytest-xdist
      - run: make test
      - save_cache:
          key: models-{{ checksum "spleeter/model/__init__.py" }}
          paths:
            - "pretrained_models"
  # =======================================================================================
  # Python 3.7 testing.
  # =======================================================================================
  test-3.7:
    docker:
      - image: python:3.7
    working_directory: ~/spleeter
    steps:
      - checkout
      - restore_cache:
          key: models-{{ checksum "spleeter/model/__init__.py" }}
      - run: apt-get update && apt-get install -y ffmpeg
      - run: pip install -r requirements.txt && pip install pytest pytest-xdist
      - run: make test
      - save_cache:
          key: models-{{ checksum "spleeter/model/__init__.py" }}
          paths:
            - "pretrained_models"
  # =======================================================================================
  # Source distribution packaging.
  # =======================================================================================
  sdist:
    docker:
      - image: python:3
    steps:
      - checkout
      - run: make build
      - save_cache:
          key: sdist-{{ .Branch }}-{{ checksum "setup.py" }}
          paths:
            - dist
  sdist-gpu:
    docker:
      - image: python:3
    steps:
      - checkout
      - run: make build-gpu
      - save_cache:
          key: sdist-{{ .Branch }}-{{ checksum "setup.py" }}
          paths:
            - dist
  # =======================================================================================
  # PyPi deployment.
  # =======================================================================================
  pypi-deploy:
    docker:
      - image: python:3
    steps:
      - checkout
      - restore_cache:
          key: sdist-{{ .Branch }}-{{ checksum "setup.py" }}
      - run:
          name: upload to PyPi
          # TODO: Infer destination regarding of branch.
          #       - master => production PyPi
          #       - other => testing PyPi
          command: make build deploy
  pypi-deploy-gpu:
    docker:
      - image: python:3
    steps:
      - checkout
      - restore_cache:
          key: sdist-{{ .Branch }}-{{ checksum "setup.py" }}
      - run:
          name: upload to PyPi
          # TODO: Infer destination regarding of branch.
          #       - master => production PyPi
          #       - other => testing PyPi
          command: make build-gpu deploy
  # =======================================================================================
  # Docker build.
  # =======================================================================================
  docker-conda-cpu:
    docker:
      - image: docker:17.05.0-ce-git
    steps:
      - checkout
      - setup_remote_docker
      - run: docker build -t researchdeezer/spleeter:conda -f docker/conda.dockerfile .
      - run: docker build --build-arg BASETAG=conda --build-arg MODEL=2stems -t researchdeezer/spleeter:conda-2stems -f docker/embedded-model.dockerfile .
      - run: docker build --build-arg BASETAG=conda --build-arg MODEL=4stems -t researchdeezer/spleeter:conda-4stems -f docker/embedded-model.dockerfile .
      - run: docker build --build-arg BASETAG=conda --build-arg MODEL=5stems -t researchdeezer/spleeter:conda-5stems -f docker/embedded-model.dockerfile .
      - run: docker run -v $(pwd):/runtime researchdeezer/spleeter:conda separate -i /runtime/audio_example.mp3 -o /tmp
      - run: docker run -v $(pwd):/runtime researchdeezer/spleeter:conda-2stems separate -i /runtime/audio_example.mp3 -o /tmp
      - run: docker run -v $(pwd):/runtime researchdeezer/spleeter:conda-4stems separate -i /runtime/audio_example.mp3 -p spleeter:4stems -o /tmp
      - run: docker run -v $(pwd):/runtime researchdeezer/spleeter:conda-5stems separate -i /runtime/audio_example.mp3 -p spleeter:5stems -o /tmp
      - run: docker login -u $DOCKERHUB_USERNAME -p $DOCKERHUB_PASSWORD
      - run: docker push researchdeezer/spleeter:conda
      - run: docker push researchdeezer/spleeter:conda-2stems
      - run: docker push researchdeezer/spleeter:conda-4stems
      - run: docker push researchdeezer/spleeter:conda-5stems
  docker-conda-gpu:
    docker:
      - image: docker:17.05.0-ce-git
    steps:
      - checkout
      - setup_remote_docker
      - run: docker build -t researchdeezer/spleeter:conda-gpu -f docker/conda-gpu.dockerfile .
      - run: docker build --build-arg BASETAG=conda-gpu --build-arg MODEL=2stems -t researchdeezer/spleeter:conda-gpu-2stems -f docker/embedded-model.dockerfile .
      - run: docker build --build-arg BASETAG=conda-gpu --build-arg MODEL=4stems -t researchdeezer/spleeter:conda-gpu-4stems -f docker/embedded-model.dockerfile .
      - run: docker build --build-arg BASETAG=conda-gpu --build-arg MODEL=5stems -t researchdeezer/spleeter:conda-gpu-5stems -f docker/embedded-model.dockerfile .
      - run: docker login -u $DOCKERHUB_USERNAME -p $DOCKERHUB_PASSWORD
      - run: docker push researchdeezer/spleeter:conda-gpu
      - run: docker push researchdeezer/spleeter:conda-gpu-2stems
      - run: docker push researchdeezer/spleeter:conda-gpu-4stems
      - run: docker push researchdeezer/spleeter:conda-gpu-5stems
      - run: docker tag researchdeezer/spleeter:conda-gpu researchdeezer/spleeter:gpu
      - run: docker push researchdeezer/spleeter:gpu
  docker-3.6-cpu:
    docker:
      - image: docker:17.05.0-ce-git
    steps:
      - checkout
      - setup_remote_docker
      - run: docker build -t researchdeezer/spleeter:3.6 -f docker/python-3.6.dockerfile .
      - run: docker build --build-arg BASETAG=3.6 --build-arg MODEL=2stems -t researchdeezer/spleeter:3.6-2stems -f docker/embedded-model.dockerfile .
      - run: docker build --build-arg BASETAG=3.6 --build-arg MODEL=4stems -t researchdeezer/spleeter:3.6-4stems -f docker/embedded-model.dockerfile .
      - run: docker build --build-arg BASETAG=3.6 --build-arg MODEL=5stems -t researchdeezer/spleeter:3.6-5stems -f docker/embedded-model.dockerfile .
      - run: docker run -v $(pwd):/runtime researchdeezer/spleeter:3.6 separate -i /runtime/audio_example.mp3 -o /tmp
      - run: docker run -v $(pwd):/runtime researchdeezer/spleeter:3.6-2stems separate -i /runtime/audio_example.mp3 -o /tmp
      - run: docker run -v $(pwd):/runtime researchdeezer/spleeter:3.6-4stems separate -i /runtime/audio_example.mp3 -p spleeter:4stems -o /tmp
      - run: docker run -v $(pwd):/runtime researchdeezer/spleeter:3.6-5stems separate -i /runtime/audio_example.mp3 -p spleeter:5stems -o /tmp
      - run: docker login -u $DOCKERHUB_USERNAME -p $DOCKERHUB_PASSWORD
      - run: docker push researchdeezer/spleeter:3.6
      - run: docker push researchdeezer/spleeter:3.6-2stems
      - run: docker push researchdeezer/spleeter:3.6-4stems
      - run: docker push researchdeezer/spleeter:3.6-5stems
  docker-3.6-gpu:
    docker:
      - image: docker:17.05.0-ce-git
    steps:
      - checkout
      - setup_remote_docker
      - run: docker build -t researchdeezer/spleeter:3.6-gpu -f docker/python-3.6-gpu.dockerfile .
      - run: docker build --build-arg BASETAG=3.6-gpu --build-arg MODEL=2stems -t researchdeezer/spleeter:3.6-gpu-2stems -f docker/embedded-model.dockerfile .
      - run: docker build --build-arg BASETAG=3.6-gpu --build-arg MODEL=4stems -t researchdeezer/spleeter:3.6-gpu-4stems -f docker/embedded-model.dockerfile .
      - run: docker build --build-arg BASETAG=3.6-gpu --build-arg MODEL=5stems -t researchdeezer/spleeter:3.6-gpu-5stems -f docker/embedded-model.dockerfile .
      - run: docker login -u $DOCKERHUB_USERNAME -p $DOCKERHUB_PASSWORD
      - run: docker push researchdeezer/spleeter:3.6-gpu
      - run: docker push researchdeezer/spleeter:3.6-gpu-2stems
      - run: docker push researchdeezer/spleeter:3.6-gpu-4stems
      - run: docker push researchdeezer/spleeter:3.6-gpu-5stems
  docker-3.7-cpu:
    docker:
      - image: docker:17.05.0-ce-git
    steps:
      - checkout
      - setup_remote_docker
      - run: docker build -t researchdeezer/spleeter:3.7 -f docker/python-3.7.dockerfile .
      - run: docker build --build-arg BASETAG=3.7 --build-arg MODEL=2stems -t researchdeezer/spleeter:3.7-2stems -f docker/embedded-model.dockerfile .
      - run: docker build --build-arg BASETAG=3.7 --build-arg MODEL=4stems -t researchdeezer/spleeter:3.7-4stems -f docker/embedded-model.dockerfile .
      - run: docker build --build-arg BASETAG=3.7 --build-arg MODEL=5stems -t researchdeezer/spleeter:3.7-5stems -f docker/embedded-model.dockerfile .
      - run: docker run -v $(pwd):/runtime researchdeezer/spleeter:3.7 separate -i /runtime/audio_example.mp3 -o /tmp
      - run: docker run -v $(pwd):/runtime researchdeezer/spleeter:3.7-2stems separate -i /runtime/audio_example.mp3 -o /tmp
      - run: docker run -v $(pwd):/runtime researchdeezer/spleeter:3.7-4stems separate -i /runtime/audio_example.mp3 -p spleeter:4stems -o /tmp
      - run: docker run -v $(pwd):/runtime researchdeezer/spleeter:3.7-5stems separate -i /runtime/audio_example.mp3 -p spleeter:5stems -o /tmp
      - run: docker login -u $DOCKERHUB_USERNAME -p $DOCKERHUB_PASSWORD
      - run: docker tag researchdeezer/spleeter:3.7 researchdeezer/spleeter:latest
      - run: docker push researchdeezer/spleeter:latest
      - run: docker push researchdeezer/spleeter:3.7
      - run: docker push researchdeezer/spleeter:3.7-2stems
      - run: docker push researchdeezer/spleeter:3.7-4stems
      - run: docker push researchdeezer/spleeter:3.7-5stems
  docker-3.7-gpu:
    docker:
      - image: docker:17.05.0-ce-git
    steps:
      - checkout
      - setup_remote_docker
      - run: docker build -t researchdeezer/spleeter:3.7-gpu -f docker/python-3.7-gpu.dockerfile .
      - run: docker build --build-arg BASETAG=3.7-gpu --build-arg MODEL=2stems -t researchdeezer/spleeter:3.7-gpu-2stems -f docker/embedded-model.dockerfile .
      - run: docker build --build-arg BASETAG=3.7-gpu --build-arg MODEL=4stems -t researchdeezer/spleeter:3.7-gpu-4stems -f docker/embedded-model.dockerfile .
      - run: docker build --build-arg BASETAG=3.7-gpu --build-arg MODEL=5stems -t researchdeezer/spleeter:3.7-gpu-5stems -f docker/embedded-model.dockerfile .
      - run: docker login -u $DOCKERHUB_USERNAME -p $DOCKERHUB_PASSWORD
      - run: docker push researchdeezer/spleeter:3.7-gpu
      - run: docker push researchdeezer/spleeter:3.7-gpu-2stems
      - run: docker push researchdeezer/spleeter:3.7-gpu-4stems
      - run: docker push researchdeezer/spleeter:3.7-gpu-5stems
workflows:
  version: 2
  spleeter-release-pipeline:
    jobs:
      - test-3.6
      - test-3.7
      - sdist:
          requires:
            - test-3.6
            - test-3.7
      - sdist-gpu:
          requires:
            - test-3.6
            - test-3.7
      - pypi-deploy:
          filters:
            branches:
              only:
                - master
          requires:
            - sdist
      - pypi-deploy-gpu:
          filters:
            branches:
              only:
                - master
          requires:
            - sdist-gpu
      - conda-forge-validation:
          type: approval
          requires:
            - pypi-deploy
            - pypi-deploy-gpu
          filters:
            branches:
              only:
                - master
      - docker-conda-cpu:
          requires:
            - conda-forge-validation
          filters:
            branches:
              only:
                - master
      - docker-conda-gpu:
          requires:
            - conda-forge-validation
          filters:
            branches:
              only:
                - master
      - docker-3.6-cpu:
          requires:
            - pypi-deploy
          filters:
            branches:
              only:
                - master
      - docker-3.6-gpu:
          requires:
            - pypi-deploy-gpu
          filters:
            branches:
              only:
                - master
      - docker-3.7-cpu:
          requires:
            - pypi-deploy
          filters:
            branches:
              only:
                - master
      - docker-3.7-gpu:
          requires:
            - pypi-deploy-gpu
          filters:
            branches:
              only:
                - master
