Automatically update dependencies #84 (#85)

This commit is contained in:
Bernie White 2022-08-13 19:13:38 +10:00 коммит произвёл GitHub
Родитель cd86344c39
Коммит 01317c6fe2
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 299 добавлений и 80 удалений

47
.github/workflows/dependencies.yaml поставляемый Normal file
Просмотреть файл

@ -0,0 +1,47 @@
#
# Automated dependency updates
#
# NOTES:
# This automatically bumps PowerShell dependency versions.
name: Dependencies
on:
schedule:
- cron: '45 1 * * 1' # At 01:45 AM, on Monday each week
workflow_dispatch:
env:
WORKING_BRANCH: dependencies/powershell-bump
jobs:
dependencies:
name: Bump dependencies
runs-on: ubuntu-latest
if: github.repository == 'microsoft/PSRule.Monitor'
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Configure
run: |
git config user.name github-actions
git config user.email '41898282+github-actions[bot]@users.noreply.github.com'
- name: Get working branch
run: |
git checkout -B ${{ env.WORKING_BRANCH }} --force
- name: Check dependencies
run: |
Import-Module ./scripts/dependencies.psm1;
Update-Dependencies -Path ./modules.json;
shell: pwsh
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Просмотреть файл

@ -11,6 +11,10 @@ What's changed since v0.4.0:
[#79](https://github.com/microsoft/PSRule.Monitor/pull/79)
- Upgrade support projects to .NET 6 by @BernieWhite.
[#83](https://github.com/microsoft/PSRule.Monitor/issues/83)
- Automatically update PowerShell dependencies by @BernieWhite.
[#84](https://github.com/microsoft/PSRule.Monitor/issues/84)
- Upate Pester tests to v5 by @BernieWhite.
[#15](https://github.com/microsoft/PSRule.Monitor/issues/15)
## v0.4.0

21
modules.json Normal file
Просмотреть файл

@ -0,0 +1,21 @@
{
"dependencies": {
"PSRule": {
"version": "1.6.0"
}
},
"devDependencies": {
"Pester": {
"version": "5.3.3"
},
"platyPS": {
"version": "0.14.2"
},
"PSDocs": {
"version": "0.9.0"
},
"PSScriptAnalyzer": {
"version": "1.20.0"
}
}
}

Просмотреть файл

@ -19,19 +19,29 @@ param (
[String]$ArtifactPath = (Join-Path -Path $PWD -ChildPath out/modules),
[Parameter(Mandatory = $False)]
[String]$AssertStyle = 'AzurePipelines'
[String]$AssertStyle = 'AzurePipelines',
[Parameter(Mandatory = $False)]
[String]$TestGroup = $Null
)
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';
}
$forcePublish = $False;
if ($Env:FORCE_PUBLISH -eq 'true') {
$forcePublish = $True;
}
if ($Env:BUILD_SOURCEBRANCH -like '*/tags/*' -and $Env:BUILD_SOURCEBRANCHNAME -like 'v0.*') {
$Build = $Env:BUILD_SOURCEBRANCHNAME.Substring(1);
}
@ -51,7 +61,7 @@ if ($version -like '*-*') {
Write-Host -Object "[Pipeline] -- Using version: $version" -ForegroundColor Green;
Write-Host -Object "[Pipeline] -- Using versionSuffix: $versionSuffix" -ForegroundColor Green;
if ($Env:coverage -eq 'true') {
if ($Env:COVERAGE -eq 'true') {
$CodeCoverage = $True;
}
@ -103,10 +113,11 @@ task VersionModule ModuleDependencies, {
}
}
$dependencies = Get-Content -Path $PWD/modules.json -Raw | ConvertFrom-Json;
$manifest = Test-ModuleManifest -Path $manifestPath;
$requiredModules = $manifest.RequiredModules | ForEach-Object -Process {
if ($_.Name -eq 'PSRule' -and $Configuration -eq 'Release') {
@{ ModuleName = 'PSRule'; ModuleVersion = '1.6.0' }
@{ ModuleName = 'PSRule'; ModuleVersion = $dependencies.dependencies.PSRule.version }
}
else {
@{ ModuleName = $_.Name; ModuleVersion = $_.Version }
@ -124,7 +135,7 @@ task ReleaseModule VersionModule, {
Write-Error -Message "[ReleaseModule] -- Module path does not exist";
}
elseif (![String]::IsNullOrEmpty($ApiKey)) {
Publish-Module -Path $modulePath -NuGetApiKey $ApiKey;
Publish-Module -Path $modulePath -NuGetApiKey $ApiKey -Force:$forcePublish;
}
}
@ -135,54 +146,14 @@ task NuGet {
}
}
# 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.6.0 -ErrorAction Ignore)) {
Install-Module -Name PSRule -Repository PSGallery -MinimumVersion 1.6.0 -Scope CurrentUser -Force;
}
if ($Null -eq (Get-InstalledModule -Name PSRule.Rules.MSFT.OSS -MinimumVersion 0.1.0 -ErrorAction Ignore)) {
Install-Module -Name PSRule.Rules.MSFT.OSS -Repository PSGallery -MinimumVersion 0.1.0 -Scope CurrentUser -Force;
}
}
# Synopsis: Install PSDocs
task PSDocs NuGet, {
if ($Null -eq (Get-InstalledModule -Name PSDocs -MinimumVersion 0.8.0 -ErrorAction Ignore)) {
Install-Module -Name PSDocs -Repository PSGallery -MinimumVersion 0.8.0 -Scope CurrentUser -Force;
}
Import-Module -Name PSDocs -Verbose:$False;
}
# 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, PSRule, {
task ModuleDependencies Dependencies, {
}
task BuildDotNet {
exec {
# Build library
dotnet build src/SDK -c $Configuration -f netstandard2.0 -p:version=$Build
dotnet publish src/PSRule.Monitor -c $Configuration -f netstandard2.0 -o $(Join-Path -Path $PWD -ChildPath out/modules/PSRule.Monitor) -p:version=$Build
}
}
@ -212,20 +183,42 @@ task CopyModule {
# Synopsis: Build modules only
task BuildModule BuildDotNet, CopyModule
task TestModule ModuleDependencies, Pester, PSScriptAnalyzer, {
task TestModule ModuleDependencies, {
# Run Pester tests
$pesterParams = @{ Path = (Join-Path -Path $PWD -ChildPath tests/PSRule.Monitor.Tests); OutputFile = 'reports/pester-unit.xml'; OutputFormat = 'NUnitXml'; PesterOption = @{ IncludeVSCodeMarker = $True }; PassThru = $True; };
$pesterOptions = @{
Run = @{
Path = (Join-Path -Path $PWD -ChildPath tests/PSRule.Monitor.Tests);
PassThru = $True;
};
TestResult = @{
Enabled = $True;
OutputFormat = 'NUnitXml';
OutputPath = 'reports/pester-unit.xml';
};
};
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'));
$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);
}
if (!(Test-Path -Path reports)) {
$Null = New-Item -Path reports -ItemType Directory -Force;
}
$results = Invoke-Pester @pesterParams;
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;
# Throw an error if pester tests failed
if ($Null -eq $results) {
@ -237,7 +230,7 @@ task TestModule ModuleDependencies, Pester, PSScriptAnalyzer, {
}
# Synopsis: Run validation
task Rules PSRule, {
task Rules Dependencies, {
$assertParams = @{
Path = './.ps-rule/'
Style = $AssertStyle
@ -252,22 +245,23 @@ task Rules PSRule, {
}
# Synopsis: Run script analyzer
task Analyze Build, PSScriptAnalyzer, {
task Analyze Build, Dependencies, {
Invoke-ScriptAnalyzer -Path out/modules/PSRule.Monitor;
}
# Synopsis: Build help
task BuildHelp BuildModule, PlatyPS, {
# Copy generated help into module out path
if (!(Test-Path -Path out/modules/PSRule.Monitor/en-US/)) {
$Null = New-Item -Path out/modules/PSRule.Monitor/en-US -Force -ItemType Directory;
task BuildHelp BuildModule, Dependencies, {
if (!(Test-Path out/modules/PSRule.Monitor/en/)) {
$Null = New-Item -Path out/modules/PSRule.Monitor/en/ -ItemType Directory -Force;
}
if (!(Test-Path -Path out/modules/PSRule.Monitor/en-AU/)) {
$Null = New-Item -Path out/modules/PSRule.Monitor/en-AU -Force -ItemType Directory;
if (!(Test-Path out/modules/PSRule.Monitor/en-US/)) {
$Null = New-Item -Path out/modules/PSRule.Monitor/en-US/ -ItemType Directory -Force;
}
if (!(Test-Path -Path out/modules/PSRule.Monitor/en-GB/)) {
$Null = New-Item -Path out/modules/PSRule.Monitor/en-GB -Force -ItemType Directory;
if (!(Test-Path out/modules/PSRule.Monitor/en-AU/)) {
$Null = New-Item -Path out/modules/PSRule.Monitor/en-AU/ -ItemType Directory -Force;
}
if (!(Test-Path out/modules/PSRule.Monitor/en-GB/)) {
$Null = New-Item -Path out/modules/PSRule.Monitor/en-GB/ -ItemType Directory -Force;
}
# Avoid YamlDotNet issue in same app domain
@ -295,11 +289,9 @@ task ScaffoldHelp Build, {
Update-MarkdownHelp -Path '.\docs\commands\PSRule.Monitor\en-US';
}
# Synopsis: Add shipit build tag
task TagBuild {
if ($Null -ne $Env:BUILD_DEFINITIONNAME) {
Write-Host "`#`#vso[build.addbuildtag]shipit";
}
task Dependencies NuGet, {
Import-Module $PWD/scripts/dependencies.psm1;
Install-Dependencies -Path $PWD/modules.json;
}
# Synopsis: Remove temp files.
@ -311,7 +303,7 @@ task Build Clean, BuildModule, VersionModule, BuildHelp
task Test Build, Rules, TestDotNet, TestModule
task Release ReleaseModule, TagBuild
task Release ReleaseModule
# Synopsis: Build and test. Entry point for CI Build stage
task . Build, Rules, TestDotNet

147
scripts/dependencies.psm1 Normal file
Просмотреть файл

@ -0,0 +1,147 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# Note:
# Handles dependencies updates.
function Update-Dependencies {
[CmdletBinding()]
param (
[Parameter(Mandatory = $True)]
[String]$Path,
[Parameter(Mandatory = $False)]
[String]$Repository = 'PSGallery'
)
process {
$modules = Get-Content -Path $Path -Raw | ConvertFrom-Json -AsHashtable;
$dependencies = CheckVersion $modules.dependencies -Repository $Repository;
$devDependencies = CheckVersion $modules.devDependencies -Repository $Repository -Dev;
$modules = [Ordered]@{
dependencies = $dependencies
devDependencies = $devDependencies
}
$modules | ConvertTo-Json -Depth 10 | Set-Content -Path $Path;
$updates = @(git status --porcelain);
if ($Null -ne $Env:WORKING_BRANCH -and $Null -ne $updates -and $updates.Length -gt 0) {
git add modules.json;
git commit -m "Update $path";
git push --force -u origin $Env:WORKING_BRANCH;
$existingBranch = @(gh pr list --head $Env:WORKING_BRANCH --state open --json number | ConvertFrom-Json);
if ($Null -eq $existingBranch -or $existingBranch.Length -eq 0) {
gh pr create -B 'main' -H $Env:WORKING_BRANCH -l 'dependencies' -t 'Bump PowerShell dependencies' -F 'out/updates.txt';
}
else {
$pr = $existingBranch[0].number
gh pr edit $pr -F 'out/updates.txt';
}
}
}
}
function Install-Dependencies {
[CmdletBinding()]
param (
[Parameter(Mandatory = $True)]
[String]$Path,
[Parameter(Mandatory = $False)]
[String]$Repository = 'PSGallery'
)
process {
$modules = Get-Content -Path $Path -Raw | ConvertFrom-Json;
InstallVersion $modules.dependencies -Repository $Repository;
InstallVersion $modules.devDependencies -Repository $Repository -Dev;
}
}
function CheckVersion {
[CmdletBinding()]
[OutputType([System.Collections.Specialized.OrderedDictionary])]
param (
[Parameter(Mandatory = $True)]
[Hashtable]$InputObject,
[Parameter(Mandatory = $True)]
[String]$Repository,
[Parameter(Mandatory = $False)]
[Switch]$Dev,
[Parameter(Mandatory = $False)]
[String]$OutputPath = 'out/'
)
begin {
$group = 'Dependencies';
if ($Dev) {
$group = 'DevDependencies';
}
if (!(Test-Path -Path $OutputPath)) {
$Null = New-Item -Path $OutputPath -ItemType Directory -Force;
}
$changeNotes = Join-Path -Path $OutputPath -ChildPath 'updates.txt';
}
process {
$dependencies = [Ordered]@{ };
$InputObject.GetEnumerator() | Sort-Object -Property Name | ForEach-Object {
$dependencies[$_.Name] = $_.Value
}
foreach ($module in $dependencies.GetEnumerator()) {
Write-Host -Object "[$group] -- Checking $($module.Name)";
$installParams = @{}
$installParams += $module.Value;
$installParams.MinimumVersion = $installParams.version;
$installParams.Remove('version');
$available = @(Find-Module -Repository $Repository -Name $module.Name @installParams -ErrorAction Ignore);
foreach ($found in $available) {
if (([Version]$found.Version) -gt ([Version]$module.Value.version)) {
Write-Host -Object "[$group] -- Newer version found $($found.Version)";
$dependencies[$module.Name].version = $found.Version;
$Null = Add-Content -Path $changeNotes -Value "Bump $($module.Name) to $($found.Version).";
}
else {
Write-Host -Object "[$group] -- Already up to date.";
}
}
}
return $dependencies;
}
}
function InstallVersion {
[CmdletBinding()]
[OutputType([void])]
param (
[Parameter(Mandatory = $True)]
[PSObject]$InputObject,
[Parameter(Mandatory = $True)]
[String]$Repository,
[Parameter(Mandatory = $False)]
[Switch]$Dev
)
begin {
$group = 'Dependencies';
if ($Dev) {
$group = 'DevDependencies';
}
}
process {
foreach ($module in $InputObject.PSObject.Properties.GetEnumerator()) {
Write-Host -Object "[$group] -- Installing $($module.Name) v$($module.Value.version)";
$installParams = @{ MinimumVersion = $module.Value.version };
if ($Null -eq (Get-InstalledModule -Name $module.Name @installParams -ErrorAction Ignore)) {
Install-Module -Name $module.Name @installParams -Force -Repository $Repository;
}
}
}
}
Export-ModuleMember -Function @(
'Update-Dependencies'
'Install-Dependencies'
)

Просмотреть файл

@ -8,16 +8,22 @@
[CmdletBinding()]
param ()
# Setup error handling
$ErrorActionPreference = 'Stop';
Set-StrictMode -Version latest;
BeforeAll {
# Setup error handling
$ErrorActionPreference = 'Stop';
Set-StrictMode -Version latest;
# Setup tests paths
$rootPath = $PWD;
if ($Env:SYSTEM_DEBUG -eq 'true') {
$VerbosePreference = 'Continue';
}
# Setup tests paths
$rootPath = $PWD;
$modulePath = (Join-Path -Path $rootPath -ChildPath out/modules/PSRule.Monitor);
$here = (Resolve-Path $PSScriptRoot).Path;
}
Describe 'PSRule.Monitor' -Tag 'PowerShellGallery' {
$modulePath = (Join-Path -Path $rootPath -ChildPath out/modules/PSRule.Monitor);
Context 'Module' {
It 'Can be imported' {
Import-Module $modulePath -Force;
@ -25,11 +31,13 @@ Describe 'PSRule.Monitor' -Tag 'PowerShellGallery' {
}
Context 'Manifest' {
$manifestPath = (Join-Path -Path $rootPath -ChildPath out/modules/PSRule.Monitor/PSRule.Monitor.psd1);
$result = Test-ModuleManifest -Path $manifestPath;
$Global:psEditor = $True;
$Null = Import-Module $modulePath -Force;
$commands = Get-Command -Module PSRule.Monitor -All;
BeforeAll {
$manifestPath = (Join-Path -Path $rootPath -ChildPath out/modules/PSRule.Monitor/PSRule.Monitor.psd1);
$result = Test-ModuleManifest -Path $manifestPath;
$Global:psEditor = $True;
$Null = Import-Module $modulePath -Force;
$commands = Get-Command -Module PSRule.Monitor -All;
}
It 'Has required fields' {
$result.Name | Should -Be 'PSRule.Monitor';