name: '$(Major).$(Minor).$(Patch)-$(SourceBranchName)'

trigger:
  batch: true
  branches:
    include:
      - '*'

variables:
  # Major Version einstellen
  - name: 'Major'
    value: '4'
  # Minor Version einstellen
  - name: 'Minor'
    value: '5'
  - name: 'Patch'
    value: "$[counter(format('{0}.{1}', variables['Major'], variables['Minor']),0)]"
  - name: 'BuildUniqueID'
    value: '$(Build.BuildID)-$(Agent.Id)-$(System.DefinitionId)-$(System.JobId)'
  - group: 'GithubCMF'
  # Docker Tag Variablen
  - name: 'DockerTagSourceBranch'
    value: $[replace(replace(variables['Build.SourceBranch'], 'refs/heads/', ''), '/', '_')]
  - name: 'CommitHash'
    value: $[replace(variables['Build.SourceVersion'], format('{0}-', variables['Build.SourceBranchName']), '')]
  - name: 'DockerTag'
    value: |
          $(Build.BuildNumber)-$(Build.SourceVersion)
          $(Major).$(Minor).$(Patch)-$(DockerTagSourceBranch)-$(CommitHash)

jobs:
  - job: unittests
    displayName: Unit Tests
    pool:
      name: 'Default'
      demands:
        - Agent.OS -equals Linux
        - docker
    steps:
      - task: npmAuthenticate@0
        displayName: 'npm auth'
        inputs:
          workingFile: .npmrc
          customEndpoint: GitHub-read-packages
      - bash: |
          echo Build and Run Tests in docker
          docker build . \
            -f $(Build.SourcesDirectory)/Dockerfile \
            -t paragondata/test-$(BuildUniqueID):$(Build.BuildId) \
            --no-cache \
            --target test \
            --build-arg SEMVERSION=$(Major).$(Minor).$(Patch) \
            --build-arg BuildUniqueID=$(BuildUniqueID)
        displayName: Build and Run Tests in docker
      - bash: |
          echo Copy Tests from docker container
          docker run --name isatest$(BuildUniqueID) --rm -d paragondata/test-$(BuildUniqueID):$(Build.BuildId)
          docker cp isatest$(BuildUniqueID):/app/testresults $(Build.StagingDirectory)/testresults
          docker cp isatest$(BuildUniqueID):/app/coverage $(Build.StagingDirectory)/coverage
          docker stop isatest$(BuildUniqueID)
        condition: always()
        displayName: Copy Tests from docker container
      - task: PublishTestResults@2
        displayName: Publish Test results
        inputs:
          testResultsFiles: '**/TESTS-*.xml'
          searchFolder: $(Build.StagingDirectory)/testresults
          testResultsFormat: JUnit
          mergeTestResults: false
          failTaskOnFailedTests: true
        condition: always()
      - task: PublishCodeCoverageResults@2
        displayName: Publish code Coverage
        inputs:
          codeCoverageTool: Cobertura
          summaryFileLocation: $(Build.StagingDirectory)/coverage/**/cobertura-coverage.xml
          pathToSources: $(Build.SourcesDirectory)
        condition: always()
      - bash: |
          echo ##############
          docker images -f "label=build.uniqueid=$(BuildUniqueID)"
          echo ##############
          if [[ $(docker images -q -f "label=build.uniqueid=$(BuildUniqueID)") ]]; then
            docker rmi -f $(docker images -q -f "label=build.uniqueid=$(BuildUniqueID)")
          else
            echo "build wurde nicht gefunden und nicht gelöscht"
          fi
        displayName: Remove docker image
        condition: always()

  - job: cibuild_debug
    displayName: ISAClient CI Debug
    pool:
      name: 'Default'
      demands:
        - Agent.OS -equals Linux
        - docker
    condition: and(ne(variables['Build.SourceBranch'], 'refs/heads/integration'), ne(variables['Build.SourceBranch'], 'refs/heads/master'), not(startsWith(variables['Build.SourceBranch'], 'refs/heads/hotfix/')), not(startsWith(variables['Build.SourceBranch'], 'refs/heads/release/')))
    steps:
      - task: npmAuthenticate@0
        displayName: 'npm auth'
        inputs:
          workingFile: .npmrc
          customEndpoint: GitHub-read-packages
      - task: Docker@2
        displayName: 'build ISAClient Debug'
        inputs:
          containerRegistry: 'Harbor isa'
          repository: isa/ui
          command: 'build'
          Dockerfile: Dockerfile
          buildContext:
          tags: '$(DockerTag)'
          arguments: |
            --no-cache
            --target publish
            --build-arg SEMVERSION=$(Major).$(Minor).$(Patch)
            --build-arg BuildUniqueID=$(BuildUniqueID)
            --label BuildType=Debug

      - task: Docker@2
        displayName: 'push ISAClient Debug'
        inputs:
          containerRegistry: 'Harbor isa'
          repository: 'isa/ui'
          command: 'push'
          tags: '$(DockerTag)'

      - task: PublishBuildArtifacts@1
        displayName: 'Artefakt veröffentlichen: HelmValues'
        inputs:
          PathtoPublish: helmvalues
          ArtifactName: HelmValues

      - task: PublishBuildArtifacts@1
        displayName: 'Artefakt veröffentlichen: ISAClientConfigs'
        inputs:
          PathtoPublish: 'apps/isa-app/src/config'
          ArtifactName: SalesConfigs

      - bash: |
          echo ##############
          docker images -f "label=build.uniqueid=$(BuildUniqueID)"
          echo ##############
          if [[ $(docker images -q -f "label=build.uniqueid=$(BuildUniqueID)") ]]; then
            docker rmi -f $(docker images -q -f "label=build.uniqueid=$(BuildUniqueID)")
          else
            echo "build wurde nicht gefunden und nicht gelöscht"
          fi
        displayName: Remove docker image
        condition: always()

  - job: cibuild_prod
    displayName: ISAClient CI Prod
    pool:
      name: 'Default'
      demands:
        - Agent.OS -equals Linux
        - docker
    condition: or(eq(variables['Build.SourceBranch'], 'refs/heads/integration'), eq(variables['Build.SourceBranch'], 'refs/heads/master'), startsWith(variables['Build.SourceBranch'], 'refs/heads/hotfix/'), startsWith(variables['Build.SourceBranch'], 'refs/heads/release/'))
    steps:
      - task: npmAuthenticate@0
        displayName: 'npm auth'
        inputs:
          workingFile: .npmrc
          customEndpoint: GitHub-read-packages
      - task: Docker@2
        displayName: 'build ISAClient Prod'
        inputs:
          containerRegistry: 'Harbor isa'
          repository: isa/ui
          command: 'build'
          Dockerfile: Dockerfile
          buildContext:
          tags: '$(DockerTag)'
          arguments: |
            --no-cache
            --target publish
            --build-arg IS_PRODUCTION=true
            --build-arg SEMVERSION=$(Major).$(Minor).$(Patch)
            --build-arg BuildUniqueID=$(BuildUniqueID)
            --label BuildType=Production

      - task: Docker@2
        displayName: 'push ISAClient Prod'
        inputs:
          containerRegistry: 'Harbor isa'
          repository: 'isa/ui'
          command: 'push'
          tags: '$(DockerTag)'

      - task: PublishBuildArtifacts@1
        displayName: 'Artefakt veröffentlichen: HelmValues'
        inputs:
          PathtoPublish: helmvalues
          ArtifactName: HelmValues

      - task: PublishBuildArtifacts@1
        displayName: 'Artefakt veröffentlichen: ISAClientConfigs'
        inputs:
          PathtoPublish: 'apps/isa-app/src/config'
          ArtifactName: SalesConfigs

      - bash: |
          echo ##############
          docker images -f "label=build.uniqueid=$(BuildUniqueID)"
          echo ##############
          if [[ $(docker images -q -f "label=build.uniqueid=$(BuildUniqueID)") ]]; then
            docker rmi -f $(docker images -q -f "label=build.uniqueid=$(BuildUniqueID)")
          else
            echo "build wurde nicht gefunden und nicht gelöscht"
          fi
        displayName: Remove docker image
        condition: always()
