pipeline.build.ps1 (255 lines of code) (raw):

# Copyright (c) Microsoft Corporation. # Licensed under the MIT License. [CmdletBinding()] param ( [Parameter(Mandatory = $False)] [String]$Build = '0.0.1', [Parameter(Mandatory = $False)] [String]$Configuration = 'Debug', [Parameter(Mandatory = $False)] [String]$ApiKey, [Parameter(Mandatory = $False)] [Switch]$CodeCoverage = $False, [Parameter(Mandatory = $False)] [String]$ArtifactPath = (Join-Path -Path $PWD -ChildPath out/modules), [Parameter(Mandatory = $False)] [String]$AssertStyle = 'AzurePipelines' ) Write-Host -Object "[Pipeline] -- PowerShell: v$($PSVersionTable.PSVersion.ToString())" -ForegroundColor Green; Write-Host -Object "[Pipeline] -- PWD: $PWD" -ForegroundColor Green; Write-Host -Object "[Pipeline] -- ArtifactPath: $ArtifactPath" -ForegroundColor Green; Write-Host -Object "[Pipeline] -- BuildNumber: $($Env:BUILD_BUILDNUMBER)" -ForegroundColor Green; Write-Host -Object "[Pipeline] -- SourceBranch: $($Env:BUILD_SOURCEBRANCH)" -ForegroundColor Green; Write-Host -Object "[Pipeline] -- SourceBranchName: $($Env:BUILD_SOURCEBRANCHNAME)" -ForegroundColor Green; Write-Host -Object "[Pipeline] -- Culture: $((Get-Culture).Name), $((Get-Culture).Parent)" -ForegroundColor Green; if ($Env:SYSTEM_DEBUG -eq 'true') { $VerbosePreference = 'Continue'; } if ($Env:BUILD_SOURCEBRANCH -like '*/tags/*' -and $Env:BUILD_SOURCEBRANCHNAME -like 'v0.*') { $Build = $Env:BUILD_SOURCEBRANCHNAME.Substring(1); } $version = $Build; $versionSuffix = [String]::Empty; if ($version -like '*-*') { [String[]]$versionParts = $version.Split('-', [System.StringSplitOptions]::RemoveEmptyEntries); $version = $versionParts[0]; if ($versionParts.Length -eq 2) { $versionSuffix = $versionParts[1]; } } Write-Host -Object "[Pipeline] -- Using version: $version" -ForegroundColor Green; Write-Host -Object "[Pipeline] -- Using versionSuffix: $versionSuffix" -ForegroundColor Green; if ($Env:COVERAGE -eq 'true') { $CodeCoverage = $True; } # Copy the PowerShell modules files to the destination path function CopyModuleFiles { param ( [Parameter(Mandatory = $True)] [String]$Path, [Parameter(Mandatory = $True)] [String]$DestinationPath ) process { $sourcePath = Resolve-Path -Path $Path; Get-ChildItem -Path $sourcePath -Recurse -File -Include *.ps1,*.psm1,*.psd1,*.ps1xml,*.yaml | Where-Object -FilterScript { ($_.FullName -notmatch '(\.(cs|csproj)|(\\|\/)(obj|bin))') } | ForEach-Object -Process { $filePath = $_.FullName.Replace($sourcePath, $destinationPath); $parentPath = Split-Path -Path $filePath -Parent; if (!(Test-Path -Path $parentPath)) { $Null = New-Item -Path $parentPath -ItemType Directory -Force; } Copy-Item -Path $_.FullName -Destination $filePath -Force; }; } } task VersionModule ModuleDependencies, { $modulePath = Join-Path -Path $ArtifactPath -ChildPath PSDocs.Azure; $manifestPath = Join-Path -Path $modulePath -ChildPath PSDocs.Azure.psd1; Write-Verbose -Message "[VersionModule] -- Checking module path: $modulePath"; if (![String]::IsNullOrEmpty($Build)) { # Update module version if (![String]::IsNullOrEmpty($version)) { Write-Verbose -Message "[VersionModule] -- Updating module manifest ModuleVersion"; Update-ModuleManifest -Path $manifestPath -ModuleVersion $version; } # Update pre-release version if (![String]::IsNullOrEmpty($versionSuffix)) { Write-Verbose -Message "[VersionModule] -- Updating module manifest Prerelease"; Update-ModuleManifest -Path $manifestPath -Prerelease $versionSuffix; } } $manifest = Test-ModuleManifest -Path $manifestPath; $requiredModules = $manifest.RequiredModules | ForEach-Object -Process { if ($_.Name -eq 'PSDocs' -and $Configuration -eq 'Release') { @{ ModuleName = 'PSDocs'; ModuleVersion = '0.9.0' } } else { @{ ModuleName = $_.Name; ModuleVersion = $_.Version } } }; Update-ModuleManifest -Path $manifestPath -RequiredModules $requiredModules; } # Synopsis: Publish to PowerShell Gallery task ReleaseModule VersionModule, { $modulePath = (Join-Path -Path $ArtifactPath -ChildPath PSDocs.Azure); Write-Verbose -Message "[ReleaseModule] -- Checking module path: $modulePath"; if (!(Test-Path -Path $modulePath)) { Write-Error -Message "[ReleaseModule] -- Module path does not exist"; } elseif (![String]::IsNullOrEmpty($ApiKey)) { Publish-Module -Path $modulePath -NuGetApiKey $ApiKey; } } # Synopsis: Install NuGet provider task NuGet { if ($Null -eq (Get-PackageProvider -Name NuGet -ErrorAction Ignore)) { Install-PackageProvider -Name NuGet -Force -Scope CurrentUser; } } # Synopsis: Install Pester module task Pester NuGet, { if ($Null -eq (Get-InstalledModule -Name Pester -RequiredVersion 4.10.1 -ErrorAction Ignore)) { Install-Module -Name Pester -RequiredVersion 4.10.1 -Scope CurrentUser -Force -SkipPublisherCheck; } Import-Module -Name Pester -RequiredVersion 4.10.1 -Verbose:$False; } # Synopsis: Install PSScriptAnalyzer module task PSScriptAnalyzer NuGet, { if ($Null -eq (Get-InstalledModule -Name PSScriptAnalyzer -MinimumVersion 1.18.3 -ErrorAction Ignore)) { Install-Module -Name PSScriptAnalyzer -MinimumVersion 1.18.3 -Scope CurrentUser -Force; } Import-Module -Name PSScriptAnalyzer -Verbose:$False; } # Synopsis: Install PSRule task PSRule NuGet, { if ($Null -eq (Get-InstalledModule -Name PSRule -MinimumVersion 1.4.0 -ErrorAction Ignore)) { Install-Module -Name PSRule -Repository PSGallery -MinimumVersion 1.4.0 -Scope CurrentUser -Force; } if ($Null -eq (Get-InstalledModule -Name PSRule.Rules.MSFT.OSS -MinimumVersion 0.1.0 -AllowPrerelease -ErrorAction Ignore)) { Install-Module -Name PSRule.Rules.MSFT.OSS -Repository PSGallery -MinimumVersion 0.1.0 -AllowPrerelease -Scope CurrentUser -Force; } Import-Module -Name PSRule.Rules.MSFT.OSS -Verbose:$False; } # Synopsis: Install PSDocs task PSDocs NuGet, { if ($Null -eq (Get-InstalledModule -Name PSDocs -MinimumVersion 0.9.0 -ErrorAction Ignore)) { Install-Module -Name PSDocs -Repository PSGallery -MinimumVersion 0.9.0 -Scope CurrentUser -Force; } Import-Module -Name PSDocs -Verbose:$False; } # Synopsis: Run validation task Rules PSRule, { $assertParams = @{ Path = './.ps-rule/' Style = $AssertStyle OutputFormat = 'NUnit3' ErrorAction = 'Stop' Module = 'PSRule.Rules.MSFT.OSS' } Assert-PSRule @assertParams -As Summary -InputPath $PWD -Format File -OutputPath reports/ps-rule-file.xml; } # Synopsis: Install PlatyPS module task platyPS { if ($Null -eq (Get-InstalledModule -Name PlatyPS -MinimumVersion 0.14.0 -ErrorAction Ignore)) { Install-Module -Name PlatyPS -Scope CurrentUser -MinimumVersion 0.14.0 -Force; } } # Synopsis: Install module dependencies task ModuleDependencies NuGet, PSDocs task CopyModule { CopyModuleFiles -Path src/PSDocs.Azure -DestinationPath out/modules/PSDocs.Azure; # Copy LICENSE Copy-Item -Path LICENSE -Destination out/modules/PSDocs.Azure; # Copy third party notices Copy-Item -Path ThirdPartyNotices.txt -Destination out/modules/PSDocs.Azure; } task BuildDotNet { exec { # Build library # Add build version -p:versionPrefix=$ModuleVersion dotnet publish src/PSDocs.Azure -c $Configuration -f netstandard2.0 -o $(Join-Path -Path $PWD -ChildPath out/modules/PSDocs.Azure) -p:version=$Build } } # Synopsis: Build modules only task BuildModule BuildDotNet, CopyModule # Synopsis: Build help task BuildHelp BuildModule, PlatyPS, { if (!(Test-Path out/modules/PSDocs.Azure/en-US/)) { $Null = New-Item -Path out/modules/PSDocs.Azure/en-US/ -ItemType Directory -Force; } if (!(Test-Path out/modules/PSDocs.Azure/en-AU/)) { $Null = New-Item -Path out/modules/PSDocs.Azure/en-AU/ -ItemType Directory -Force; } if (!(Test-Path out/modules/PSDocs.Azure/en-GB/)) { $Null = New-Item -Path out/modules/PSDocs.Azure/en-GB/ -ItemType Directory -Force; } # Avoid YamlDotNet issue in same app domain exec { $pwshPath = (Get-Process -Id $PID).Path; &$pwshPath -Command { # Generate MAML and about topics Import-Module -Name PlatyPS -Verbose:$False; $Null = New-ExternalHelp -OutputPath 'out/docs/PSDocs.Azure' -Path '.\docs\commands\en-US', '.\docs\concepts\en-US' -Force; } } if (!(Test-Path -Path 'out/docs/PSDocs.Azure/PSDocs.Azure-help.xml')) { throw 'Failed find generated cmdlet help.'; } # Copy generated help into module out path $Null = Copy-Item -Path out/docs/PSDocs.Azure/* -Destination out/modules/PSDocs.Azure/en-US; $Null = Copy-Item -Path out/docs/PSDocs.Azure/* -Destination out/modules/PSDocs.Azure/en-AU; $Null = Copy-Item -Path out/docs/PSDocs.Azure/* -Destination out/modules/PSDocs.Azure/en-GB; } task TestModule ModuleDependencies, Pester, PSScriptAnalyzer, { # Run Pester tests $pesterParams = @{ Path = (Join-Path -Path $PWD -ChildPath tests/PSDocs.Azure.Tests); OutputFile = 'reports/pester-unit.xml'; OutputFormat = 'NUnitXml'; PesterOption = @{ IncludeVSCodeMarker = $True }; PassThru = $True; }; if ($CodeCoverage) { $pesterParams.Add('CodeCoverage', (Join-Path -Path $PWD -ChildPath 'out/modules/**/*.psm1')); $pesterParams.Add('CodeCoverageOutputFile', (Join-Path -Path $PWD -ChildPath 'reports/pester-coverage.xml')); } if (!(Test-Path -Path reports)) { $Null = New-Item -Path reports -ItemType Directory -Force; } $results = Invoke-Pester @pesterParams; # Throw an error if pester tests failed if ($Null -eq $results) { throw 'Failed to get Pester test results.'; } elseif ($results.FailedCount -gt 0) { throw "$($results.FailedCount) tests failed."; } } task UpdateTemplateDocs Build, { Import-Module (Join-Path -Path $PWD -ChildPath 'out/modules/PSDocs.Azure') # Scan for Azure template file recursively in the templates/ directory Get-AzDocTemplateFile -Path templates/ | ForEach-Object { $template = Get-Item -Path $_.TemplateFile; Invoke-PSDocument -Module PSDocs.Azure -OutputPath $template.Directory.FullName -InputPath $template.FullName -Option @{}; } # Scan for Azure template file recursively in the samples/bicep/ directory Get-AzDocTemplateFile -Path examples/bicep/ | ForEach-Object { $template = Get-Item -Path $_.TemplateFile; Invoke-PSDocument -Module PSDocs.Azure -OutputPath $template.Directory.FullName -InputPath $template.FullName -Option @{ 'Configuration.AZURE_BICEP_REGISTRY_MODULES_METADATA_SCHEMA_ENABLED' = $True }; } } # Synopsis: Run script analyzer task Analyze Build, PSScriptAnalyzer, { Invoke-ScriptAnalyzer -Path out/modules/PSDocs.Azure; } # Synopsis: Add shipit build tag task TagBuild { if ($Null -ne $Env:BUILD_DEFINITIONNAME) { Write-Host "`#`#vso[build.addbuildtag]shipit"; } } # Synopsis: Remove temp files. task Clean { Remove-Item -Path out,reports -Recurse -Force -ErrorAction SilentlyContinue; } task Build Clean, BuildModule, VersionModule, BuildHelp task Test Build, Rules, TestModule task Release ReleaseModule, TagBuild # Synopsis: Build and test. Entry point for CI Build stage task . Build, Rules