name: UnifiedPackageBuild-$(Build.BuildId)
trigger:
  branches:
    include:
    - master
    - release*
pr:
  branches:
    include:
    - master
    - release*

parameters:
  - name: ForceAzureBlobDelete
    displayName: Delete Azure Blob
    type: string
    values:
      - true
      - false
    default: false

resources:
  repositories:
  - repository: ComplianceRepo
    type: github
    endpoint: ComplianceGHRepo
    name: PowerShell/compliance
    ref: master

variables:
  - name: DOTNET_CLI_TELEMETRY_OPTOUT
    value: 1
  - name: POWERSHELL_TELEMETRY_OPTOUT
    value: 1
  - name: NugetSecurityAnalysisWarningLevel
    value: none
  - name: branchCounterKey
    value: $[format('{0:yyyyMMdd}-{1}', pipeline.startTime,variables['Build.SourceBranch'])]
  - name: branchCounter
    value: $[counter(variables['branchCounterKey'], 1)]
  - name: ForceAzureBlobDelete
    value:  ${{ parameters.ForceAzureBlobDelete }}
  - name: Github_Build_Repository_Uri
    value: https://github.com/powershell/powershell
  - name: SBOMGenerator_Formats
    value: spdx:2.2

stages:
  - stage: prep
    jobs:
      - template: templates/checkAzureContainer.yml

  - stage: macos
    dependsOn: ['prep']
    jobs:
      - template: templates/mac.yml
        parameters:
          buildArchitecture: x64

      - template: templates/mac.yml
        parameters:
          buildArchitecture: arm64

      - template: templates/mac-file-signing.yml
        parameters:
          buildArchitecture: x64

      - template: templates/mac-file-signing.yml
        parameters:
          buildArchitecture: arm64

      - template: templates/mac-package-build.yml
        parameters:
          buildArchitecture: x64

      - template: templates/mac-package-build.yml
        parameters:
          buildArchitecture: arm64

      - template: templates/mac-package-signing.yml
        parameters:
          buildArchitecture: x64

      - template: templates/mac-package-signing.yml
        parameters:
          buildArchitecture: arm64

  - stage: linux
    dependsOn: ['prep']
    jobs:
      - template: templates/linux.yml
        parameters:
          buildName: deb

      - template: templates/linux.yml
        parameters:
          buildName: rpm
          uploadDisplayName: Upload and Sign

      - template: templates/linux.yml
        parameters:
          buildName: fxdependent
          parentJob: build_deb

      - template: templates/linux.yml
        parameters:
          buildName: alpine

  - stage: windows
    dependsOn: ['prep']
    jobs:
      - template: templates/windows-hosted-build.yml
        parameters:
          Architecture: x64

      - template: templates/windows-hosted-build.yml
        parameters:
          Architecture: x64
          BuildConfiguration: minSize

      - template: templates/windows-hosted-build.yml
        parameters:
          Architecture: x86

      - template: templates/windows-hosted-build.yml
        parameters:
          Architecture: arm

      - template: templates/windows-hosted-build.yml
        parameters:
          Architecture: arm64

      - template: templates/windows-hosted-build.yml
        parameters:
          Architecture: fxdependent

      - template: templates/windows-hosted-build.yml
        parameters:
          Architecture: fxdependentWinDesktop

      - template: templates/windows-packaging.yml
        parameters:
          Architecture: x64
          parentJob: build_windows_x64_release

      - template: templates/windows-packaging.yml
        parameters:
          Architecture: x64
          BuildConfiguration: minSize
          parentJob: build_windows_x64_minSize

      - template: templates/windows-packaging.yml
        parameters:
          Architecture: x86
          parentJob: build_windows_x86_release

      - template: templates/windows-packaging.yml
        parameters:
          Architecture: arm
          parentJob: build_windows_arm_release

      - template: templates/windows-packaging.yml
        parameters:
          Architecture: arm64
          parentJob: build_windows_arm64_release

      - template: templates/windows-packaging.yml
        parameters:
          Architecture: fxdependent
          parentJob: build_windows_fxdependent_release

      - template: templates/windows-packaging.yml
        parameters:
          Architecture: fxdependentWinDesktop
          parentJob: build_windows_fxdependentWinDesktop_release

      - template: templates/windows-package-signing.yml
        parameters:
          parentJobs:
            - sign_windows_x64_release
            - sign_windows_x64_minSize
            - sign_windows_x86_release
            - sign_windows_arm_release
            - sign_windows_arm64_release
            - sign_windows_fxdependent_release
            - sign_windows_fxdependentWinDesktop_release

  - stage: compliance
    dependsOn: ['windows']
    jobs:
      - template: templates/compliance.yml

  - stage: nuget_and_json
    dependsOn: ['windows','linux','macOS']
    jobs:
      - template: templates/nuget.yml

      - template: templates/json.yml

  - stage: test_and_release_artifacts
    dependsOn: ['prep']
    jobs:
      - template: templates/testartifacts.yml

      - job: release_json
        displayName: Create and Upload release.json
        pool:
          name: PowerShell1ES
          demands:
          - ImageOverride -equals MMS2019
        steps:
        - checkout: self
          clean: true
        - template: templates/SetVersionVariables.yml
          parameters:
            ReleaseTagVar: $(ReleaseTagVar)

        - powershell: |
            $metadata = Get-Content '$(Build.SourcesDirectory)/tools/metadata.json' -Raw | ConvertFrom-Json
            $LTS = $metadata.LTSRelease.Latest
            @{ ReleaseVersion = "$(Version)"; LTSRelease = $LTS } | ConvertTo-Json | Out-File "$(Build.StagingDirectory)\release.json"
            Get-Content "$(Build.StagingDirectory)\release.json"
            Write-Host "##vso[artifact.upload containerfolder=metadata;artifactname=metadata]$(Build.StagingDirectory)\release.json"
          displayName: Create and upload release.json file to build artifact
