Repository navigation
Expand file tree
/
Copy pathcreate-powershell-ci.ps1
More file actions
106 lines (90 loc) · 3.78 KB
/
Copy pathcreate-powershell-ci.ps1
File metadata and controls
106 lines (90 loc) · 3.78 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
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost", "")]
<#
.SYNOPSIS
Generates the PowerShell CI GitHub Actions workflow file.
.DESCRIPTION
Creates (or overwrites) .github\workflows\powershell-ci.yml with a
multi-OS CI pipeline that:
- Triggers on pushes and pull requests to the main branch, and also
supports manual dispatch (workflow_dispatch).
- Runs on a 3-OS matrix: ubuntu-latest, windows-latest, macos-latest.
- Installs PSScriptAnalyzer and Pester from the PowerShell Gallery.
- Executes a PSScriptAnalyzer security scan (Error/Warning severity,
security rules only) and fails the job if any issues are detected.
- Runs Pester tests from the ./tests directory (if present) and fails
the job if any tests fail.
The .github\workflows directory is created automatically if absent.
.PARAMETER (none)
This script accepts no parameters.
.EXAMPLE
.\create-powershell-ci.ps1
# Writes powershell-ci.yml and prints the file path on success.
.NOTES
- Run from the repository root so the workflow path resolves correctly.
- This script generates only powershell-ci.yml. Use
create-download-cert.ps1 to also generate download-cert.yml.
- Existing files with the same name will be overwritten silently.
- PSAvoidUsingWriteHost is suppressed intentionally for status output.
#>
param()
# Workflow folder
$workflowDir = ".github\workflows"
if (-not (Test-Path $workflowDir)) {
New-Item -ItemType Directory -Path $workflowDir -Force | Out-Null
}
# powershell-ci.yml
$powershellCiYaml = @'
name: PowerShell CI
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
workflow_dispatch:
jobs:
powershell-ci:
name: PowerShell CI (${{ matrix.os }})
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install Modules
shell: pwsh
run: |
Set-PSRepository PSGallery -InstallationPolicy Trusted
Install-Module PSScriptAnalyzer -Force -Scope CurrentUser
Install-Module Pester -Force -Scope CurrentUser -SkipPublisherCheck
- name: Run PSScriptAnalyzer (Security Scan)
shell: pwsh
run: |
$results = Invoke-ScriptAnalyzer -Path . -Recurse -Severity Error,Warning |
Where-Object { $_.RuleName -like "*Security*" }
if ($results) {
$results | Format-Table
Write-Host "::error title=Security Scan [Run #$($env:GITHUB_RUN_NUMBER)]::Potential security issues found in PowerShell scripts."
throw "Security issues detected by PSScriptAnalyzer"
} else {
Write-Host "::notice title=Security Scan [Run #$($env:GITHUB_RUN_NUMBER)]::No common security issues found."
}
- name: Run Pester Tests
shell: pwsh
run: |
if (Test-Path ./tests) {
$results = Invoke-Pester -Path ./tests -PassThru
if ($results.FailedCount -gt 0) {
Write-Host "::error title=Pester Tests [Run #$($env:GITHUB_RUN_NUMBER)]::$($results.FailedCount) tests failed."
throw "Pester tests failed."
} else {
Write-Host "::notice title=Pester Tests [Run #$($env:GITHUB_RUN_NUMBER)]::All tests passed successfully."
}
} else {
Write-Host "::notice title=Pester Tests [Run #$($env:GITHUB_RUN_NUMBER)]::No Pester tests found in ./tests."
}
'@
$powershellCiFile = Join-Path $workflowDir "powershell-ci.yml"
$powershellCiYaml | Set-Content -Path $powershellCiFile -Force
Write-Host "$powershellCiFile created successfully."