# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

jobs:
- job: 'version_number_validation'
  pool:
    vmImage: 'Ubuntu 16.04'
  strategy:
    matrix:
      Python36:
        PYTHON_VERSION: '3.6'
  steps:
  - script: |
      echo $(build_version)
      if [[ $(build_version) =~ ^v[0-9](.[0-9]){1,3}$ ]]; then
        echo 'valid build version $(build_version)'
        echo `git describe --tags --abbrev=0`
      else
        echo 'invalid build version $(build_version)'
        exit 1
      fi
    condition: eq( variables['build_type'], 'prerelease' )
    displayName: 'Validate prerelease version number'

  - script: |
      export BRANCH_TAG=`git describe --tags --abbrev=0`
      echo $BRANCH_TAG
      if [[ $BRANCH_TAG = $(build_version) && $BRANCH_TAG =~ ^v[0-9](.[0-9]){1,3}$ ]]; then
        echo 'Build version match branch tag'
      else
        echo 'Build version does not match branch tag'
        exit 1
      fi
    condition: eq( variables['build_type'], 'release' )
    displayName: 'Validate release version number and branch tag'


- job: 'Build_upload_nni_ubuntu'
  dependsOn: version_number_validation
  condition: succeeded()
  pool:
    vmImage: 'Ubuntu 16.04'
  strategy:
    matrix:
      Python36:
        PYTHON_VERSION: '3.6'

  steps:
  - script: |
      python3 -m pip install --upgrade pip setuptools --user
      python3 -m pip install twine --user
    displayName: 'Install twine'

  - script: |
      cd deployment/pypi
      if [ $(build_type) = 'prerelease' ]
      then
        # NNI build scripts (Makefile) uses branch tag as package version number
        git tag $(build_version)
        echo 'building prerelease package...'
        make version_ts=true build
      else
        echo 'building release package...'
        make build
      fi
    condition: eq( variables['upload_package'], 'true')
    displayName: 'build nni bdsit_wheel'

  - script: |
      cd deployment/pypi
      if [ $(build_type) = 'prerelease' ]
      then
        echo 'uploading prerelease package to testpypi...'
        python3 -m twine upload -u $(testpypi_user) -p $(testpypi_pwd) --repository-url https://test.pypi.org/legacy/ dist/*
      else
        echo 'uploading release package to pypi...'
        python3 -m twine upload -u $(pypi_user) -p $(pypi_pwd) dist/*
      fi
    condition: eq( variables['upload_package'], 'true')
    displayName: 'upload nni package to pypi/testpypi'

  - script: |
      cd deployment/docker

      if [ $(build_type) = 'prerelease' ]
      then
        docker login -u $(docker_hub_dev_user) -p $(docker_hub_dev_pwd)
        export IMG_NAME=$(dev_docker_img)
        export IMG_TAG=`git describe --tags --abbrev=0`.`date -u +%y%m%d%H%M`
        echo 'updating docker file for testpyi...'
        sed -ie 's/RUN python3 -m pip --no-cache-dir install nni/RUN python3 -m pip install --no-cache-dir --index-url https:\/\/test.pypi.org\/simple --extra-index-url https:\/\/pypi.org\/simple nni/' Dockerfile
      else
        docker login -u $(docker_hub_user) -p $(docker_hub_pwd)
        export IMG_NAME=msranni/nni
        export IMG_TAG=`git describe --tags --abbrev=0`
      fi
      echo $IMG_NAME:$IMG_TAG
      cat Dockerfile
      docker build -f Dockerfile -t $IMG_NAME:$IMG_TAG .
      docker push $IMG_NAME:$IMG_TAG

      if [ $(update_latest_tag) = 'true' ]
      then
        docker tag $IMG_NAME:$IMG_TAG $IMG_NAME:latest
        docker push $IMG_NAME:latest
      fi

    condition: eq( variables['build_docker_img'], 'true')
    displayName: 'build and upload nni docker image'

- job: 'Build_upload_nni_macos'
  dependsOn: version_number_validation
  condition: succeeded()
  pool:
    vmImage: 'macOS-10.15'
  strategy:
    matrix:
      Python36:
        PYTHON_VERSION: '3.6'
  steps:
  - script: |
      python3 -m pip install --upgrade pip setuptools --user
      python3 -m pip install twine --user
    displayName: 'Install twine'

  - script: |
      make install-dependencies
    displayName: 'Install nni dependencies'

  - script: |
      cd deployment/pypi
      if [ $(build_type) = 'prerelease' ]
      then
        # NNI build scripts (Makefile) uses branch tag as package version number
        git tag $(build_version)
        echo 'building prerelease package...'
        PATH=$HOME/Library/Python/3.7/bin:$PATH make version_ts=true build
      else
        echo 'building release package...'
        PATH=$HOME/Library/Python/3.7/bin:$PATH make build
      fi
    condition: eq( variables['upload_package'], 'true')
    displayName: 'build nni bdsit_wheel'

  - script: |
      cd deployment/pypi
      if [ $(build_type) = 'prerelease' ]
      then
        echo 'uploading prerelease package to testpypi...'
        python3 -m twine upload -u $(testpypi_user) -p $(testpypi_pwd) --repository-url https://test.pypi.org/legacy/ dist/*
      else
        echo 'uploading release package to pypi...'
        python3 -m twine upload -u $(pypi_user) -p $(pypi_pwd) dist/*
      fi
    condition: eq( variables['upload_package'], 'true')
    displayName: 'upload nni package to pypi/testpypi'

- job: 'Build_upload_nni_win32'
  dependsOn: version_number_validation
  condition: succeeded()
  pool:
    vmImage: 'vs2017-win2016'
  strategy:
    matrix:
      Python36:
        PYTHON_VERSION: '3.6'
  steps:
  - powershell: |
      python -m pip install --upgrade pip setuptools
      python -m pip install twine
    displayName: 'Install twine'

  - powershell: |
      cd deployment\pypi
      if($env:BUILD_TYPE -eq 'prerelease'){
        # NNI build scripts (powershell) uses branch tag as package version number
        git tag $env:BUILD_VERSION
        Write-Host 'building prerelease package...'
        .\install.ps1 -version_os 32 -version_ts $True
      }
      else{
        Write-Host 'building release package...'
        .\install.ps1 -version_os 32 -version_ts $False
      }
    condition: eq( variables['upload_package'], 'true')
    displayName: 'build nni bdsit_wheel'

  - powershell: |
      cd deployment\pypi
      if($env:BUILD_TYPE -eq 'prerelease'){
        Write-Host 'uploading prerelease package to testpypi...'
        python -m twine upload -u $(testpypi_user) -p $(testpypi_pwd) --repository-url https://test.pypi.org/legacy/ dist/*
      }
      else{
        Write-Host 'uploading release package to pypi...'
        python -m twine upload -u $(pypi_user) -p $(pypi_pwd) dist/*
      }
    condition: eq( variables['upload_package'], 'true')
    displayName: 'upload nni package to pypi/testpypi'

- job: 'Build_upload_nni_win_amd64'
  dependsOn: version_number_validation
  condition: succeeded()
  pool:
    vmImage: 'vs2017-win2016'
  strategy:
    matrix:
      Python36:
        PYTHON_VERSION: '3.6'
  steps:
  - powershell: |
      python -m pip install --upgrade pip setuptools
      python -m pip install twine
    displayName: 'Install twine'

  - powershell: |
      cd deployment\pypi
      if($env:BUILD_TYPE -eq 'prerelease'){
        # NNI build scripts (powershell) uses branch tag as package version number
        git tag $env:BUILD_VERSION
        Write-Host 'building prerelease package...'
        .\install.ps1 -version_os 64 -version_ts $True
      }
      else{
        Write-Host 'building release package...'
        .\install.ps1 -version_os 64 -version_ts $False
      }
    condition: eq( variables['upload_package'], 'true')
    displayName: 'build nni bdsit_wheel'

  - powershell: |
      cd deployment\pypi
      if($env:BUILD_TYPE -eq 'prerelease'){
        Write-Host 'uploading prerelease package to testpypi...'
        python -m twine upload -u $(testpypi_user) -p $(testpypi_pwd) --repository-url https://test.pypi.org/legacy/ dist/*
      }
      else{
        Write-Host 'uploading release package to pypi...'
        python -m twine upload -u $(pypi_user) -p $(pypi_pwd) dist/*
      }
    condition: eq( variables['upload_package'], 'true')
    displayName: 'upload nni package to pypi/testpypi'
