2019-05-16 13:56:28 +03:00
|
|
|
|
|
|
|
[CmdletBinding()]
|
|
|
|
param (
|
|
|
|
[Parameter(Mandatory = $False)]
|
2019-06-10 16:19:29 +03:00
|
|
|
[String]$Build = '0.0.1',
|
2019-05-16 13:56:28 +03:00
|
|
|
|
|
|
|
[Parameter(Mandatory = $False)]
|
|
|
|
[String]$Configuration = 'Debug',
|
|
|
|
|
|
|
|
[Parameter(Mandatory = $False)]
|
2019-06-10 16:19:29 +03:00
|
|
|
[String]$ApiKey,
|
2019-05-16 13:56:28 +03:00
|
|
|
|
|
|
|
[Parameter(Mandatory = $False)]
|
|
|
|
[Switch]$CodeCoverage = $False,
|
|
|
|
|
|
|
|
[Parameter(Mandatory = $False)]
|
|
|
|
[String]$ArtifactPath = (Join-Path -Path $PWD -ChildPath out/modules)
|
|
|
|
)
|
|
|
|
|
2019-06-10 16:19:29 +03:00
|
|
|
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;
|
|
|
|
|
2019-05-16 13:56:28 +03:00
|
|
|
if ($Env:SYSTEM_DEBUG -eq 'true') {
|
|
|
|
$VerbosePreference = 'Continue';
|
|
|
|
}
|
|
|
|
|
2019-06-10 16:19:29 +03:00
|
|
|
if ($Env:BUILD_SOURCEBRANCH -like '*/tags/*' -and $Env:BUILD_SOURCEBRANCHNAME -like 'v0.*') {
|
|
|
|
$Build = $Env:BUILD_SOURCEBRANCHNAME.Substring(1);
|
2019-05-16 13:56:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-10 16:19:29 +03:00
|
|
|
$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];
|
|
|
|
}
|
2019-05-24 02:57:41 +03:00
|
|
|
}
|
|
|
|
|
2019-06-10 16:19:29 +03:00
|
|
|
Write-Host -Object "[Pipeline] -- Using version: $version" -ForegroundColor Green;
|
|
|
|
Write-Host -Object "[Pipeline] -- Using versionSuffix: $versionSuffix" -ForegroundColor Green;
|
2019-05-17 14:25:30 +03:00
|
|
|
|
2019-07-28 17:17:15 +03:00
|
|
|
if ($Env:coverage -eq 'true') {
|
|
|
|
$CodeCoverage = $True;
|
|
|
|
}
|
|
|
|
|
2019-05-16 13:56:28 +03:00
|
|
|
# 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;
|
|
|
|
|
2019-09-25 01:59:25 +03:00
|
|
|
Get-ChildItem -Path $sourcePath -Recurse -File -Include *.ps1,*.psm1,*.psd1,*.ps1xml,*.yaml | Where-Object -FilterScript {
|
2019-05-16 13:56:28 +03:00
|
|
|
($_.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;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-19 10:57:11 +03:00
|
|
|
task VersionModule ModuleDependencies, {
|
2019-05-17 14:25:30 +03:00
|
|
|
$modulePath = Join-Path -Path $ArtifactPath -ChildPath PSRule.Rules.Azure;
|
|
|
|
$manifestPath = Join-Path -Path $modulePath -ChildPath PSRule.Rules.Azure.psd1;
|
|
|
|
Write-Verbose -Message "[VersionModule] -- Checking module path: $modulePath";
|
|
|
|
|
2019-06-10 16:19:29 +03:00
|
|
|
if (![String]::IsNullOrEmpty($Build)) {
|
2019-05-16 13:56:28 +03:00
|
|
|
# Update module version
|
|
|
|
if (![String]::IsNullOrEmpty($version)) {
|
|
|
|
Write-Verbose -Message "[VersionModule] -- Updating module manifest ModuleVersion";
|
2019-05-17 14:25:30 +03:00
|
|
|
Update-ModuleManifest -Path $manifestPath -ModuleVersion $version;
|
2019-05-16 13:56:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# Update pre-release version
|
2019-06-10 16:19:29 +03:00
|
|
|
if (![String]::IsNullOrEmpty($versionSuffix)) {
|
2019-05-16 13:56:28 +03:00
|
|
|
Write-Verbose -Message "[VersionModule] -- Updating module manifest Prerelease";
|
2019-06-10 16:19:29 +03:00
|
|
|
Update-ModuleManifest -Path $manifestPath -Prerelease $versionSuffix;
|
2019-05-16 13:56:28 +03:00
|
|
|
}
|
|
|
|
}
|
2019-06-10 16:19:29 +03:00
|
|
|
|
|
|
|
$manifest = Test-ModuleManifest -Path $manifestPath;
|
|
|
|
$requiredModules = $manifest.RequiredModules | ForEach-Object -Process {
|
|
|
|
if ($_.Name -eq 'PSRule' -and $Configuration -eq 'Release') {
|
2019-08-12 14:19:18 +03:00
|
|
|
@{ ModuleName = 'PSRule'; ModuleVersion = '0.8.0' }
|
2019-06-10 16:19:29 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
@{ ModuleName = $_.Name; ModuleVersion = $_.Version }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Update-ModuleManifest -Path $manifestPath -RequiredModules $requiredModules;
|
2019-09-25 01:59:25 +03:00
|
|
|
$manifestContent = Get-Content -Path $manifestPath -Raw;
|
|
|
|
$manifestContent = $manifestContent -replace 'PSRule = ''System.Collections.Hashtable''', 'PSRule = @{ Baseline = ''Azure.SubscriptionDefault'' }';
|
|
|
|
$manifestContent | Set-Content -Path $manifestPath;
|
2019-05-16 13:56:28 +03:00
|
|
|
}
|
|
|
|
|
2019-05-19 10:57:11 +03:00
|
|
|
# Synopsis: Publish to PowerShell Gallery
|
2019-05-16 13:56:28 +03:00
|
|
|
task ReleaseModule VersionModule, {
|
2019-05-17 14:25:30 +03:00
|
|
|
$modulePath = (Join-Path -Path $ArtifactPath -ChildPath PSRule.Rules.Azure);
|
|
|
|
Write-Verbose -Message "[ReleaseModule] -- Checking module path: $modulePath";
|
|
|
|
|
|
|
|
if (!(Test-Path -Path $modulePath)) {
|
|
|
|
Write-Error -Message "[ReleaseModule] -- Module path does not exist";
|
|
|
|
}
|
2019-06-10 16:53:22 +03:00
|
|
|
elseif (![String]::IsNullOrEmpty($ApiKey)) {
|
|
|
|
Publish-Module -Path $modulePath -NuGetApiKey $ApiKey;
|
2019-05-16 13:56:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# 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 -MinimumVersion 4.0.0 -ErrorAction Ignore)) {
|
|
|
|
Install-Module -Name Pester -MinimumVersion 4.0.0 -Scope CurrentUser -Force -SkipPublisherCheck;
|
|
|
|
}
|
|
|
|
Import-Module -Name Pester -Verbose:$False;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Synopsis: Install PSScriptAnalyzer module
|
|
|
|
task PSScriptAnalyzer NuGet, {
|
|
|
|
if ($Null -eq (Get-InstalledModule -Name PSScriptAnalyzer -MinimumVersion 1.17.0 -ErrorAction Ignore)) {
|
|
|
|
Install-Module -Name PSScriptAnalyzer -MinimumVersion 1.17.0 -Scope CurrentUser -Force;
|
|
|
|
}
|
|
|
|
Import-Module -Name PSScriptAnalyzer -Verbose:$False;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Synopsis: Install PSRule
|
|
|
|
task PSRule NuGet, {
|
2019-08-12 14:19:18 +03:00
|
|
|
if ($Null -eq (Get-InstalledModule -Name PSRule -MinimumVersion 0.8.0 -ErrorAction Ignore)) {
|
|
|
|
Install-Module -Name PSRule -MinimumVersion 0.8.0 -Scope CurrentUser -Force;
|
2019-05-16 13:56:28 +03:00
|
|
|
}
|
|
|
|
Import-Module -Name PSRule -Verbose:$False;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Synopsis: Install PSDocs
|
|
|
|
task PSDocs NuGet, {
|
|
|
|
if ($Null -eq (Get-InstalledModule -Name PSDocs -MinimumVersion 0.6.1 -ErrorAction Ignore)) {
|
|
|
|
Install-Module -Name PSDocs -MinimumVersion 0.6.1 -AllowPrerelease -Scope CurrentUser -Force;
|
|
|
|
}
|
|
|
|
Import-Module -Name PSDocs -Verbose:$False;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Synopsis: Install PlatyPS module
|
|
|
|
task platyPS {
|
2019-06-10 16:19:29 +03:00
|
|
|
if ($Null -eq (Get-InstalledModule -Name PlatyPS -MinimumVersion 0.14.0 -ErrorAction Ignore)) {
|
|
|
|
Install-Module -Name PlatyPS -Scope CurrentUser -MinimumVersion 0.14.0 -Force;
|
2019-05-16 13:56:28 +03:00
|
|
|
}
|
|
|
|
Import-Module -Name PlatyPS -Verbose:$False;
|
|
|
|
}
|
|
|
|
|
2019-05-19 10:57:11 +03:00
|
|
|
# Synopsis: Install module dependencies
|
|
|
|
task ModuleDependencies NuGet, PSRule, {
|
2019-06-12 04:55:39 +03:00
|
|
|
if ($Null -eq (Get-InstalledModule -Name Az.Accounts -MinimumVersion 1.5.2 -ErrorAction Ignore)) {
|
|
|
|
Install-Module -Name Az.Accounts -Scope CurrentUser -MinimumVersion 1.5.2 -Force;
|
2019-05-19 10:57:11 +03:00
|
|
|
}
|
2019-06-12 04:55:39 +03:00
|
|
|
if ($Null -eq (Get-InstalledModule -Name Az.Resources -MinimumVersion 1.4.0 -ErrorAction Ignore)) {
|
|
|
|
Install-Module -Name Az.Resources -Scope CurrentUser -MinimumVersion 1.4.0 -Force;
|
2019-05-19 10:57:11 +03:00
|
|
|
}
|
2019-06-12 04:55:39 +03:00
|
|
|
if ($Null -eq (Get-InstalledModule -Name Az.Security -MinimumVersion 0.7.4 -ErrorAction Ignore)) {
|
|
|
|
Install-Module -Name Az.Security -Scope CurrentUser -MinimumVersion 0.7.4 -Force;
|
2019-05-19 10:57:11 +03:00
|
|
|
}
|
2019-06-12 04:55:39 +03:00
|
|
|
if ($Null -eq (Get-InstalledModule -Name Az.Storage -MinimumVersion 1.3.0 -ErrorAction Ignore)) {
|
|
|
|
Install-Module -Name Az.Storage -Scope CurrentUser -MinimumVersion 1.3.0 -Force -AllowClobber;
|
2019-05-19 10:57:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-16 13:56:28 +03:00
|
|
|
task CopyModule {
|
|
|
|
CopyModuleFiles -Path src/PSRule.Rules.Azure -DestinationPath out/modules/PSRule.Rules.Azure;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Synopsis: Build modules only
|
|
|
|
task BuildModule CopyModule
|
|
|
|
|
|
|
|
task TestRules PSRule, Pester, PSScriptAnalyzer, {
|
|
|
|
# Run Pester tests
|
|
|
|
$pesterParams = @{ Path = $PWD; OutputFile = 'reports/pester-unit.xml'; OutputFormat = 'NUnitXml'; PesterOption = @{ IncludeVSCodeMarker = $True }; PassThru = $True; };
|
|
|
|
|
2019-05-19 10:57:11 +03:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2019-05-16 13:56:28 +03:00
|
|
|
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.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Synopsis: Run script analyzer
|
|
|
|
task Analyze Build, PSScriptAnalyzer, {
|
|
|
|
Invoke-ScriptAnalyzer -Path out/modules/PSRule.Rules.Azure;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Synopsis: Build table of content for rules
|
2019-06-12 06:48:32 +03:00
|
|
|
task BuildRuleDocs Build, PSRule, PSDocs, {
|
|
|
|
Import-Module (Join-Path -Path $PWD -ChildPath out/modules/PSRule.Rules.Azure) -Force;
|
|
|
|
$Null = Invoke-PSDocument -Name Azure -OutputPath .\docs\rules\en-US\ -Path .\RuleToc.Doc.ps1;
|
|
|
|
$rules = Get-PSRule -Module 'PSRule.Rules.Azure';
|
2019-06-10 16:19:29 +03:00
|
|
|
$rules | ForEach-Object -Process {
|
|
|
|
Invoke-PSDocument -Path .\RuleHelp.Doc.ps1 -OutputPath .\docs\rules\en-US\ -InstanceName $_.Info.Name -inputObject $_;
|
|
|
|
}
|
2019-05-16 13:56:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# Synopsis: Build help
|
2019-06-10 16:19:29 +03:00
|
|
|
task BuildHelp BuildModule, PlatyPS, {
|
2019-05-16 13:56:28 +03:00
|
|
|
# Generate MAML and about topics
|
|
|
|
$Null = New-ExternalHelp -OutputPath out/docs/PSRule.Rules.Azure -Path '.\docs\commands\PSRule.Rules.Azure\en-US' -Force;
|
|
|
|
|
|
|
|
# Copy generated help into module out path
|
|
|
|
$Null = Copy-Item -Path out/docs/PSRule.Rules.Azure/ -Destination out/modules/PSRule.Rules.Azure/en-US/ -Recurse;
|
|
|
|
$Null = Copy-Item -Path out/docs/PSRule.Rules.Azure/ -Destination out/modules/PSRule.Rules.Azure/en-AU/ -Recurse;
|
2019-06-12 04:55:39 +03:00
|
|
|
$Null = Copy-Item -Path out/docs/PSRule.Rules.Azure/ -Destination out/modules/PSRule.Rules.Azure/en-GB/ -Recurse;
|
2019-06-10 16:19:29 +03:00
|
|
|
$Null = Copy-Item -Path docs/rules/en-US/*.md -Destination out/modules/PSRule.Rules.Azure/en-US/;
|
|
|
|
$Null = Copy-Item -Path docs/rules/en-US/*.md -Destination out/modules/PSRule.Rules.Azure/en-AU/;
|
2019-06-12 04:55:39 +03:00
|
|
|
$Null = Copy-Item -Path docs/rules/en-US/*.md -Destination out/modules/PSRule.Rules.Azure/en-GB/;
|
2019-05-16 13:56:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-10 16:19:29 +03:00
|
|
|
task ScaffoldHelp Build, BuildRuleDocs, {
|
2019-05-20 00:32:09 +03:00
|
|
|
Import-Module (Join-Path -Path $PWD -ChildPath out/modules/PSRule.Rules.Azure) -Force;
|
|
|
|
Update-MarkdownHelp -Path '.\docs\commands\PSRule.Rules.Azure\en-US';
|
|
|
|
}
|
|
|
|
|
2019-06-11 06:01:07 +03:00
|
|
|
# Synopsis: Add shipit build tag
|
|
|
|
task TagBuild {
|
|
|
|
if ($Null -ne $Env:BUILD_DEFINITIONNAME) {
|
|
|
|
Write-Host "`#`#vso[build.addbuildtag]shipit";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-16 13:56:28 +03:00
|
|
|
# Synopsis: Remove temp files.
|
|
|
|
task Clean {
|
|
|
|
Remove-Item -Path out,reports -Recurse -Force -ErrorAction SilentlyContinue;
|
|
|
|
}
|
|
|
|
|
|
|
|
task Build Clean, BuildModule, VersionModule, BuildHelp
|
|
|
|
|
|
|
|
task Test Build, TestRules
|
|
|
|
|
2019-06-11 06:01:07 +03:00
|
|
|
task Release ReleaseModule, TagBuild
|
2019-05-16 13:56:28 +03:00
|
|
|
|
|
|
|
task . Build, Test
|