Skip to content

Commit f26ba7c

Browse files
committed
ci: add workflow that publishes to the PSGallery on changes to important files.
1 parent b089172 commit f26ba7c

File tree

2 files changed

+75
-4
lines changed

2 files changed

+75
-4
lines changed

.github/workflows/publish.yml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
name: Build and Publish to PSGallery
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
paths:
7+
- 'Public/**'
8+
- 'Private/**'
9+
- 'Classes/**'
10+
- 'Build/**'
11+
- 'Locksmith2.psm1'
12+
- 'Locksmith2.psd1'
13+
- 'LICENSE'
14+
workflow_dispatch: # Allow manual trigger
15+
16+
jobs:
17+
build-and-publish:
18+
runs-on: ubuntu-latest
19+
20+
steps:
21+
- name: Checkout code
22+
uses: actions/checkout@v4
23+
24+
- name: Install required PowerShell modules
25+
shell: pwsh
26+
run: |
27+
Write-Host "Installing required modules..." -ForegroundColor Cyan
28+
Install-Module -Name Pester -AllowClobber -Scope CurrentUser -SkipPublisherCheck -Force
29+
Install-Module -Name PSScriptAnalyzer -AllowClobber -Scope CurrentUser -Force
30+
Install-Module -Name PSPublishModule -AllowClobber -Scope CurrentUser -Force
31+
Write-Host "Modules installed successfully" -ForegroundColor Green
32+
33+
- name: Run Pester tests
34+
shell: pwsh
35+
run: |
36+
Write-Host "Running Pester tests..." -ForegroundColor Cyan
37+
$testResults = Invoke-Pester -Path ./Tests/ -PassThru -CI -Output Detailed
38+
Write-Host "Tests completed: $($testResults.PassedCount) passed, $($testResults.FailedCount) failed" -ForegroundColor Cyan
39+
40+
if ($testResults.FailedCount -gt 0) {
41+
Write-Host "Tests failed! Build aborted." -ForegroundColor Red
42+
exit 1
43+
}
44+
45+
Write-Host "All tests passed!" -ForegroundColor Green
46+
47+
- name: Build and Publish to PowerShell Gallery
48+
shell: pwsh
49+
env:
50+
PSGALLERY_API_KEY: ${{ secrets.PSGALLERY_API_KEY }}
51+
run: |
52+
Write-Host "Building and publishing Locksmith2 module..." -ForegroundColor Cyan
53+
./Build/Build-Module.ps1 -PublishToPSGallery -PSGalleryAPIKey $env:PSGALLERY_API_KEY
54+
Write-Host "Published successfully" -ForegroundColor Green
55+
56+
- name: Get module version
57+
id: version
58+
shell: pwsh
59+
run: |
60+
$manifest = Import-PowerShellDataFile ./Locksmith2.psd1
61+
$version = $manifest.ModuleVersion
62+
echo "version=$version" >> $env:GITHUB_OUTPUT
63+
Write-Host "Module version: $version" -ForegroundColor Green

Build/Build-Module.ps1

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
# A CalVer string if you need to manually override the default yyyy.M.d version string.
33
[string]$CalVer,
44
[switch]$PublishToPSGallery,
5-
[string]$PSGalleryAPIPath
5+
[string]$PSGalleryAPIPath,
6+
[string]$PSGalleryAPIKey
67
)
78

89
if (Get-Module -Name 'PSPublishModule' -ListAvailable) {
@@ -113,8 +114,15 @@ Build-Module -ModuleName 'Locksmith2' {
113114
New-ConfigurationArtefact -Type Packed -Enable -Path "$PSScriptRoot\..\Artefacts\Packed" -IncludeTagName
114115

115116
# global options for publishing to github/psgallery
116-
if($PublishToPSGallery) {
117-
New-ConfigurationPublish -Type PowerShellGallery -FilePath $PSGalleryAPIPath -Enabled:$true
117+
if ($PublishToPSGallery) {
118+
if ($PSGalleryAPIKey) {
119+
# Use API key directly (from environment variable in CI)
120+
New-ConfigurationPublish -Type PowerShellGallery -ApiKey $PSGalleryAPIKey -Enabled:$true
121+
} elseif ($PSGalleryAPIPath) {
122+
# Use API key from file (for local development)
123+
New-ConfigurationPublish -Type PowerShellGallery -FilePath $PSGalleryAPIPath -Enabled:$true
124+
} else {
125+
Write-Error "PublishToPSGallery specified but neither PSGalleryAPIKey nor PSGalleryAPIPath provided."
126+
}
118127
}
119-
#New-ConfigurationPublish -Type GitHub -FilePath 'C:\Support\Important\GitHubAPI.txt' -UserName 'CompanyName' -Enabled:$false
120128
}

0 commit comments

Comments
 (0)