# Docker
# Build a Docker image
# https://docs.microsoft.com/azure/devops/pipelines/languages/docker

trigger:
  branches:
    include:
    - master
  paths:
    include:
    - .dev/docker/env/Dockerfile
    - .dev/docker/windows
    - .ci/azure-pipelines/env.yml

pr:
  paths:
    include:
    - .dev/docker/env/Dockerfile
    - .dev/docker/windows
    - .ci/azure-pipelines/env.yml

schedules:
- cron: "0 0 * * 0"
  displayName: "Sunday midnight build"
  branches:
    include:
    - master

resources:
- repo: self

variables:
  dockerHub: 'PointCloudLibrary@hub.docker.com'
  dockerHubID: "pointcloudlibrary"

jobs:
- job: BuildAndPushUbuntu
  timeoutInMinutes: 360
  displayName: "Env"
  pool:
    vmImage: 'ubuntu-latest'
  strategy:
    matrix:
      Ubuntu 18.04:
        # Test the oldest supported version of Ubuntu
        UBUNTU_VERSION: 18.04
        VTK_VERSION: 7
        ENSENSOSDK_VERSION: 2.3.1570
        TAG: 18.04
      Ubuntu 20.04:
        UBUNTU_VERSION: 20.04
        VTK_VERSION: 7
        TAG: 20.04
      # Test the latest LTS version of Ubuntu
      Ubuntu 22.04:
        UBUNTU_VERSION: 22.04
        VTK_VERSION: 9
        TAG: 22.04
      Ubuntu 22.10:
        UBUNTU_VERSION: 22.10
        USE_LATEST_CMAKE: true
        VTK_VERSION: 9
        TAG: 22.10
  steps:
  - script: |
      dockerBuildArgs="" ; \
      if [ -n "$UBUNTU_VERSION" ]; then \
        dockerBuildArgs="$dockerBuildArgs --build-arg UBUNTU_VERSION=$UBUNTU_VERSION" ; \
      fi ; \
      if [ -n "$ENSENSOSDK_VERSION" ]; then \
        dockerBuildArgs="$dockerBuildArgs --build-arg ENSENSOSDK_VERSION=$ENSENSOSDK_VERSION" ; \
      fi ; \
      if [ -n "$VTK_VERSION" ]; then \
        dockerBuildArgs="$dockerBuildArgs --build-arg VTK_VERSION=$VTK_VERSION" ; \
      fi ; \
      if [ -n "$USE_LATEST_CMAKE" ]; then \
        dockerBuildArgs="$dockerBuildArgs --build-arg USE_LATEST_CMAKE=$USE_LATEST_CMAKE" ; \
      fi
      echo "##vso[task.setvariable variable=dockerBuildArgs]$dockerBuildArgs"
    displayName: "Prepare docker build arguments"
  - task: Docker@2
    displayName: "Build docker image"
    inputs:
      command: build
      arguments: |
        --no-cache
        $(dockerBuildArgs)
        -t $(dockerHubID)/env:$(TAG)
      dockerfile: '$(Build.SourcesDirectory)/.dev/docker/env/Dockerfile'
      tags: "$(TAG)"
  - script: |
      set -x
      docker run --rm -v "$(Build.SourcesDirectory)":/pcl $(dockerHubID)/env:$(TAG) bash -c ' \
      mkdir /pcl/build && cd /pcl/build && \
      cmake /pcl \
        -DCMAKE_BUILD_TYPE="Release" \
        -DPCL_ONLY_CORE_POINT_TYPES=ON \
        -DBUILD_io:BOOL=OFF \
        -DBUILD_kdtree:BOOL=OFF && \
      cmake --build . -- -j2'
    displayName: 'Verify Dockerimage'
  - task: Docker@2
    displayName: "Push docker image"
    inputs:
      command: push
      containerRegistry: $(dockerHub)
      repository: $(dockerHubID)/env
      tags: "$(TAG)"
      condition: and(eq(variables['Build.Repository.Name'], 'PointCloudLibrary/pcl'),
                     eq(variables['Build.SourceBranch'], 'refs/heads/master'))
- job: BuildAndPushWindows
  timeoutInMinutes: 360
  displayName: "Env"
  pool:
    vmImage: 'windows-2019'
  strategy:
    matrix:
      Winx86:
        PLATFORM: x86
        TAG: winx86
        GENERATOR: "'Visual Studio 16 2019' -A Win32"
        VCPKGCOMMIT: fd766eba2b4cf59c7123d46189be373e2cee959d
      Winx64:
        PLATFORM: x64
        TAG: winx64
        GENERATOR: "'Visual Studio 16 2019' -A x64"
        VCPKGCOMMIT: master
  steps:
  - task: Docker@2
    displayName: "Build docker image"
    inputs:
      command: build
      arguments: |
        --no-cache
        --build-arg PLATFORM=$(PLATFORM)
        --build-arg VCPKGCOMMIT=$(VCPKGCOMMIT)
        -t $(dockerHubID)/env:$(TAG)
      dockerfile: '$(Build.SourcesDirectory)/.dev/docker/windows/Dockerfile'
      tags: "$(TAG)"
      
  - script: >
      docker run --rm -v "$(Build.SourcesDirectory)":c:\pcl $(dockerHubID)/env:$(TAG) 
      powershell -command "mkdir c:\pcl\build; cd c:\pcl\build; 
      cmake c:\pcl -G$(GENERATOR) 
      -DVCPKG_TARGET_TRIPLET=$(PLATFORM)-windows-rel 
      -DCMAKE_BUILD_TYPE='Release' 
      -DCMAKE_TOOLCHAIN_FILE=c:\vcpkg\scripts\buildsystems\vcpkg.cmake 
      -DPCL_ONLY_CORE_POINT_TYPES=ON 
      -DBUILD_io:BOOL=OFF 
      -DBUILD_kdtree:BOOL=OFF;
      cmake --build . "
    displayName: 'Verify Dockerimage'
  - task: Docker@2
    displayName: "Push docker image"
    inputs:
      command: push
      containerRegistry: $(dockerHub)
      repository: $(dockerHubID)/env
      tags: "$(TAG)"
      condition: and(eq(variables['Build.Repository.Name'], 'PointCloudLibrary/pcl'),
                     eq(variables['Build.SourceBranch'], 'refs/heads/master'))
