2020-01-07 17:05:08 +03:00
|
|
|
# Copyright (c) Microsoft Corporation.
|
|
|
|
# Licensed under the MIT License.
|
2019-05-08 01:46:11 +03:00
|
|
|
|
|
|
|
[CmdletBinding()]
|
|
|
|
param (
|
2019-05-08 16:03:34 +03:00
|
|
|
[Parameter(Mandatory = $False)]
|
2019-08-12 14:54:18 +03:00
|
|
|
[String]$Build = '0.0.1',
|
2019-05-08 16:03:34 +03:00
|
|
|
|
|
|
|
[Parameter(Mandatory = $False)]
|
|
|
|
[String]$Configuration = 'Debug',
|
|
|
|
|
|
|
|
[Parameter(Mandatory = $False)]
|
2019-08-12 14:54:18 +03:00
|
|
|
[String]$ApiKey,
|
2019-05-08 16:03:34 +03:00
|
|
|
|
|
|
|
[Parameter(Mandatory = $False)]
|
|
|
|
[Switch]$CodeCoverage = $False,
|
|
|
|
|
|
|
|
[Parameter(Mandatory = $False)]
|
2020-01-05 11:12:30 +03:00
|
|
|
[String]$ArtifactPath = (Join-Path -Path $PWD -ChildPath out/modules),
|
|
|
|
|
|
|
|
[Parameter(Mandatory = $False)]
|
|
|
|
[String]$AssertStyle = 'AzurePipelines'
|
2019-05-08 01:46:11 +03:00
|
|
|
)
|
|
|
|
|
2020-02-10 16:40:57 +03:00
|
|
|
Write-Host -Object "[Pipeline] -- PowerShell: v$($PSVersionTable.PSVersion.ToString())" -ForegroundColor Green;
|
2019-08-12 14:54:18 +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;
|
2020-02-10 16:40:57 +03:00
|
|
|
Write-Host -Object "[Pipeline] -- Culture: $((Get-Culture).Name), $((Get-Culture).Parent)" -ForegroundColor Green;
|
2019-08-12 14:54:18 +03:00
|
|
|
|
2019-05-11 12:25:19 +03:00
|
|
|
if ($Env:SYSTEM_DEBUG -eq 'true') {
|
|
|
|
$VerbosePreference = 'Continue';
|
|
|
|
}
|
|
|
|
|
2019-08-12 14:54:18 +03:00
|
|
|
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;
|
|
|
|
|
2020-03-23 08:07:52 +03:00
|
|
|
if ($Env:COVERAGE -eq 'true') {
|
2019-05-08 16:03:34 +03:00
|
|
|
$CodeCoverage = $True;
|
|
|
|
}
|
|
|
|
|
2019-05-08 01:46:11 +03:00
|
|
|
function CopyModuleFiles {
|
|
|
|
param (
|
|
|
|
[Parameter(Mandatory = $True)]
|
|
|
|
[String]$Path,
|
|
|
|
|
|
|
|
[Parameter(Mandatory = $True)]
|
|
|
|
[String]$DestinationPath
|
|
|
|
)
|
|
|
|
|
|
|
|
process {
|
|
|
|
$sourcePath = Resolve-Path -Path $Path;
|
|
|
|
|
2019-12-23 09:08:06 +03:00
|
|
|
Get-ChildItem -Path $sourcePath -Recurse -File -Include *.ps1,*.yaml,*.psm1,*.psd1,*.ps1xml | Where-Object -FilterScript {
|
2019-05-08 01:46:11 +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-08-12 14:54:18 +03:00
|
|
|
task VersionModule ModuleDependencies, {
|
|
|
|
$modulePath = Join-Path -Path $ArtifactPath -ChildPath PSRule.Rules.Kubernetes;
|
|
|
|
$manifestPath = Join-Path -Path $modulePath -ChildPath PSRule.Rules.Kubernetes.psd1;
|
|
|
|
Write-Verbose -Message "[VersionModule] -- Checking module path: $modulePath";
|
2019-05-08 16:03:34 +03:00
|
|
|
|
2019-08-12 14:54:18 +03:00
|
|
|
if (![String]::IsNullOrEmpty($Build)) {
|
2019-05-08 16:03:34 +03:00
|
|
|
# Update module version
|
|
|
|
if (![String]::IsNullOrEmpty($version)) {
|
|
|
|
Write-Verbose -Message "[VersionModule] -- Updating module manifest ModuleVersion";
|
2019-08-12 14:54:18 +03:00
|
|
|
Update-ModuleManifest -Path $manifestPath -ModuleVersion $version;
|
2019-05-08 16:03:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# Update pre-release version
|
2019-08-12 14:54:18 +03:00
|
|
|
if (![String]::IsNullOrEmpty($versionSuffix)) {
|
2019-05-08 16:03:34 +03:00
|
|
|
Write-Verbose -Message "[VersionModule] -- Updating module manifest Prerelease";
|
2019-08-12 14:54:18 +03:00
|
|
|
Update-ModuleManifest -Path $manifestPath -Prerelease $versionSuffix;
|
2019-05-08 16:03:34 +03:00
|
|
|
}
|
|
|
|
}
|
2019-08-12 14:54:18 +03:00
|
|
|
|
2022-12-11 11:03:06 +03:00
|
|
|
$dependencies = Get-Content -Path $PWD/modules.json -Raw | ConvertFrom-Json;
|
2019-08-12 14:54:18 +03:00
|
|
|
$manifest = Test-ModuleManifest -Path $manifestPath;
|
|
|
|
$requiredModules = $manifest.RequiredModules | ForEach-Object -Process {
|
|
|
|
if ($_.Name -eq 'PSRule' -and $Configuration -eq 'Release') {
|
2022-12-11 11:03:06 +03:00
|
|
|
@{ ModuleName = 'PSRule'; ModuleVersion = $dependencies.dependencies.PSRule.version }
|
2019-08-12 14:54:18 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
@{ ModuleName = $_.Name; ModuleVersion = $_.Version }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Update-ModuleManifest -Path $manifestPath -RequiredModules $requiredModules;
|
2019-05-08 16:03:34 +03:00
|
|
|
}
|
|
|
|
|
2019-08-12 14:54:18 +03:00
|
|
|
# Synopsis: Publish to PowerShell Gallery
|
2019-05-08 16:03:34 +03:00
|
|
|
task ReleaseModule VersionModule, {
|
2019-12-23 09:33:17 +03:00
|
|
|
$modulePath = (Join-Path -Path $ArtifactPath -ChildPath PSRule.Rules.Kubernetes);
|
2019-08-12 14:54:18 +03:00
|
|
|
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;
|
2019-05-08 16:03:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-08 01:46:11 +03:00
|
|
|
# Synopsis: Install NuGet provider
|
|
|
|
task NuGet {
|
|
|
|
if ($Null -eq (Get-PackageProvider -Name NuGet -ErrorAction Ignore)) {
|
|
|
|
Install-PackageProvider -Name NuGet -Force -Scope CurrentUser;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-11 11:03:06 +03:00
|
|
|
# Synopsis: Install module dependencies
|
|
|
|
task ModuleDependencies Dependencies, {
|
2019-08-12 14:54:18 +03:00
|
|
|
}
|
|
|
|
|
2022-12-11 11:03:06 +03:00
|
|
|
task Dependencies NuGet, {
|
|
|
|
Import-Module $PWD/scripts/dependencies.psm1;
|
|
|
|
Install-Dependencies -Path $PWD/modules.json -Dev;
|
2019-08-12 14:54:18 +03:00
|
|
|
}
|
|
|
|
|
2019-05-08 01:46:11 +03:00
|
|
|
task CopyModule {
|
|
|
|
CopyModuleFiles -Path src/PSRule.Rules.Kubernetes -DestinationPath out/modules/PSRule.Rules.Kubernetes;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Synopsis: Build modules only
|
|
|
|
task BuildModule CopyModule
|
|
|
|
|
2022-12-11 11:03:06 +03:00
|
|
|
task TestModule ModuleDependencies, {
|
2019-05-08 01:46:11 +03:00
|
|
|
# Run Pester tests
|
2022-12-11 11:03:06 +03:00
|
|
|
$pesterOptions = @{
|
|
|
|
Run = @{
|
|
|
|
Path = (Join-Path -Path $PWD -ChildPath tests/PSRule.Rules.Kubernetes.Tests);
|
|
|
|
PassThru = $True;
|
|
|
|
};
|
|
|
|
TestResult = @{
|
|
|
|
Enabled = $True;
|
|
|
|
OutputFormat = 'NUnitXml';
|
|
|
|
OutputPath = 'reports/pester-unit.xml';
|
|
|
|
};
|
|
|
|
};
|
2019-05-08 01:46:11 +03:00
|
|
|
|
2019-08-12 14:54:18 +03:00
|
|
|
if ($CodeCoverage) {
|
2022-12-11 11:03:06 +03:00
|
|
|
$codeCoverageOptions = @{
|
|
|
|
Enabled = $True;
|
|
|
|
OutputPath = (Join-Path -Path $PWD -ChildPath 'reports/pester-coverage.xml');
|
|
|
|
Path = (Join-Path -Path $PWD -ChildPath 'out/modules/**/*.psm1');
|
|
|
|
};
|
|
|
|
|
|
|
|
$pesterOptions.Add('CodeCoverage', $codeCoverageOptions);
|
2019-08-12 14:54:18 +03:00
|
|
|
}
|
|
|
|
|
2019-05-08 01:46:11 +03:00
|
|
|
if (!(Test-Path -Path reports)) {
|
|
|
|
$Null = New-Item -Path reports -ItemType Directory -Force;
|
|
|
|
}
|
|
|
|
|
2022-12-11 11:03:06 +03:00
|
|
|
if ($Null -ne $TestGroup) {
|
|
|
|
$pesterOptions.Add('Filter', @{ Tag = $TestGroup });
|
|
|
|
}
|
|
|
|
|
|
|
|
# https://pester.dev/docs/commands/New-PesterConfiguration
|
|
|
|
$pesterConfiguration = New-PesterConfiguration -Hashtable $pesterOptions;
|
|
|
|
|
|
|
|
$results = Invoke-Pester -Configuration $pesterConfiguration;
|
2019-05-08 01:46:11 +03:00
|
|
|
|
|
|
|
# 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.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-05 11:12:30 +03:00
|
|
|
# Synopsis: Run validation
|
2022-12-11 11:03:06 +03:00
|
|
|
task Rules Dependencies, {
|
2020-01-05 11:12:30 +03:00
|
|
|
$assertParams = @{
|
2022-12-11 11:03:06 +03:00
|
|
|
Path = './.ps-rule/'
|
|
|
|
Style = $AssertStyle
|
2022-01-06 02:49:36 +03:00
|
|
|
OutputFormat = 'NUnit3'
|
2022-12-11 11:03:06 +03:00
|
|
|
ErrorAction = 'Stop'
|
|
|
|
As = 'Summary'
|
2020-01-05 11:12:30 +03:00
|
|
|
}
|
2022-12-11 11:03:06 +03:00
|
|
|
Import-Module (Join-Path -Path $PWD -ChildPath out/modules/PSRule.Rules.Kubernetes) -Force;
|
2022-01-06 02:49:36 +03:00
|
|
|
Assert-PSRule @assertParams -InputPath $PWD -Module PSRule.Rules.MSFT.OSS -Format File -OutputPath reports/ps-rule-file.xml;
|
2020-01-05 11:12:30 +03:00
|
|
|
|
|
|
|
$rules = Get-PSRule -Module PSRule.Rules.Kubernetes;
|
|
|
|
$rules | Assert-PSRule @assertParams -OutputPath reports/ps-rule-file2.xml;
|
|
|
|
}
|
|
|
|
|
2019-08-12 14:54:18 +03:00
|
|
|
# Synopsis: Run script analyzer
|
2022-12-11 11:03:06 +03:00
|
|
|
task Analyze Build, ModuleDependencies, {
|
2019-08-12 14:54:18 +03:00
|
|
|
Invoke-ScriptAnalyzer -Path out/modules/PSRule.Rules.Kubernetes;
|
|
|
|
}
|
|
|
|
|
2019-05-08 16:03:34 +03:00
|
|
|
# Synopsis: Build table of content for rules
|
2022-12-11 11:03:06 +03:00
|
|
|
task BuildRuleDocs Build, ModuleDependencies, {
|
2019-08-12 14:54:18 +03:00
|
|
|
Import-Module (Join-Path -Path $PWD -ChildPath out/modules/PSRule.Rules.Kubernetes) -Force;
|
2020-02-18 06:22:22 +03:00
|
|
|
$Null = Invoke-PSDocument -Name module -OutputPath .\docs\rules\en\ -Path .\RuleToc.Doc.ps1;
|
2019-08-12 14:54:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# Synopsis: Build help
|
2022-12-11 11:03:06 +03:00
|
|
|
task BuildHelp BuildModule, ModuleDependencies, {
|
2020-01-05 11:12:30 +03:00
|
|
|
if (!(Test-Path out/modules/PSRule.Rules.Kubernetes/en/)) {
|
|
|
|
$Null = New-Item -Path out/modules/PSRule.Rules.Kubernetes/en/ -ItemType Directory -Force;
|
2019-12-23 09:08:06 +03:00
|
|
|
}
|
|
|
|
|
2019-08-12 14:54:18 +03:00
|
|
|
# Copy generated help into module out path
|
2020-01-05 11:12:30 +03:00
|
|
|
$Null = Copy-Item -Path docs/rules/en/*.md -Destination out/modules/PSRule.Rules.Kubernetes/en/;
|
2019-08-12 14:54:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
task ScaffoldHelp Build, BuildRuleDocs, {
|
2020-01-05 11:12:30 +03:00
|
|
|
# Import-Module (Join-Path -Path $PWD -ChildPath out/modules/PSRule.Rules.Kubernetes) -Force;
|
2020-02-18 06:22:22 +03:00
|
|
|
# Update-MarkdownHelp -Path '.\docs\commands\PSRule.Rules.Kubernetes\en';
|
2019-08-12 14:54:18 +03:00
|
|
|
}
|
|
|
|
|
2019-05-08 01:46:11 +03:00
|
|
|
# Synopsis: Remove temp files.
|
|
|
|
task Clean {
|
|
|
|
Remove-Item -Path out,reports -Recurse -Force -ErrorAction SilentlyContinue;
|
|
|
|
}
|
|
|
|
|
2019-08-12 14:54:18 +03:00
|
|
|
task Build Clean, BuildModule, VersionModule, BuildHelp
|
2019-05-08 01:46:11 +03:00
|
|
|
|
2020-01-05 11:12:30 +03:00
|
|
|
task Test Build, Rules, TestModule
|
2019-05-08 01:46:11 +03:00
|
|
|
|
2022-12-11 11:03:06 +03:00
|
|
|
task Release ReleaseModule
|
2019-05-08 16:03:34 +03:00
|
|
|
|
2022-01-06 02:49:36 +03:00
|
|
|
task . Build, Rules
|