name: $(version)
trigger: none
pr: none

variables:
  Codeql.Enabled: false
  DisableDockerDetector: true
  NugetSecurityAnalysisWarningLevel: warn

stages:
################################################################################
- stage: BuildExecutables
################################################################################
  displayName: Build Executables
  pool:
    name: $(pool.linux.name)
    demands:
      - ImageOverride -equals agent-aziotedge-ubuntu-20.04-docker  
  dependsOn: []
  jobs:
  # Build API Proxy executable
  - template: templates/build-api-proxy.yaml

# TODO: Consider removing this consolidate stage, which is here mostly for
# consistency with build-images.yaml.
################################################################################
- stage: ConsolidateAndPubishArtifacts
################################################################################
  displayName: Consolidate And Publish Artifacts
  pool:
    name: $(pool.linux.name)
    demands:
      - ImageOverride -equals agent-aziotedge-ubuntu-20.04-docker  
  dependsOn: BuildExecutables
  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 API Proxy Artifacts (amd64)
      inputs:
        sourceFolder: '$(Agent.TempDirectory)/extracted_tars/api_proxy_x86_64'
        contents: '**'
        targetFolder: '$(Build.BinariesDirectory)/publish'       

    - task: CopyFiles@2
      displayName: Copy API Proxy Artifacts (arm32)
      inputs:
        sourceFolder: '$(Agent.TempDirectory)/extracted_tars/api_proxy_armv7l'
        contents: '**'
        targetFolder: '$(Build.BinariesDirectory)/publish'    

    - task: CopyFiles@2
      displayName: Copy API Proxy Artifacts (arm64)
      inputs:
        sourceFolder: '$(Agent.TempDirectory)/extracted_tars/api_proxy_aarch64'
        contents: '**'
        targetFolder: '$(Build.BinariesDirectory)/publish'
        
    - task: AzureArtifacts.manifest-generator-task.manifest-generator-task.ManifestGeneratorTask@0
      displayName: 'SBOM Generation Task'
      inputs:
          BuildDropPath: '$(Build.BinariesDirectory)/publish'

    - 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-20.04-docker
  dependsOn: ConsolidateAndPubishArtifacts
  jobs:
  - job: BuildImageApiProxy
    steps:
    - bash: |
        if [ -z '$(version)' ]; then
          echo '##vso[task.setvariable variable=buildVersion]$(Build.BuildNumber)'
        else
          echo '##vso[task.setvariable variable=buildVersion]$(version)'
        fi
      displayName: 'Set build version'
    - template: templates/image-linux.yaml
      parameters:
        displayName: API Proxy
        name: azureiotedge-api-proxy
        app: api-proxy-module
        version: $(buildVersion)
    - task: ComponentGovernanceComponentDetection@0
      inputs:
        dockerImagesToScan: '$(registry.address)/microsoft/azureiotedge-api-proxy:$(buildVersion)-linux-amd64,$(registry.address)/microsoft/azureiotedge-api-proxy:$(buildVersion)-linux-arm32v7,$(registry.address)/microsoft/azureiotedge-api-proxy:$(buildVersion)-linux-arm64v8'
