parameters:
  - name: build_single
    type: string
    default: ''

stages:

################################################################################
  - stage: CheckBuildImages
################################################################################
    displayName: Check For Source Code Changes
    pool:
      name: $(pool.linux.name)
      demands:
        - ImageOverride -equals agent-aziotedge-ubuntu-22.04-msmoby
    dependsOn: []
    jobs:
      - job: check_source_change_runtime
        displayName: Check Source Changes Edgelet (changes are outside of test or doc folders)
        steps:
          - bash: |
              git log -m -1 --name-only --first-parent --pretty="" | egrep -i -v '^(test/Microsoft.Azure.Devices.Edge.Test|doc|edgelet)'
              if [[ $? == 0 ]]; then
                echo "Detected changes outside of test, doc, and edgelet folders"
                echo "##vso[task.setvariable variable=RUNTIMECHANGES;isOutput=true]TRUE"
              fi
            displayName: Check changes in runtime sources
            name: check_files

# This stage builds Rocks DB and adds the files to staging directory
################################################################################
  - stage: BuildRocksDb
################################################################################
    displayName: Build RocksDB
    dependsOn: CheckBuildImages
    condition: |
      and(
        succeeded(), or(
          not('${{ parameters.build_single }}'),
          eq(dependencies.CheckBuildImages.outputs['check_source_change_runtime.check_files.RUNTIMECHANGES'], 'true')
        )
      )
    jobs:
      - template: build-rocksdb.yaml
        parameters:
          build_single: ${{ parameters.build_single }}

################################################################################
  - stage: BuildExecutables
################################################################################
    displayName: Build Executables
    condition: |
      and(
        succeeded(), or(
          not('${{ parameters.build_single }}'),
          eq(dependencies.CheckBuildImages.outputs['check_source_change_runtime.check_files.RUNTIMECHANGES'], 'true')
        )
      )
    pool:
      name: $(pool.linux.name)
      demands:
        - ImageOverride -equals agent-aziotedge-ubuntu-22.04-msmoby
    dependsOn: CheckBuildImages
    jobs:
    - job: BuildDotnetComponents
      displayName: Build Dotnet Components
      variables:
        Codeql.Enabled: true
      steps:
      - script: scripts/linux/buildBranch.sh -c $(Build.Configuration) --no-rocksdb-bin ${{ iif(parameters.build_single, '--skip-quickstart', '') }}
        name: build
        displayName: Build ($(Build.Configuration)) dotnet artifacts
      - bash: |
          mkdir $(Build.ArtifactStagingDirectory)/core-linux && \
          mv $(Build.BinariesDirectory)/publish/{CACertificates,scripts} $(Build.ArtifactStagingDirectory)/core-linux
          if $INCLUDE_QUICKSTART; then
            mv $(Build.BinariesDirectory)/publish/{e2e_deployment_files,e2e_test_files,IotEdgeQuickstart*,LeafDevice*} $(Build.ArtifactStagingDirectory)/core-linux
          fi
        displayName: Move/stage test file artifacts
        env:
          INCLUDE_QUICKSTART: ${{ iif(eq(0, parameters.build_single), 'true', 'false') }}
      - script: scripts/linux/createArtifactInfo.sh --output-folder '$(Build.ArtifactStagingDirectory)/core-linux' --build-number $(Build.BuildNumber)
        displayName: 'Create Artifact Info File'
      - task: PublishBuildArtifacts@1
        displayName: Publish core-linux artifact
        inputs:
          PathtoPublish: '$(Build.ArtifactStagingDirectory)/core-linux'
          artifactName: 'core-linux'
      - task: PublishBuildArtifacts@1
        displayName: Publish Dotnet Artifacts
        inputs:
          PathtoPublish: '$(Build.BinariesDirectory)/publish'
          artifactName: 'dotnet_artifacts'
          StoreAsTar: true

    - template: build-api-proxy.yaml
      parameters:
        build_single: ${{ parameters.build_single }}

# This stage consolidates .NET binaries, Rocksdb binaries and the SBOM into a
# single artifact from which all Docker images can be built.
################################################################################
  - stage: ConsolidateAndPubishArtifacts
