PSScriptAnalyzer #113
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
# Modification of Microsoft's PSScriptAnalyzer GitHub Action. | |
# | |
# ~ This workflow uses actions that are not certified by GitHub. | |
# ~ They are provided by a third-party and are governed by | |
# ~ separate terms of service, privacy policy, and support | |
# ~ documentation. | |
# ~ | |
# ~ https://github.com/microsoft/psscriptanalyzer-action | |
# ~ https://pester.dev/docs/usage/code-coverage#integrating-with-github-actions | |
name: PSScriptAnalyzer | |
on: | |
push: | |
branches: [ "main" ] | |
pull_request: | |
branches: [ "main" ] | |
schedule: | |
# 4:22 AM on Saturdays | |
- cron: '22 4 * * 6' | |
env: | |
POWERSHELL_TELEMETRY_OPTOUT: 1 | |
permissions: | |
contents: read | |
jobs: | |
build: | |
name: PSScriptAnalyzer | |
permissions: | |
# Needed by actions/checkout to fetch code | |
contents: read | |
# Needed by github/codeql-action/upload-sarif to upload code scan results | |
security-events: write | |
# Private repository permission to get the Action run status | |
actions: read | |
# Run on multiple operating systems | |
# platform = Operating System for Github Action (https://github.com/actions/runner-images) | |
# pestertag = Pester Tag for platform specific testing (https://pester.dev/docs/usage/tags) | |
# shell = PowerShell executable (Windows PowerShell vs Powershell v6+) | |
strategy: | |
matrix: | |
info: | |
- platform: "windows-latest" | |
pestertag: "WindowsOnly" | |
shell: "powershell" | |
- platform: "windows-latest" | |
pestertag: "WindowsOnly" | |
shell: "pwsh" | |
- platform: "macos-latest" | |
pestertag: "MacosOnly" | |
shell: "pwsh" | |
#platform: [windows-latest, macos-latest, ubuntu-latest] | |
runs-on: ${{ matrix.info.platform }} | |
steps: | |
# Checkout repo | |
- name: Checkout | |
uses: actions/checkout@v4 | |
# Scan code (Pwsh) | |
# Installs the following modules for PowerShell | |
# - PSScriptAnalyzer (https://github.com/PowerShell/PSScriptAnalyzer) | |
# - ConvertToSARIF (https://github.com/microsoft/ConvertToSARIF) | |
- name: Run PSScriptAnalyzer | |
uses: microsoft/psscriptanalyzer-action@v1.1 | |
with: | |
path: .\ | |
recurse: true | |
output: psscriptanalyzer-results-${{ matrix.info.platform }}.sarif | |
# Upload the SARIF file generated from PSScriptAnalyzer. | |
- name: Upload PSScriptAnalyzer SARIF results file | |
uses: github/codeql-action/upload-sarif@v3 | |
with: | |
sarif_file: psscriptanalyzer-results-${{ matrix.info.platform }}.sarif | |
# Test code | |
# Two different paths since shell parameter can't be ${{ matrix.info.shell }} | |
# Installs the following modules for PowerShell | |
# - Pester (https://github.com/pester/Pester) | |
# - PSToml (https://github.com/jborean93/PSToml) | |
# - powershell-yaml (https://github.com/cloudbase/powershell-yaml) | |
- name: Install modules - Pwsh | |
if: ${{ matrix.info.shell == 'pwsh' }} | |
shell: pwsh | |
run: | | |
Set-PSRepository PSGallery -InstallationPolicy Trusted | |
Install-Module -Name Pester -Force -Scope CurrentUser | |
Install-Module -Name PSToml -Force -Scope CurrentUser | |
Install-Module -Name powershell-yaml -Force -Scope CurrentUser | |
- name: Install modules - Windows PowerShell | |
if: ${{ matrix.info.shell == 'powershell' }} | |
shell: powershell | |
run: | | |
Set-PSRepository PSGallery -InstallationPolicy Trusted | |
Install-Module -Name Pester -Force -Scope CurrentUser | |
Install-Module -Name PSToml -Force -Scope CurrentUser | |
Install-Module -Name powershell-yaml -Force -Scope CurrentUser | |
# Run Pester tests with Code Coverage | |
# Two different steps since shell parameter can't be ${{ matrix.info.shell }} | |
- name: Run Pester Tests - Pwsh | |
if: ${{ matrix.info.shell == 'pwsh' }} | |
shell: pwsh | |
run: | | |
$pesterconfig = New-PesterConfiguration | |
$pesterconfig.Run.Path = "." | |
$pesterconfig.CodeCoverage.Enabled = $true | |
$pesterconfig.CodeCoverage.OutputPath = "pester-coverage-${{ matrix.info.platform }}.xml" | |
$pesterconfig.CodeCoverage.CoveragePercentTarget = 75 | |
$pesterconfig.TestResult.Enabled = $true | |
$pesterconfig.TestResult.OutputPath = "pester-tests-${{ matrix.info.platform }}.xml" | |
$pesterconfig.Filter.Tag = "${{ matrix.info.pestertag }}" | |
Invoke-Pester -Configuration $pesterconfig | |
- name: Run Pester Tests - Windows PowerShell | |
if: ${{ matrix.info.shell == 'powershell' }} | |
shell: powershell | |
run: | | |
$pesterconfig = New-PesterConfiguration | |
$pesterconfig.Run.Path = "." | |
$pesterconfig.CodeCoverage.Enabled = $true | |
$pesterconfig.CodeCoverage.OutputPath = "pester-coverage-${{ matrix.info.platform }}.xml" | |
$pesterconfig.CodeCoverage.CoveragePercentTarget = 75 | |
$pesterconfig.TestResult.Enabled = $true | |
$pesterconfig.TestResult.OutputPath = "pester-tests-${{ matrix.info.platform }}.xml" | |
$pesterconfig.Filter.Tag = "${{ matrix.info.pestertag }}" | |
Invoke-Pester -Configuration $pesterconfig | |
# Upload the Pester results. | |
- name: Upload Pester reports | |
if: ${{ success() }} | |
uses: actions/upload-artifact@v4 | |
with: | |
name: pester-report-${{ matrix.info.shell }}-${{ matrix.info.platform }} | |
# Includes tests and code coverage. | |
path: | | |
pester-coverage-${{ matrix.info.platform }}.xml | |
pester-tests-${{ matrix.info.platform }}.xml | |
if-no-files-found: warn | |
retention-days: 0 | |
compression-level: 6 | |
overwrite: false | |
include-hidden-files: false |