-
Notifications
You must be signed in to change notification settings - Fork 0
208 lines (185 loc) · 7.7 KB
/
Copy pathpowershell-ci.yml
File metadata and controls
208 lines (185 loc) · 7.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# PowerShell CI Workflow
#
# Purpose: Run PSScriptAnalyzer linting and Pester tests for PowerShell scripts
# Runs on: push and pull_request events
#
# Jobs:
# - powershell-lint: Runs PSScriptAnalyzer on all .ps1 files (skips if no files found)
# - test: Runs Pester tests on Windows, macOS, and Linux (skips if no *.Tests.ps1 files found)
#
# Note: This workflow is OPTIONAL and can be removed for non-PowerShell projects.
# To remove PowerShell support entirely, delete:
# - .github/workflows/powershell-ci.yml (this file)
# - .github/instructions/powershell.instructions.md
# - .github/linting/PSScriptAnalyzerSettings.psd1
# - tests/PowerShell/ directory
# - templates/powershell/ directory
#
# Configuration:
# - Linting: .github/linting/PSScriptAnalyzerSettings.psd1
# - Testing: Pester configuration is inline in the workflow steps below
#
# Template Repository Consideration:
# This workflow does NOT use path-based filtering (on.push.paths or
# on.pull_request.paths) because this is a template repository. Path
# filtering would prevent the workflow from being properly tested and
# copied by users of the template. Template repositories should run all
# workflows on all changes to ensure accuracy when the template is used.
name: PowerShell CI
on:
push:
branches: ["**"]
pull_request:
branches: ["**"]
permissions:
contents: read
jobs:
powershell-lint:
name: Lint (PSScriptAnalyzer)
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v6
- name: Find PowerShell files
id: find-ps1
run: |
# Find all .ps1 files in the repository
ps1_files=$(find . -name "*.ps1" -type f 2>/dev/null | grep -v "node_modules" || true)
if [ -z "$ps1_files" ]; then
echo "No PowerShell files found in repository"
echo "found=false" >> "$GITHUB_OUTPUT"
else
echo "Found PowerShell files:"
echo "$ps1_files"
echo "found=true" >> "$GITHUB_OUTPUT"
fi
- name: Install PSScriptAnalyzer
if: steps.find-ps1.outputs.found == 'true'
shell: pwsh
run: |
if (-not (Get-Module -ListAvailable -Name PSScriptAnalyzer)) {
Install-Module -Name PSScriptAnalyzer -Force -Scope CurrentUser
}
Import-Module PSScriptAnalyzer
- name: Run PSScriptAnalyzer
if: steps.find-ps1.outputs.found == 'true'
shell: pwsh
run: |
$settingsPath = ".github/linting/PSScriptAnalyzerSettings.psd1"
$ps1Files = Get-ChildItem -Path . -Filter "*.ps1" -Recurse -File |
Where-Object { $_.FullName -notmatch "node_modules" }
# Per-file rule exclusions for files that cannot be modified to
# satisfy certain rules (e.g., v1.0-targeted templates with
# intentionally unused parameters, or v1.0 scripts that cannot
# use [CmdletBinding(SupportsShouldProcess)] attributes).
$perFileExclusions = @{
'_RobustCloudServiceFunctionTemplate.ps1' = @(
'PSReviewUnusedParameter',
'PSUseDeclaredVarsMoreThanAssignments'
)
'_SimpleFunctionTemplate.ps1' = @(
'PSReviewUnusedParameter',
'PSUseDeclaredVarsMoreThanAssignments'
)
'Test-FileWriteability.ps1' = @(
'PSUseShouldProcessForStateChangingFunctions'
)
'Export-ADDSOUPermissions.ps1' = @(
'PSUseShouldProcessForStateChangingFunctions'
)
}
$hasErrors = $false
foreach ($file in $ps1Files) {
Write-Host "Analyzing: $($file.FullName)"
$results = Invoke-ScriptAnalyzer -Path $file.FullName -Settings $settingsPath
# Filter out per-file excluded rules
$excludedRules = $perFileExclusions[$file.Name]
if ($excludedRules) {
$results = $results | Where-Object { $_.RuleName -notin $excludedRules }
}
if ($results) {
$hasErrors = $true
foreach ($result in $results) {
$severity = $result.Severity
$message = "$($file.Name):$($result.Line):$($result.Column): [$severity] $($result.RuleName) - $($result.Message)"
if ($severity -eq 'Error') {
Write-Host "::error file=$($file.FullName),line=$($result.Line),col=$($result.Column)::$($result.RuleName) - $($result.Message)"
} else {
Write-Host "::warning file=$($file.FullName),line=$($result.Line),col=$($result.Column)::$($result.RuleName) - $($result.Message)"
}
}
}
}
if ($hasErrors) {
Write-Host ""
Write-Host "::error::PSScriptAnalyzer found issues. See above for details."
exit 1
} else {
Write-Host ""
Write-Host "All PowerShell files passed linting."
}
- name: Skip - No PowerShell files
if: steps.find-ps1.outputs.found != 'true'
run: echo "Skipping PSScriptAnalyzer - no PowerShell files found in repository"
# Pester Test Job
#
# Purpose: Run Pester 5.x tests across multiple operating systems
#
# Customization:
# - To change test location, update $config.Run.Path in "Run Pester tests" step
# - To change output format, update $config.TestResult.OutputFormat
# - To add test filters, use $config.Filter.* properties
#
# Requirements:
# - Pester test files must use *.Tests.ps1 naming convention
# - Tests should be placed in tests/PowerShell/ or tests/ directory
# - See templates/powershell/Example.Tests.ps1 for test template
test:
name: PowerShell Tests (Pester)
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- name: Checkout repository
uses: actions/checkout@v6
- name: Find Pester test files
id: find-tests
shell: pwsh
run: |
$testFiles = Get-ChildItem -Path . -Filter "*.Tests.ps1" -Recurse -File |
Where-Object { $_.FullName -notmatch "node_modules" }
if ($testFiles.Count -eq 0) {
Write-Host "No Pester test files found"
"found=false" | Out-File -FilePath $env:GITHUB_OUTPUT -Append
} else {
Write-Host "Found $($testFiles.Count) Pester test file(s):"
$testFiles | ForEach-Object { Write-Host " - $($_.FullName)" }
"found=true" | Out-File -FilePath $env:GITHUB_OUTPUT -Append
}
- name: Install Pester
if: steps.find-tests.outputs.found == 'true'
shell: pwsh
run: |
Install-Module -Name Pester -MinimumVersion 5.0 -Force -Scope CurrentUser
Import-Module Pester
Write-Host "Pester version: $((Get-Module Pester).Version)"
- name: Run Pester tests
if: steps.find-tests.outputs.found == 'true'
shell: pwsh
run: |
$config = New-PesterConfiguration
# Run tests from tests/ directory (includes subdirectories)
$config.Run.Path = "tests/"
# Exit with non-zero code if tests fail
$config.Run.Exit = $true
# Enable test result output
$config.TestResult.Enabled = $true
$config.TestResult.OutputFormat = "NUnitXml"
$config.TestResult.OutputPath = "testResults.xml"
# Show detailed output
$config.Output.Verbosity = "Detailed"
Invoke-Pester -Configuration $config
- name: Skip - No Pester tests
if: steps.find-tests.outputs.found != 'true'
run: echo "Skipping Pester tests - no *.Tests.ps1 files found in repository"