################################################################################
    displayName: Consolidate And Publish Artifacts
    pool:
      name: $(pool.linux.name)
      demands:
        - ImageOverride -equals agent-aziotedge-ubuntu-22.04-msmoby
    dependsOn: [BuildExecutables, BuildRocksDB]
    jobs:
    - job: ConsolidateAndPublishArtifacts
      steps:
      - task: DownloadBuildArtifacts@0
        displayName: Download Artifacts
        inputs:
          buildType: 'current'
          downloadPath: '$(Build.ArtifactStagingDirectory)'
          downloadType: specific
          extractTars: true

      - task: CopyFiles@2
        displayName: Copy/Stage dotnet artifacts
        inputs:
          sourceFolder: '$(Agent.TempDirectory)/extracted_tars/dotnet_artifacts'
          contents: '**'
          targetFolder: '$(Build.BinariesDirectory)/publish'

      - ${{ if or(not(parameters.build_single), endsWith(parameters.build_single, 'amd64')) }}:
        - task: CopyFiles@2
          displayName: Copy API Proxy Artifacts (amd64)
          inputs:
            sourceFolder: '$(Agent.TempDirectory)/extracted_tars/api_proxy_x86_64'
            contents: '**'
            targetFolder: '$(Build.BinariesDirectory)/publish'

      - ${{ if or(not(parameters.build_single), endsWith(parameters.build_single, 'arm32v7')) }}:
        - task: CopyFiles@2
          displayName: Copy API Proxy Artifacts (arm32v7)
          inputs:
            sourceFolder: '$(Agent.TempDirectory)/extracted_tars/api_proxy_armv7l'
            contents: '**'
            targetFolder: '$(Build.BinariesDirectory)/publish'

      - ${{ if or(not(parameters.build_single), endsWith(parameters.build_single, 'arm64v8')) }}:
        - task: CopyFiles@2
          displayName: Copy API Proxy Artifacts (arm64v8)
          inputs:
            sourceFolder: '$(Agent.TempDirectory)/extracted_tars/api_proxy_aarch64'
            contents: '**'
            targetFolder: '$(Build.BinariesDirectory)/publish'

      - task: CopyFiles@2
        displayName: Copy rocksdb libs
        inputs:
          sourceFolder: '$(Build.ArtifactStagingDirectory)/librocksdb'
          contents: '**'
          targetFolder: '$(Build.BinariesDirectory)/publish/librocksdb'

      - task: PublishBuildArtifacts@1
        displayName: 'Publish Artifacts'
        inputs:
          pathToPublish: '$(Build.BinariesDirectory)/publish'
          artifactName: 'consolidated_artifacts'
          StoreAsTar: true

################################################################################
  - stage:  BuildImages
