Merge pull request #29 from PixelRobots/powershellmodule-fix #16
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Publish Module to PowerShell Gallery | |
on: | |
push: | |
tags: | |
- 'v*' | |
release: | |
types: | |
- published | |
workflow_dispatch: | |
jobs: | |
publish: | |
runs-on: windows-latest | |
steps: | |
# Checkout the repository | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
# Validate PowerShell Code | |
- name: Run PSScriptAnalyzer and format output in Markdown | |
run: | | |
# Run ScriptAnalyzer and capture the output | |
$results = Invoke-ScriptAnalyzer -Path ./ -Recurse -Severity 'Error', 'Warning' -ExcludeRule PSAvoidUsingWriteHost | |
# Separate the warnings and errors | |
$warnings = $results | Where-Object { $_.Severity -eq 'Warning' } | |
$errors = $results | Where-Object { $_.Severity -eq 'Error' } | |
# Debug: Output the count of warnings and errors | |
Write-Host "Found $($warnings.Count) warnings" | |
Write-Host "Found $($errors.Count) errors" | |
# Create Markdown formatted tables | |
function ConvertTo-MarkdownTable { | |
param ($items) | |
$header = "| RuleName | Severity | ScriptName | Line | Message |" | |
$separator = "| --- | --- | --- | --- | --- |" | |
$rows = foreach ($item in $items) { | |
"| $($item.RuleName) | $($item.Severity) | $($item.ScriptName) | $($item.Line) | $($item.Message) |" | |
} | |
# Join rows into a single string | |
return "$header`n$separator`n$($rows -join "`n")" | |
} | |
# Append warnings to the GitHub Actions summary (if any) | |
if ($warnings.Count -gt 0) { | |
$warningTable = ConvertTo-MarkdownTable -items $warnings | |
Add-Content -Path $env:GITHUB_STEP_SUMMARY -Value "### PSScriptAnalyzer Warnings`n" | |
Add-Content -Path $env:GITHUB_STEP_SUMMARY -Value "$warningTable`n" | |
} else { | |
Add-Content -Path $env:GITHUB_STEP_SUMMARY -Value "### No Warnings Found`n" | |
} | |
# Append errors to the GitHub Actions summary (if any) | |
if ($errors.Count -gt 0) { | |
$errorTable = ConvertTo-MarkdownTable -items $errors | |
Add-Content -Path $env:GITHUB_STEP_SUMMARY -Value "### PSScriptAnalyzer Errors`n" | |
Add-Content -Path $env:GITHUB_STEP_SUMMARY -Value "$errorTable`n" | |
} else { | |
Add-Content -Path $env:GITHUB_STEP_SUMMARY -Value "### No Errors Found`n" | |
} | |
# Fail the job if there are any errors | |
if ($errors.Count -gt 0) { | |
Write-Error "PSScriptAnalyzer found errors." | |
} | |
shell: pwsh | |
# Validate the module (ensure it loads correctly) | |
- name: Validate PowerShell Module | |
run: | | |
$manifest = Test-ModuleManifest -Path ./KubeTidy.psd1 | |
Write-Host "Validated Module Version: $($manifest.Version)" | |
# Publish to PowerShell Gallery | |
- name: Publish to PowerShell Gallery | |
run: | | |
Install-Module -Name PowerShellGet -Force -AllowClobber | |
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted | |
Publish-Module -Path ./ -NuGetApiKey $env:PSGALLERY_API_KEY -Repository PSGallery | |
env: | |
PSGALLERY_API_KEY: ${{ secrets.PSGALLERY_API_KEY }} |