diff --git a/.github/workflows/build-website.yaml b/.github/workflows/build-website.yaml index d51cedc6f1..725f9ddeb9 100644 --- a/.github/workflows/build-website.yaml +++ b/.github/workflows/build-website.yaml @@ -93,10 +93,35 @@ jobs: - name: "\U0001F4E6 Install dependencies" run: npm ci - # The test catalog export imports the source module, which needs this module. + # The PowerShell modules the build needs, by the pinned versions in the files that pin them. On a hit the + # PowerShell Gallery is not contacted at all, which is the point: the Gallery handing a runner a source + # location it cannot use has failed this job with an error four steps removed from the cause. + - name: Restore the PowerShell modules + uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ~/.local/share/powershell/Modules + key: pwsh-modules-${{ runner.os }}-${{ hashFiles('build/Update-CommandReference.ps1', '.github/workflows/build-website.yaml') }} + restore-keys: pwsh-modules-${{ runner.os }}- + + # The test catalog export imports the source module, which needs this module. Retried, because a failed + # download here is a Gallery problem rather than a problem with the change being built. - name: Install Microsoft.Graph.Authentication shell: pwsh - run: Install-Module Microsoft.Graph.Authentication -MinimumVersion 2.27.0 -Scope CurrentUser -Repository PSGallery -Force + run: | + $ErrorActionPreference = 'Stop' + $available = { [bool](Get-Module Microsoft.Graph.Authentication -ListAvailable | + Where-Object { $_.Version -ge [version]'2.27.0' }) } + if (& $available) { 'Already available.'; exit 0 } + foreach ($attempt in 1..3) { + try { + Install-Module Microsoft.Graph.Authentication -MinimumVersion 2.27.0 -Scope CurrentUser -Repository PSGallery -Force + } catch { + Write-Host "::warning::Attempt $attempt of 3 failed: $($_.Exception.Message)" + } + if (& $available) { exit 0 } + if ($attempt -lt 3) { Start-Sleep -Seconds (5 * $attempt) } + } + throw 'Could not install Microsoft.Graph.Authentication from the PowerShell Gallery after 3 attempts.' # The Open Graph cards rendered by earlier builds, by their hash (scripts/og-images.mjs): a build renders only # the cards of the pages that changed. diff --git a/build/Update-CommandReference.ps1 b/build/Update-CommandReference.ps1 index c0f552d299..6ca144c1ab 100644 --- a/build/Update-CommandReference.ps1 +++ b/build/Update-CommandReference.ps1 @@ -6,9 +6,44 @@ $alt3DocusaurusVersion = "1.0.37" $platyPSVersion = "0.14.2" -if (-not (Get-Module Alt3.Docusaurus.Powershell -ListAvailable | Where-Object { $_.Version -eq $alt3DocusaurusVersion })) { Install-Module Alt3.Docusaurus.Powershell -RequiredVersion $alt3DocusaurusVersion -Scope CurrentUser -Force -SkipPublisherCheck } -if (-not (Get-Module PlatyPS -ListAvailable | Where-Object { $_.Version -eq $platyPSVersion })) { Install-Module PlatyPS -RequiredVersion $platyPSVersion -Scope CurrentUser -Force -SkipPublisherCheck } -if (-not (Get-Module Pester -ListAvailable | Where-Object { $_.Version -eq '5.7.1' })) { Install-Module Pester -RequiredVersion 5.7.1 -Scope CurrentUser -Force -SkipPublisherCheck } +# Installs a pinned module, retrying a failed download, and stops with a message that names the +# cause when it cannot be had at all. +# +# Install-Module writes a non-terminating error on a failed download, so without this the script +# used to carry on to the Import-Module calls below and fail there instead, with +# "The specified module 'PlatyPS' with version '0.14.2' was not loaded because no valid module file +# was found", four steps away from the PowerShell Gallery handing the runner a source location it +# could not use. A Gallery hiccup then reads as a problem with the branch being built. +function Install-PinnedModule { + param( + [Parameter(Mandatory)] [string] $Name, + [Parameter(Mandatory)] [string] $Version, + [int] $Attempts = 3 + ) + + $isAvailable = { [bool](Get-Module $Name -ListAvailable | Where-Object { $_.Version -eq $Version }) } + if (& $isAvailable) { + Write-Verbose "$Name $Version is already available." + return + } + + for ($attempt = 1; $attempt -le $Attempts; $attempt++) { + try { + Install-Module $Name -RequiredVersion $Version -Scope CurrentUser -Force -SkipPublisherCheck -ErrorAction Stop + } catch { + Write-Warning "Attempt $attempt of $Attempts to install $Name $Version failed: $($_.Exception.Message)" + } + + if (& $isAvailable) { return } + if ($attempt -lt $Attempts) { Start-Sleep -Seconds (5 * $attempt) } + } + + throw "Could not install $Name $Version from the PowerShell Gallery after $Attempts attempts. The command reference cannot be generated without it." +} + +Install-PinnedModule -Name Alt3.Docusaurus.Powershell -Version $alt3DocusaurusVersion +Install-PinnedModule -Name PlatyPS -Version $platyPSVersion +Install-PinnedModule -Name Pester -Version '5.7.1' Import-Module Alt3.Docusaurus.Powershell -RequiredVersion $alt3DocusaurusVersion Import-Module PlatyPS -RequiredVersion $platyPSVersion diff --git a/powershell/tests/general/PesterVersionPinning.Tests.ps1 b/powershell/tests/general/PesterVersionPinning.Tests.ps1 index d48b74eb78..8739c55079 100644 --- a/powershell/tests/general/PesterVersionPinning.Tests.ps1 +++ b/powershell/tests/general/PesterVersionPinning.Tests.ps1 @@ -19,7 +19,8 @@ BeforeDiscovery { } @{ Path = 'build/Update-CommandReference.ps1' - Pattern = 'Install-Module Pester -RequiredVersion 5\.7\.1' + # Doc generation installs through a retry helper that takes the version as a parameter. + Pattern = "Install-PinnedModule -Name Pester -Version '5\.7\.1'" Because = 'doc generation should install the supported Pester version when missing' } @{