################################################################################
    displayName: Build Images
    pool:
      name: $(pool.linux.name)
      demands:
        - ImageOverride -equals agent-aziotedge-ubuntu-22.04-msmoby
    dependsOn: ConsolidateAndPubishArtifacts
    variables:
      ${{ if eq(length(parameters.build_single), 0) }}:
        platforms: 'linux/amd64,linux/arm/v7,linux/arm64'
      ${{ elseif endsWith(parameters.build_single, 'amd64') }}:
        platforms: linux/amd64
      ${{ elseif endsWith(parameters.build_single, 'arm32v7') }}:
        platforms: linux/arm/v7
      ${{ elseif endsWith(parameters.build_single, 'arm64v8') }}:
        platforms: linux/arm64
    jobs:

    # Product images
    - job: BuildImageEdgeAgent
      steps:
      - template: image-linux.yaml
        parameters:
          displayName: Edge Agent
          name: azureiotedge-agent
          app: Microsoft.Azure.Devices.Edge.Agent.Service
          version: $(Build.BuildNumber)
          use_rocksdb: true
          platforms: $(platforms)

    - job: BuildImageEdgeHub
      steps:
      - template: image-linux.yaml
        parameters:
          displayName: Edge Hub
          name: azureiotedge-hub
          app: Microsoft.Azure.Devices.Edge.Hub.Service
          version: $(Build.BuildNumber)
          use_rocksdb: true
          platforms: $(platforms)

    - job: BuildImageTemperatureSensor
      steps:
      - template: image-linux.yaml
        parameters:
          displayName: Temperature Sensor
          name: azureiotedge-simulated-temperature-sensor
          app: SimulatedTemperatureSensor
          platforms: $(platforms)
          version: $(Build.BuildNumber)

    - job: BuildImageDiagnostics
      steps:
      - template: image-linux.yaml
        parameters:
          displayName: Diagnostics
          name: azureiotedge-diagnostics
          app: IotedgeDiagnosticsDotnet
          platforms: $(platforms)
          version: $(Build.BuildNumber)

    - job: BuildImageMetricsCollector
      steps:
      - template: image-linux.yaml
        parameters:
          displayName: Metrics Collector
          name: azureiotedge-metrics-collector
          app: Microsoft.Azure.Devices.Edge.Azure.Monitor
          platforms: $(platforms)
          version: $(Build.BuildNumber)

    - job: BuildImageApiProxy
      steps:
      - template: image-linux.yaml
        parameters:
          displayName: API Proxy
          name: azureiotedge-api-proxy
          app: api-proxy-module
          version: $(Build.BuildNumber)
          platforms: $(platforms)

      - task: ComponentGovernanceComponentDetection@0
        inputs:
          dockerImagesToScan: '$(registry.address)/microsoft/azureiotedge-api-proxy:$(Build.BuildNumber)-linux-amd64,$(registry.address)/microsoft/azureiotedge-api-proxy:$(Build.BuildNumber)-linux-arm32v7,$(registry.address)/microsoft/azureiotedge-api-proxy:$(Build.BuildNumber)-linux-arm64v8'

    # Test images
    - job: BuildImageTemperatureFilter
      steps:
      - template: image-linux.yaml
        parameters:
          displayName: Temperature Filter
          name: azureiotedge-temperature-filter
          app: TemperatureFilter
          platforms: $(platforms)
          version: $(Build.BuildNumber)

    - job: BuildImageLoadGen
      steps:
      - template: image-linux.yaml
        parameters:
          displayName: Load Gen
          name: azureiotedge-load-gen
          app: load-gen
          platforms: $(platforms)
          version: $(Build.BuildNumber)

    - job: BuildImageTestAnalyzer
      steps:
      - template: image-linux.yaml
        parameters:
          displayName: Test Analyzer
          name: azureiotedge-analyzer
          app: TestAnalyzer
          version: $(Build.BuildNumber)
          use_rocksdb: true
          platforms: $(platforms)

      # Functions Sample - Only supported on amd64
    - job: BuildImageFunctionsSample
      steps:
      - template: image-linux.yaml
        parameters:
          displayName: Functions Sample
          name: azureiotedge-functions-filter
          app: EdgeHubTriggerCSharp
          platforms: linux/amd64
          version: $(Build.BuildNumber)

    - job: BuildImageDirectMethodSender
      steps:
      - template: image-linux.yaml
        parameters:
          displayName: Direct Method Sender
          name: azureiotedge-direct-method-sender
          app: DirectMethodSender
          platforms: $(platforms)
          version: $(Build.BuildNumber)

    - job: BuildImageDirectMethodReceiver
      steps:
      - template: image-linux.yaml
        parameters:
          displayName: Direct Method Receiver
          name: azureiotedge-direct-method-receiver
          app: DirectMethodReceiver
          platforms: $(platforms)
          version: $(Build.BuildNumber)

    - job: BuildImageMetricsValidator
      steps:
      - template: image-linux.yaml
        parameters:
          displayName: Metrics Validator
          name: azureiotedge-metrics-validator
          app: MetricsValidator
          platforms: $(platforms)
          version: $(Build.BuildNumber)

    - job: BuildImageNumberLogger
      steps:
      - template: image-linux.yaml
        parameters:
          displayName: Number Logger
          name: azureiotedge-number-logger
          app: NumberLogger
          platforms: $(platforms)
          version: $(Build.BuildNumber)

    - job: BuildImageModuleRestarter
      steps:
      - template: image-linux.yaml
        parameters:
          displayName: Module Restarter
          name: azureiotedge-module-restarter
          app: ModuleRestarter
          platforms: $(platforms)
          version: $(Build.BuildNumber)

    - job: BuildImageTwinTester
      steps:
      - template: image-linux.yaml
        parameters:
          displayName: TwinTester
          name: azureiotedge-twin-tester
          app: TwinTester
          version: $(Build.BuildNumber)
          use_rocksdb: true
          platforms: $(platforms)

    - job: BuildImageRelayer
      steps:
      - template: image-linux.yaml
        parameters:
          displayName: Relayer
          name: azureiotedge-relayer
          app: Relayer
          platforms: $(platforms)
          version: $(Build.BuildNumber)

    - job: BuildImageNetworkController
      steps:
      - template: image-linux.yaml
        parameters:
          displayName: Network Controller
          name: azureiotedge-network-controller
          app: NetworkController
          version: $(Build.BuildNumber)
          use_rocksdb: true
          platforms: $(platforms)

    - job: BuildImageTestResultCoordinator
      steps:
      - template: image-linux.yaml
        parameters:
          displayName: TestResultCoordinator
          name: azureiotedge-test-result-coordinator
          app: TestResultCoordinator
          version: $(Build.BuildNumber)
          use_rocksdb: true
          platforms: $(platforms)

    - job: BuildImageTestMetricsCollector
      steps:
      - template: image-linux.yaml
        parameters:
          displayName: Test Metrics Collector
          name: azureiotedge-test-metrics-collector
          app: TestMetricsCollector
          platforms: $(platforms)
          version: $(Build.BuildNumber)

    - job: BuildImageDeploymentTester
      steps:
      - template: image-linux.yaml
        parameters:
          displayName: DeploymentTester
          name: azureiotedge-deployment-tester
          app: DeploymentTester
          platforms: $(platforms)
          version: $(Build.BuildNumber)

    - job: BuildImageEdgeHubRestartTester
      steps:
      - template: image-linux.yaml
        parameters:
          displayName: EdgeHubRestartTester
          name: azureiotedge-edgehub-restart-tester
          app: EdgeHubRestartTester
          platforms: $(platforms)
          version: $(Build.BuildNumber)

    - job: BuildImageCloudToDeviceMessageTester
      steps:
      - template: image-linux.yaml
        parameters:
          displayName: Cloud To Device Message Tester
          name: azureiotedge-c2dmessage-tester
          app: CloudToDeviceMessageTester
          platforms: $(platforms)
          version: $(Build.BuildNumber)
