parameters:
  - name: build_single
    type: string
    default: ''
  - name: linux_packages
    type: object
    default:
      - os:
          container: redhat/ubi8:latest
          identity: platform:el8
          iotedge: redhat8
        targets:
          - arch: amd64
            identity: iot-identity-service/packages/el8/amd64
            iotedged: edgelet/target/rpmbuild/RPMS/x86_64
      - os:
          container: redhat/ubi9:latest
          identity: platform:el9
          iotedge: redhat9
        targets:
          - arch: amd64
            identity: iot-identity-service/packages/el9/amd64
            iotedged: edgelet/target/rpmbuild/RPMS/x86_64
      - os:
          container: mcr.microsoft.com/mirror/docker/library/debian:bullseye
          identity: debian:11
          iotedge: debian11
        targets:
          - arch: amd64
            identity: iot-identity-service/packages/debian11/amd64
            iotedged: edgelet/target/release
          - arch: arm32v7
            identity: iot-identity-service/packages/debian11/arm32v7
            iotedged: edgelet/target/armv7-unknown-linux-gnueabihf/release
          - arch: aarch64
            identity: iot-identity-service/packages/debian11/aarch64
            iotedged: edgelet/target/aarch64-unknown-linux-gnu/release
      - os:
          container: mcr.microsoft.com/mirror/docker/library/debian:bookworm
          identity: debian:12
          iotedge: debian12
        targets:
          - arch: amd64
            identity: iot-identity-service/packages/debian12/amd64
            iotedged: edgelet/target/release
          - arch: arm32v7
            identity: iot-identity-service/packages/debian12/arm32v7
            iotedged: edgelet/target/armv7-unknown-linux-gnueabihf/release
          - arch: aarch64
            identity: iot-identity-service/packages/debian12/aarch64
            iotedged: edgelet/target/aarch64-unknown-linux-gnu/release
      - os:
          container: mcr.microsoft.com/mirror/docker/library/ubuntu:20.04
          identity: ubuntu:20.04
          iotedge: ubuntu20.04
        targets:
          - arch: amd64
            identity: iot-identity-service/packages/ubuntu2004/amd64
            iotedged: edgelet/target/release
          - arch: arm32v7
            identity: iot-identity-service/packages/ubuntu2004/arm32v7
            iotedged: edgelet/target/armv7-unknown-linux-gnueabihf/release
          - arch: aarch64
            identity: iot-identity-service/packages/ubuntu2004/aarch64
            iotedged: edgelet/target/aarch64-unknown-linux-gnu/release
      - os:
          container: mcr.microsoft.com/mirror/docker/library/ubuntu:22.04
          identity: ubuntu:22.04
          iotedge: ubuntu22.04
        targets:
          - arch: amd64
            identity: iot-identity-service/packages/ubuntu2204/amd64
            iotedged: edgelet/target/release
          - arch: arm32v7
            identity: iot-identity-service/packages/ubuntu2204/arm32v7
            iotedged: edgelet/target/armv7-unknown-linux-gnueabihf/release
          - arch: aarch64
            identity: iot-identity-service/packages/ubuntu2204/aarch64
            iotedged: edgelet/target/aarch64-unknown-linux-gnu/release
      - os:
          container: mcr.microsoft.com/mirror/docker/library/ubuntu:24.04
          identity: ubuntu:24.04
          iotedge: ubuntu24.04
        targets:
          - arch: amd64
            identity: iot-identity-service/packages/ubuntu2404/amd64
            iotedged: edgelet/target/release
          - arch: arm32v7
            identity: iot-identity-service/packages/ubuntu2404/arm32v7
            iotedged: edgelet/target/armv7-unknown-linux-gnueabihf/release
          - arch: aarch64
            identity: iot-identity-service/packages/ubuntu2404/aarch64
            iotedged: edgelet/target/aarch64-unknown-linux-gnu/release

stages:

################################################################################
  - stage: CheckBuildPackages
################################################################################
    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_edgelet
        displayName: Check Source Changes Edgelet (changes ARE in builds or edgelet)
        steps:
          - bash: |
              git log -m -1 --name-only --first-parent --pretty="" | egrep -i '^(builds|scripts|edgelet)'
              if [[ $? == 0 ]]; then
                echo "Detected changes inside builds or edgelet folders"
                echo "##vso[task.setvariable variable=EDGELETCHANGES;isOutput=true]TRUE"
              fi
            displayName: Check changes in edgelet sources
            name: check_files

################################################################################
  - stage: BuildPackages
