# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml

name: $(Year:yyyy).$(Month).$(DayOfMonth)$(Rev:.r)$(postfix)$(branchName) # NOTE: rev resets when the default retention period expires

trigger:
  branches:
    include:
    - '*'

pool:
  vmImage: ubuntu-latest # ubuntu-latest - set to windows-latest or another Windows vmImage for Windows builds

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'  
  containerRegistry: esuite.azurecr.io
  DOCKER_BUILDKIT: 1
  
  imageName: 'e-suite.Scheduler' 
   
  publishPath: $(Build.Repository.LocalPath)/build/e-suite.Scheduler

  ${{ if eq(variables['Build.SourceBranchName'], 'master') }}:
    branchName: ''
  ${{ elseif startsWith(variables['Build.SourceBranch'], 'refs/heads/') }}:
    branchName: $[ replace(replace(variables['Build.SourceBranch'], 'refs/heads/', ''), '/', '-' ) ]
  ${{ elseif startsWith(variables['Build.SourceBranch'], 'refs/pull/') }}:
    branchName: $[ replace(replace(variables['System.PullRequest.TargetBranch'], 'refs/heads/', ''), '/', '-' ) ]

  ${{ if eq(variables['branchName'], '') }}:
    postfix: ''
  ${{ else }}:
    postfix: '-'

steps:
- task: UseDotNet@2
  displayName: 'Set .net core version'
  inputs:
    version: '9.0.x'
   
- task: DotNetCoreCLI@2
  displayName: 'Nuget Restore'
  inputs:
    command: 'restore'
    feedsToUse: config
    projects: '**/*.csproj'
    nugetConfigPath: nuget.config

- task: DotNetCoreCLI@2
  inputs:
    command: 'build'
    arguments: '--configuration $(buildConfiguration)'
  displayName: 'dotnet build $(buildConfiguration)'

- task: DotNetCoreCLI@2
  displayName: 'Run Tests'
  inputs:
    command: test
    projects: '**/*Tests/*.csproj'
    arguments: '--configuration $(buildConfiguration) --collect "Code coverage" --settings .runsettings'

- task: DotNetCoreCLI@2
  displayName: 'Publish e-suite.Scheduler'
  condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'))
  inputs:
    command: publish
    projects: 'e-suite.Scheduler/e-suite.Scheduler.csproj'
    publishWebProjects: false
    arguments: -c $(BuildConfiguration) -o build
    zipAfterPublish: false

- powershell: |
   Write-Host "Show all folder content"
   Get-ChildItem -Path $(publishPath)\*.* -Recurse -Force | % { $_.FullName }
  errorActionPreference: continue
  displayName: 'PowerShell Script List folder structure'
  condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'))
  continueOnError: true

- task: Docker@2
  displayName: Build e-suite.Scheduler Image
  condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'))
  inputs:
    command: build
    repository: $(imageName)
    buildContext: $(publishPath)
    Dockerfile: $(publishPath)/Dockerfile.Azure
    containerRegistry: |
      $(containerRegistry)
    tags: |
      $(Build.BuildNumber)

- task: Docker@2
  displayName: Push e-suite.Scheduler
  condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'))
  inputs:
    command: push
    repository: $(imageName)
    containerRegistry: |    
      $(containerRegistry)
    tags: |
      $(Build.BuildNumber)