# SPDX-FileCopyrightText: 2024-2025 Sarah Vaupel <sarah.vaupel@uniworx.de>
#
# SPDX-License-Identifier: AGPL-3.0-or-later

trigger:
  branches:
    include:
      - '*'
  tags:
    include:
      - '*'
  #paths:
  #  exclude:
  #    - CHANGELOG.md

parameters:
  - name: services
    type: object
    default:
      - name: frontend
        imageBase:
          image: devfra.azurecr.io/de.fraport.build/npm
          tag: node-20
        # extraBuildOptions: |
        #   --build-arg NPM_CUSTOM_REGISTRY=https://pkgs.dev.azure.com/fraport/_packaging/packages/npm/registry/
        dependsOn: []
        dependenciesCaches:
          - key: package.json | package-lock.json
            path: node_modules/
          - key: package.json | package-lock.json
            path: .npm/
          - key: package.json | esbuild.config.mjs | utils/renamer.pl | utils/faviconize.pl | frontend/src/icons.scss
            path: assets/icons/
          - key: package.json | esbuild.config.mjs | utils/renamer.pl | utils/faviconize.pl | frontend/src/icons.scss
            path: assets/favicons/
        buildPool: 'Prod Private Agent Pool'
        buildCores: 1
        buildTimeout: 60
        buildArtifacts: |
          assets/icons/fradrive/*.svg
          assets/favicons/*.png
          assets/favicons/include.html
          frontend/src/env.sass
          config/manifest.json
          static/**/*
          well-known/**/*
      - name: backend
        imageBase:
          image: devfra.azurecr.io/de.fraport.build/haskell
          tag: 8.10.4
        dependsOn:
          - Build_frontend
        dependenciesCaches:
          - key: stack.yaml | stack.yaml.lock
            path: .stack/
        buildPool: 'Prod Private Agent Pool DS3'
        buildCores: 3
        buildTimeout: 1440
        buildArtifacts: |
          bin/*

variables:
  buildImageUpstream: devfra.azurecr.io/de.fraport.fradrive.build
  setupImages: $[ or( eq(variables.forcePushLatest, true), eq(variables['Build.SourceBranch'], 'refs/heads/master'), startsWith(variables['Build.SourceBranch'], 'refs/heads/update'), startsWith(variables['Build.SourceBranch'], 'refs/tags/') ) ]

pool: 'Prod Private Agent Pool'

stages:

- stage: Setup
  jobs:
    - ${{ each service in parameters.services }}:
      - template: .azure-pipelines/templates/jobs/setup_image.yaml
        parameters:
          imageName: ${{service.name}}
          imageBase: ${{service.imageBase}}
      - template: .azure-pipelines/templates/jobs/setup_dependencies.yaml
        parameters:
          serviceName: ${{service.name}}
          dependenciesCaches: ${{service.dependenciesCaches}}
          dependenciesBuildPool: ${{service.buildPool}}
          dependenciesBuildCores: ${{service.buildCores}}
          dependenciesBuildTimeout: ${{service.buildTimeout}}

- stage: Build
  dependsOn: Setup
  jobs:
    - ${{ each service in parameters.services }}:
      - job: Build_${{service.name}}
        displayName: Compile ${{service.name}}
        dependsOn: ${{service.dependsOn}}
        pool: '${{service.buildPool}}'
        timeoutInMinutes: ${{service.buildTimeout}}
        container:
          ${{ if eq(variables.setupImages, true) }}:
            image: $(buildImageUpstream)/${{service.name}}:$(Build.BuildNumber)
          ${{ else }}:
            image: $(buildImageUpstream)/${{service.name}}:latest
          endpoint: devfra
          env:
            PROJECT_DIR: $(Build.Repository.LocalPath)
            IN_CONTAINER: true
            IN_CI: true
        steps:
          - ${{ each dependencyCache in service.dependenciesCaches }}:
            - template: .azure-pipelines/templates/steps/cache.yaml
              parameters:
                cacheIdent: '${{service.name}}-dependencies'
                cacheKeys: '${{dependencyCache.key}}'
                cachePath: '${{dependencyCache.path}}'
          - ${{ each dependency in service.dependsOn }}:
            - template: .azure-pipelines/templates/steps/artifact-download.yaml
              parameters:
                artifactName: '${{dependency}}'
          - template: .azure-pipelines/templates/steps/make.yaml
            parameters:
              makeJob: compile
              makeService: ${{service.name}}
              makeVars: 'CPU_CORES=${{service.buildCores}} STACK_CORES=-j${{service.buildCores}}'
          - task: CopyFiles@2
            displayName: Prepare ${{service.name}} build artifacts for upload
            inputs:
              Contents: '${{service.buildArtifacts}}'
              TargetFolder: '$(Build.ArtifactStagingDirectory)'
          - task: PublishBuildArtifacts@1
            displayName: Publish ${{service.name}} build artifacts
            inputs:
              PathtoPublish: '$(Build.ArtifactStagingDirectory)'
              ArtifactName: 'Build_${{service.name}}'
              publishLocation: 'Container'

# - stage: Test
#   dependsOn: Build
#   condition: eq(variables.skipTests, false)
#   jobs:
#     - ${{ each service in parameters.services }}:
#       - job: Test_${{service.name}}
#         displayName: Run ${{service.name}} tests
#         pool: '${{service.buildPool}}'
#         timeoutInMinutes: ${{service.buildTimeout}}
#         container:
#           # TODO: do not use latest on update branches
#           image: $(buildImageUpstream)/${{service.name}}:latest
#           endpoint: devfra
#           env:
#             PROJECT_DIR: $(Build.Repository.LocalPath)
#             IN_CONTAINER: true
#             IN_CI: true
#         steps:
#           - ${{ each dependencyCache in service.dependenciesCaches }}:
#             - template: .azure-pipelines/templates/steps/cache.yaml
#               parameters:
#                 cacheIdent: '${{service.name}}-dependencies'
#                 cacheKeys: '${{dependencyCache.key}}'
#                 cachePath: '${{dependencyCache.path}}'
#           - ${{ each dependency in service.dependsOn }}:
#             - template: .azure-pipelines/templates/steps/artifact-download.yaml
#               parameters:
#                 artifactName: '${{dependency}}'
#           - task: Docker@2
#             displayName: Login to container registry
#             inputs:
#               command: login
#               containerRegistry: devfra
#           - task: Bash@3
#             displayName: Start database container for testing
#             inputs:
#               targetType: inline
#               script: |
#                 docker run -d devfra.azurecr.io/de.fraport.trusted/postgres:16.1-bookworm
#           - template: .azure-pipelines/templates/steps/make.yaml
#             parameters:
#               makeJob: lint
#               makeService: ${{service.name}}
#               makeVars: 'CPU_CORES=${{service.buildCores}} STACK_CORES=-j${{service.buildCores}}'
#           - template: .azure-pipelines/templates/steps/make.yaml
#             parameters:
#               makeJob: test
#               makeService: ${{service.name}}
#               makeVars: 'CPU_CORES=${{service.buildCores}} STACK_CORES=-j${{service.buildCores}}'
#           - task: Docker@2
#             displayName: Logout from container registry
#             inputs:
#               command: logout
#               containerRegistry: devfra
#       - job: TestReport_${{service.name}}
#         displayName: Upload test reports for ${{service.name}}
#         steps:
#           - script: echo "Work in progress" # TODO

- stage: Release
  dependsOn: Build # TODO Test
  condition: or(eq(variables.forceRelease, true), startsWith(variables['Build.SourceBranch'], 'refs/tags/'))
  jobs:
    - template: .azure-pipelines/templates/jobs/release.yaml
      parameters:
        releaseTag: ${{split(variables['Build.SourceBranch'], '/')[2]}}