jobs:

################################################################################
- job: build_windows_x64_agent
################################################################################
  displayName: Windows Agent (x64)
  pool:
    vmImage: vs2017-win2016
  steps:

  # Steps template for windows platform
  - template: windows.template.yml

  # Package dotnet core windows dependency (VC++ Redistributable)
  - powershell: |
      Write-Host "Downloading 'VC++ Redistributable' package."
      $outDir = Join-Path -Path $env:TMP -ChildPath ([Guid]::NewGuid())
      New-Item -Path $outDir -ItemType directory 
      $outFile = Join-Path -Path $outDir -ChildPath "ucrt.zip"
      Invoke-WebRequest -Uri https://vstsagenttools.blob.core.windows.net/tools/ucrt/ucrt_x64.zip -OutFile $outFile
      Write-Host "Unzipping 'VC++ Redistributable' package to agent layout."
      $unzipDir = Join-Path -Path $outDir -ChildPath "unzip"
      Add-Type -AssemblyName System.IO.Compression.FileSystem
      [System.IO.Compression.ZipFile]::ExtractToDirectory($outFile, $unzipDir)
      $agentLayoutBin = Join-Path -Path $(Build.SourcesDirectory) -ChildPath "_layout\bin"
      Copy-Item -Path $unzipDir -Destination $agentLayoutBin -Force
    displayName: Package UCRT
    condition: and(succeeded(), ne(variables['build.reason'], 'PullRequest'))

  # Create agent package zip
  - script: dev.cmd package Release
    workingDirectory: src
    displayName: Package Release
    condition: and(succeeded(), ne(variables['build.reason'], 'PullRequest'))

  # Upload agent package zip as build artifact
  - task: PublishBuildArtifacts@1
    displayName: Publish Artifact (Windows x64)
    condition: and(succeeded(), ne(variables['build.reason'], 'PullRequest'))
    inputs:
      pathToPublish: _package
      artifactName: agent
      artifactType: container

################################################################################
- job: build_linux_x64_agent
################################################################################
  displayName: Linux Agent (x64)
  pool:
    vmImage: ubuntu-16.04
  steps:

  # Steps template for non-windows platform
  - template: nonwindows.template.yml

  # Create agent package zip
  - script: ./dev.sh package Release
    workingDirectory: src
    displayName: Package Release
    condition: and(succeeded(), ne(variables['build.reason'], 'PullRequest'))

  # Upload agent package zip as build artifact
  - task: PublishBuildArtifacts@1
    displayName: Publish Artifact (Linux x64)
    condition: and(succeeded(), ne(variables['build.reason'], 'PullRequest'))
    inputs:
      pathToPublish: _package
      artifactName: agent
      artifactType: container

################################################################################
- job: build_osx_agent
################################################################################
  displayName: macOS Agent (x64)
  pool:
    vmImage: macOS-10.14
  steps:

  # Steps template for non-windows platform
  - template: nonwindows.template.yml
  
  # Create agent package zip
  - script: ./dev.sh package Release
    workingDirectory: src
    displayName: Package Release
    condition: and(succeeded(), ne(variables['build.reason'], 'PullRequest'))

  # Upload agent package zip as build artifact
  - task: PublishBuildArtifacts@1
    displayName: Publish Artifact (OSX)
    condition: and(succeeded(), ne(variables['build.reason'], 'PullRequest'))
    inputs:
      pathToPublish: _package
      artifactName: agent
      artifactType: container
