|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +parameters: |
| 5 | + - name: AzureSubscription |
| 6 | + type: string |
| 7 | + default: 'ACR Images Push Service Connection' |
| 8 | + - name: RegistryUri |
| 9 | + type: string |
| 10 | + default: 'https://msgraphprodregistry.azurecr.io' |
| 11 | + - name: RepositoryName |
| 12 | + type: string |
| 13 | + default: 'MicrosoftGraphAcr' |
| 14 | + - name: ArtifactPath |
| 15 | + type: string |
| 16 | + default: '$(System.DefaultWorkingDirectory)/drop' |
| 17 | + |
| 18 | +steps: |
| 19 | + - task: AzurePowerShell@5 |
| 20 | + displayName: 'Publish PowerShell packages to ACR' |
| 21 | + inputs: |
| 22 | + azureSubscription: ${{ parameters.AzureSubscription }} |
| 23 | + ScriptType: InlineScript |
| 24 | + azurePowerShellVersion: LatestVersion |
| 25 | + pwsh: true |
| 26 | + Inline: | |
| 27 | + $ErrorActionPreference = 'Stop' |
| 28 | + $minimumPsResourceGetVersion = [version]'1.1.1' |
| 29 | + $installedModule = Get-Module -ListAvailable -Name Microsoft.PowerShell.PSResourceGet | |
| 30 | + Where-Object Version -GE $minimumPsResourceGetVersion | |
| 31 | + Sort-Object Version -Descending | |
| 32 | + Select-Object -First 1 |
| 33 | +
|
| 34 | + if ($null -eq $installedModule) { |
| 35 | + Install-Module -Name Microsoft.PowerShell.PSResourceGet -MinimumVersion $minimumPsResourceGetVersion -Scope CurrentUser -Force -AllowClobber |
| 36 | + } |
| 37 | + Import-Module Microsoft.PowerShell.PSResourceGet -MinimumVersion $minimumPsResourceGetVersion -Force |
| 38 | +
|
| 39 | + $repositoryName = '${{ parameters.RepositoryName }}' |
| 40 | + $existingRepository = Get-PSResourceRepository -Name $repositoryName -ErrorAction SilentlyContinue |
| 41 | + if ($null -ne $existingRepository) { |
| 42 | + Unregister-PSResourceRepository -Name $repositoryName |
| 43 | + } |
| 44 | + Register-PSResourceRepository -Name $repositoryName -Uri '${{ parameters.RegistryUri }}' |
| 45 | +
|
| 46 | + Add-Type -AssemblyName System.IO.Compression.FileSystem |
| 47 | + function Get-PackageMetadata { |
| 48 | + param([Parameter(Mandatory)][string] $PackagePath) |
| 49 | +
|
| 50 | + $archive = [System.IO.Compression.ZipFile]::OpenRead($PackagePath) |
| 51 | + try { |
| 52 | + $nuspecEntry = $archive.Entries | Where-Object FullName -Like '*.nuspec' | Select-Object -First 1 |
| 53 | + if ($null -eq $nuspecEntry) { |
| 54 | + throw "Package '$PackagePath' doesn't contain a nuspec file." |
| 55 | + } |
| 56 | +
|
| 57 | + $reader = [System.IO.StreamReader]::new($nuspecEntry.Open()) |
| 58 | + try { |
| 59 | + [xml] $nuspec = $reader.ReadToEnd() |
| 60 | + } |
| 61 | + finally { |
| 62 | + $reader.Dispose() |
| 63 | + } |
| 64 | +
|
| 65 | + return [pscustomobject]@{ |
| 66 | + Id = [string]$nuspec.package.metadata.id |
| 67 | + Version = [string]$nuspec.package.metadata.version |
| 68 | + Path = $PackagePath |
| 69 | + } |
| 70 | + } |
| 71 | + finally { |
| 72 | + $archive.Dispose() |
| 73 | + } |
| 74 | + } |
| 75 | +
|
| 76 | + $packages = @(Get-ChildItem -Path '${{ parameters.ArtifactPath }}' -Recurse -File -Filter 'Microsoft.Graph*.nupkg' | |
| 77 | + ForEach-Object { Get-PackageMetadata -PackagePath $_.FullName }) |
| 78 | + if ($packages.Count -eq 0) { |
| 79 | + throw "No Microsoft Graph PowerShell packages were found under '${{ parameters.ArtifactPath }}'." |
| 80 | + } |
| 81 | +
|
| 82 | + $packages = $packages | Sort-Object @{ |
| 83 | + Expression = { |
| 84 | + if ($_.Id -eq 'Microsoft.Graph.Authentication') { 0 } |
| 85 | + elseif ($_.Id -in @('Microsoft.Graph', 'Microsoft.Graph.Beta')) { 2 } |
| 86 | + else { 1 } |
| 87 | + } |
| 88 | + }, Id |
| 89 | +
|
| 90 | + foreach ($package in $packages) { |
| 91 | + $existingPackage = Find-PSResource -Name $package.Id -Version $package.Version -Repository $repositoryName -ErrorAction SilentlyContinue |
| 92 | + if ($null -ne $existingPackage) { |
| 93 | + Write-Host "$($package.Id) $($package.Version) already exists in $repositoryName; continuing the idempotent deployment." |
| 94 | + continue |
| 95 | + } |
| 96 | +
|
| 97 | + Write-Host "Publishing $($package.Id) $($package.Version) to $repositoryName." |
| 98 | + Publish-PSResource -NupkgPath $package.Path -Repository $repositoryName -ErrorAction Stop |
| 99 | + } |
| 100 | +
|
| 101 | + foreach ($package in $packages) { |
| 102 | + $foundPackage = $null |
| 103 | + for ($attempt = 1; $attempt -le 6 -and $null -eq $foundPackage; $attempt++) { |
| 104 | + $foundPackage = Find-PSResource -Name $package.Id -Version $package.Version -Repository $repositoryName -ErrorAction SilentlyContinue |
| 105 | + if ($null -eq $foundPackage -and $attempt -lt 6) { |
| 106 | + Start-Sleep -Seconds 10 |
| 107 | + } |
| 108 | + } |
| 109 | + if ($null -eq $foundPackage) { |
| 110 | + throw "Published package '$($package.Id)' version '$($package.Version)' couldn't be found in ACR." |
| 111 | + } |
| 112 | + } |
0 commit comments