################################################################################
    displayName: Build Packages
    dependsOn: CheckBuildPackages
    condition: |
      and(
        succeeded(), or(
          not('${{ parameters['build_single'] }}'),
          eq(dependencies.CheckBuildPackages.outputs['check_source_change_edgelet.check_files.EDGELETCHANGES'], 'true')
        )
      )
    jobs:
    ################################################################################
    # DEB/RPM packages for Linux distros
    ################################################################################
      - ${{ each build in parameters.linux_packages }}:
        - ${{ each target in build.targets }}:
          - ${{ if or(not(parameters.build_single), eq(format('{0}:{1}', build.os.iotedge, target.arch), parameters.build_single)) }}:
            - job: linux_${{ replace(build.os.iotedge, '.', '') }}_${{ target.arch }}
              displayName: Linux ${{ build.os.iotedge }} ${{ target.arch }}
              pool:
                name: $(pool.linux.name)
                demands:
                  - ImageOverride -equals agent-aziotedge-ubuntu-22.04-msmoby
              variables:
                arch: ${{ target.arch }}
                os.container: ${{ build.os.container }}
                os.identity: ${{ build.os.identity }}
                os.iotedge: ${{ build.os.iotedge }}
                target.identity: ${{ target.identity }}
                target.iotedged: ${{ target.iotedged }}
              steps:
                - bash: |
                    BASE_VERSION=`cat $BUILD_SOURCESDIRECTORY/edgelet/version.txt`
                    VERSION="$BASE_VERSION~$BUILD_BUILDNUMBER"
                    echo "Version: $VERSION"
                    echo "##vso[task.setvariable variable=VERSION;]$VERSION"
                    echo "##vso[task.setvariable variable=PACKAGE_ARCH;]$(arch)"
                    echo "##vso[task.setvariable variable=PACKAGE_OS;]$(os.iotedge)"
                  displayName: Set Version
                - bash: |
                    set -xeuo pipefail
                    identity_commit=$(
                      sed -nE \
                        's/^source = "git\+https:\/\/github\.com\/Azure\/iot-identity-service\?branch=main#([0-9a-fA-F]+)"$/\1/p' \
                        edgelet/Cargo.lock | head -n 1
                    )
                    git clone https://github.com/Azure/iot-identity-service.git
                    pushd iot-identity-service
                    git checkout "$identity_commit"
                    git submodule update --init --recursive .
                    packageVersion=$(
                      grep "PACKAGE_VERSION:" .github/workflows/packages.yaml \
                      | awk '{print $2}' \
                      | tr -d "'" \
                      | tr -d '"'
                    )
                    docker run --rm \
                      -v "$(Build.SourcesDirectory)/iot-identity-service:/src" \
                      -e 'ARCH=$(arch)' \
                      -e 'OS=$(os.identity)' \
                      -e "PACKAGE_VERSION=$packageVersion" \
                      -e "PACKAGE_RELEASE=1" \
                      --privileged \
                      '$(os.container)' \
                      '/src/ci/package.sh'
                    popd
                  displayName: Create aziot-identity packages
                - task: CopyFiles@2
                  displayName: Copy aziot-identity packages to artifact staging
                  inputs:
                    SourceFolder: $(target.identity)
                    Contents: |
                      *.deb
                      *.ddeb
                      *.rpm
                      !*-debugsource-*.rpm
                      !*-devel-*.rpm
                      !*.src.rpm
                    TargetFolder: '$(build.artifactstagingdirectory)/aziot-identity'
                - task: PublishBuildArtifacts@1
                  displayName: Publish aziot-identity artifacts
                  inputs:
                    PathtoPublish: '$(build.artifactstagingdirectory)/aziot-identity'
                    ArtifactName: 'aziot-identity-$(os.iotedge)-$(arch)'
                - bash: sudo rm -rf iot-identity-service
                  displayName: Clean up iot-identity-service
                - script: edgelet/build/linux/package.sh
                  displayName: Create aziot-edge packages
                - task: CopyFiles@2
                  displayName: Copy aziot-edge packages to artifact staging
                  inputs:
                    SourceFolder: $(target.iotedged)
                    Contents: |
                      *.deb
                      *.ddeb
                      *.rpm
                      !*-debugsource-*.rpm
                    TargetFolder: '$(build.artifactstagingdirectory)/iotedged'
                - task: PublishBuildArtifacts@1
                  displayName: Publish iotedged artifacts
                  inputs:
                    PathtoPublish: '$(build.artifactstagingdirectory)/iotedged'
                    ArtifactName: 'iotedged-$(os.iotedge)-$(arch)'

    ################################################################################
      - job: snap
    ################################################################################
        displayName: Snap
        condition: and(succeeded(), not('${{ parameters['build_single'] }}'))
        strategy:
          matrix:
            amd64:
              arch: amd64
              pool: $(pool.linux.name)
              image: agent-aziotedge-ubuntu-22.04
            aarch64:
              arch: aarch64
              pool: $(pool.linux.arm.name)
              image: agent-aziotedge-ubuntu-22.04-arm64
        pool:
          name: $(pool)
          demands:
          - ImageOverride -equals $(image)
        steps:
          - bash: |
              set -xeuo pipefail
              sudo snap refresh
              sudo snap install snapcraft --classic
              lxd init --minimal
            displayName: Set up build environment for snaps
          - bash: |
              set -xeuo pipefail
              identity_commit=$(
                sed -nE \
                  's/^source = "git\+https:\/\/github\.com\/Azure\/iot-identity-service\?branch=main#([0-9a-fA-F]+)"$/\1/p' \
                  edgelet/Cargo.lock | head -n 1
              )
              git clone https://github.com/Azure/iot-identity-service.git
              pushd iot-identity-service
              git checkout "$identity_commit"
              git submodule update --init --recursive .
              SNAPCRAFT_BUILD_INFO=1 snapcraft --use-lxd
              popd
            displayName: Create aziot-identity packages
          - task: CopyFiles@2
            displayName: Copy aziot-identity packages to artifact staging
            inputs:
              Contents: '*.snap'
              SourceFolder: '$(build.sourcesdirectory)/iot-identity-service'
              TargetFolder: '$(build.artifactstagingdirectory)/aziot-identity'
          - task: PublishBuildArtifacts@1
            displayName: Publish aziot-identity artifacts
            inputs:
              ArtifactName: 'aziot-identity-snap-$(arch)'
              PathtoPublish: '$(build.artifactstagingdirectory)/aziot-identity'
          - bash: sudo rm -rf iot-identity-service
            displayName: Clean up iot-identity-service
          - bash: |
              set -xeuo pipefail
              SNAPCRAFT_BUILD_INFO=1 snapcraft --use-lxd
            displayName: Create aziot-edge packages
          - task: CopyFiles@2
            displayName: Copy aziot-edge packages to artifact staging
            inputs:
              Contents: '*.snap'
              SourceFolder: '$(build.sourcesdirectory)'
              TargetFolder: '$(build.artifactstagingdirectory)/iotedged'
          - task: PublishBuildArtifacts@1
            displayName: Publish iotedged artifacts
            inputs:
              ArtifactName: 'iotedged-snap-$(arch)'
              PathtoPublish: '$(build.artifactstagingdirectory)/iotedged'

    ################################################################################
      - job: azurelinux
    ################################################################################
        displayName: Azure Linux
        condition:
          and(
            succeeded(),
            or(eq(variables['build.linux.mariner'], ''), eq(variables['build.linux.mariner'], true))
          )
        timeoutInMinutes: 90
        strategy:
          matrix:
            2.0-amd64:
              agent-pool: $(pool.linux.name)
              agent-image: agent-aziotedge-ubuntu-22.04-msmoby
              arch: amd64
              os: mariner
              os_id: cm
              os_version: 2
              release_tag: 2.0-stable
              target.identity: iot-identity-service/packages/mariner2/amd64
              target.iotedged: builds/mariner2/out/RPMS/x86_64
            2.0-aarch64:
              agent-pool: $(pool.linux.arm.name)
              agent-image: agent-aziotedge-ubuntu-22.04-arm64-msmoby
              arch: aarch64
              os: mariner
              os_id: cm
              os_version: 2
              release_tag: 2.0-stable
              target.identity: iot-identity-service/packages/mariner2/aarch64
              target.iotedged: builds/mariner2/out/RPMS/aarch64
            3.0-amd64:
              agent-pool: $(pool.linux.name)
              agent-image: agent-aziotedge-ubuntu-22.04-msmoby
              arch: amd64
              os: azurelinux
              os_id: azl
              os_version: 3
              release_tag: 3.0-stable
              target.identity: iot-identity-service/packages/azurelinux3/amd64
              target.iotedged: builds/azurelinux3/out/RPMS/x86_64
            3.0-aarch64:
              agent-pool: $(pool.linux.arm.name)
              agent-image: agent-aziotedge-ubuntu-22.04-arm64-msmoby
              arch: aarch64
              os: azurelinux
              os_id: azl
              os_version: 3
              release_tag: 3.0-stable
              target.identity: iot-identity-service/packages/azurelinux3/aarch64
              target.iotedged: builds/azurelinux3/out/RPMS/aarch64
        pool:
          name: $(agent-pool)
          demands:
          - ImageOverride -equals $(agent-image)
          - DiskSizeGiB -equals 100 # EFLOW requires ~85GB to build the edgelet artifacts
          - WorkFolder -equals /mnt/storage/_work
        steps:
          - bash: |
              BASE_VERSION=`cat $BUILD_SOURCESDIRECTORY/edgelet/version.txt`
              VERSION="$BASE_VERSION~$BUILD_BUILDNUMBER"  # Give deb format here; let Makefile convert to rpm format
              echo "##vso[task.setvariable variable=VERSION;]$VERSION"
              echo "##vso[task.setvariable variable=PACKAGE_ARCH;]$(arch)"
              echo "PACKAGE_OS=$(os)"
              echo "##vso[task.setvariable variable=OSVERSION;]$(os_version)"
              azurelinux_arch=$(arch)
              if [ $azurelinux_arch == "amd64" ]; then
                azurelinux_arch="x86_64"
              fi
              echo "##vso[task.setvariable variable=AZURELINUX_ARCH;]$azurelinux_arch"
            displayName: Set Version
          - bash: |
              set -xeuo pipefail
              identity_commit=$(
                sed -nE \
                  's/^source = "git\+https:\/\/github\.com\/Azure\/iot-identity-service\?branch=main#([0-9a-fA-F]+)"$/\1/p' \
                  edgelet/Cargo.lock | head -n 1
              )
              git clone https://github.com/Azure/iot-identity-service.git
              pushd iot-identity-service
              git checkout "$identity_commit"
              git submodule update --init --recursive .
              packageVersion=$(
                grep "PACKAGE_VERSION:" .github/workflows/packages.yaml \
                | awk '{print $2}' \
                | tr -d "'" \
                | tr -d '"'
              )
              docker run --rm \
                -v "$(Build.SourcesDirectory)/iot-identity-service:/src" \
                -e "ARCH=$PACKAGE_ARCH" \
                -e "OS=$OS:$OSVERSION" \
                -e "PACKAGE_VERSION=$packageVersion" \
                -e "PACKAGE_RELEASE=1" \
                --privileged \
                'mcr.microsoft.com/mirror/docker/library/ubuntu:22.04' \
                '/src/ci/package.sh'
              popd
              # aziot-edge package script expects to query the version from the identity package in the root source directory
              os_dirs='$(os)$(os_version)/$(arch)'
              os_suffix='$(os_id)$(os_version).$(AZURELINUX_ARCH)'
              sudo cp iot-identity-service/packages/$os_dirs/aziot-identity-service-$packageVersion-1.$os_suffix.rpm .
            displayName: Create aziot-identity packages
          - task: CopyFiles@2
            displayName: Copy aziot-identity build logs to artifact staging
            inputs:
              SourceFolder: iot-identity-service/AzureLinux-Build/build/logs/pkggen/rpmbuilding
              Contents: |
                **/*.rpm.log
              TargetFolder: '$(build.artifactstagingdirectory)/aziot-identity'
            condition: succeededOrFailed()
          - task: CopyFiles@2
            displayName: Copy aziot-identity packages to artifact staging
            inputs:
              SourceFolder: $(target.identity)
              Contents: |
                *.rpm
                !*-devel-*.rpm
              TargetFolder: '$(build.artifactstagingdirectory)/aziot-identity'
            condition: succeededOrFailed()
          - task: PublishBuildArtifacts@1
            displayName: Publish aziot-identity artifacts
            inputs:
              PathtoPublish: '$(build.artifactstagingdirectory)/aziot-identity'
              ArtifactName: 'aziot-identity-$(os)$(os_version)-$(arch)'
          - bash: sudo rm -rf iot-identity-service
            displayName: Clean up iot-identity-service
            condition: succeededOrFailed()
          - bash: |
              docker run --rm \
                -v "$(Build.SourcesDirectory):/src" \
                -e "ARCH=$arch" \
                -e "OS=$OS" \
                -e "AZURELINUX_RELEASE=$RELEASE_TAG" \
                -e "ARCH=$AZURELINUX_ARCH" \
                -e "VERSION=$VERSION" \
                --privileged \
                'mcr.microsoft.com/mirror/docker/library/ubuntu:22.04' \
                '/src/edgelet/build/linux/package-mariner.sh'
            displayName: Create aziot-edge packages
          - task: CopyFiles@2
            displayName: Copy aziot-edge build logs to artifact staging
            inputs:
              SourceFolder: builds/$(os)$(os_version)/build/logs/pkggen/rpmbuilding
              Contents: |
                **/*.rpm.log
              TargetFolder: '$(build.artifactstagingdirectory)/iotedged'
            condition: succeededOrFailed()
          - task: CopyFiles@2
            displayName: Copy aziot-edge packages to artifact staging
            inputs:
              SourceFolder: $(target.iotedged)
              Contents: |
                aziot-edge-*.rpm
              TargetFolder: '$(build.artifactstagingdirectory)/iotedged'
            condition: succeededOrFailed()
          - task: PublishBuildArtifacts@1
            displayName: Publish iotedged artifacts
            inputs:
              PathtoPublish: '$(build.artifactstagingdirectory)/iotedged'
              ArtifactName: 'iotedged-$(os)$(os_version)-$(arch)'
            condition: succeededOrFailed()
