Skip to content

Commit 51059c4

Browse files
committed
Add publish to powershellgallery workflow #9
1 parent 4ec2890 commit 51059c4

2 files changed

Lines changed: 415 additions & 0 deletions

File tree

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
name: Manual Publish to PowerShell Gallery
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
confirm_publish:
7+
description: 'Type "PUBLISH" to confirm you want to publish to PowerShell Gallery'
8+
required: true
9+
type: string
10+
11+
jobs:
12+
publish:
13+
runs-on: windows-latest
14+
if: github.event.inputs.confirm_publish == 'PUBLISH'
15+
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v4
19+
20+
- name: Install PowerShell Module Dependencies
21+
shell: pwsh
22+
run: |
23+
Set-PSRepository PSGallery -InstallationPolicy Trusted
24+
Install-Module -Name PowerShell-Yaml -Force -SkipPublisherCheck
25+
26+
- name: Get Module Version
27+
id: version
28+
shell: pwsh
29+
run: |
30+
$manifestPath = "src/PSBlogger.psd1"
31+
$manifest = Import-PowerShellDataFile -Path $manifestPath
32+
$version = $manifest.ModuleVersion
33+
Write-Host "Module version: $version"
34+
echo "version=$version" >> $env:GITHUB_OUTPUT
35+
36+
- name: Validate Module Manifest
37+
shell: pwsh
38+
run: |
39+
# Test module manifest
40+
$manifestPath = "./src/PSBlogger.psd1"
41+
Write-Host "Testing module manifest at: $manifestPath"
42+
43+
# Test manifest syntax
44+
$manifest = Test-ModuleManifest -Path $manifestPath -Verbose
45+
Write-Host "Module manifest is valid"
46+
Write-Host "Module Name: $($manifest.Name)"
47+
Write-Host "Module Version: $($manifest.Version)"
48+
Write-Host "Module Author: $($manifest.Author)"
49+
Write-Host "Module Description: $($manifest.Description)"
50+
51+
# Check required fields for PowerShell Gallery
52+
if (-not $manifest.Description) {
53+
throw "Module description is required for PowerShell Gallery"
54+
}
55+
if (-not $manifest.Author) {
56+
throw "Module author is required for PowerShell Gallery"
57+
}
58+
59+
# Test that the module can be imported successfully
60+
Import-Module $manifestPath -Force
61+
$loadedModule = Get-Module PSBlogger
62+
Write-Host "Successfully imported module. Exported commands:"
63+
$loadedModule.ExportedCommands.Keys | Sort-Object | ForEach-Object { Write-Host " - $_" }
64+
65+
- name: Run Tests
66+
shell: pwsh
67+
run: |
68+
# Install Pester for testing
69+
Install-Module -Name Pester -Force -SkipPublisherCheck
70+
71+
# Install pandoc for tests
72+
choco install pandoc -y
73+
74+
# Change to src directory and run tests
75+
Set-Location "./src"
76+
77+
# Configure Pester
78+
$PesterConfig = @{
79+
Run = @{
80+
Path = './tests'
81+
}
82+
Output = @{
83+
Verbosity = 'Normal'
84+
}
85+
Should = @{
86+
ErrorAction = 'Stop'
87+
}
88+
}
89+
90+
# Run tests and fail if any tests fail
91+
$testResults = Invoke-Pester -Configuration $PesterConfig
92+
if ($testResults.FailedCount -gt 0) {
93+
throw "Tests failed. Cannot publish to PowerShell Gallery."
94+
}
95+
96+
- name: Publish to PowerShell Gallery
97+
shell: pwsh
98+
env:
99+
NUGET_API_KEY: ${{ secrets.POWERSHELLGALLERY_API }}
100+
run: |
101+
# Validate that we have the API key
102+
if (-not $env:NUGET_API_KEY) {
103+
throw "POWERSHELLGALLERY_API secret is not set"
104+
}
105+
106+
Write-Host "Publishing PSBlogger version ${{ steps.version.outputs.version }} to PowerShell Gallery..."
107+
108+
# Check if this version already exists
109+
try {
110+
$existingModule = Find-Module -Name PSBlogger -RequiredVersion ${{ steps.version.outputs.version }} -ErrorAction SilentlyContinue
111+
if ($existingModule) {
112+
throw "Version ${{ steps.version.outputs.version }} already exists on PowerShell Gallery"
113+
}
114+
} catch {
115+
if ($_.Exception.Message -notlike "*Version*already exists*") {
116+
Write-Host "Could not check existing versions (this is normal for new modules): $($_.Exception.Message)"
117+
} else {
118+
throw
119+
}
120+
}
121+
122+
# Actually publish the module (no -WhatIf here)
123+
try {
124+
Publish-Module -Path "./src" -NuGetApiKey $env:NUGET_API_KEY -Verbose -Force
125+
Write-Host "✅ Successfully published to PowerShell Gallery"
126+
} catch {
127+
Write-Error "❌ Failed to publish to PowerShell Gallery: $_"
128+
throw
129+
}
130+
131+
- name: Create Git Tag and Release
132+
shell: pwsh
133+
run: |
134+
# Configure git
135+
git config user.name "github-actions"
136+
git config user.email "github-actions@github.com"
137+
138+
# Check if tag already exists
139+
$tagExists = git tag -l "v${{ steps.version.outputs.version }}"
140+
if ($tagExists) {
141+
Write-Host "Tag v${{ steps.version.outputs.version }} already exists"
142+
} else {
143+
# Create and push tag
144+
git tag -a "v${{ steps.version.outputs.version }}" -m "Release version ${{ steps.version.outputs.version }}"
145+
git push origin "v${{ steps.version.outputs.version }}"
146+
Write-Host "Created and pushed tag v${{ steps.version.outputs.version }}"
147+
}
148+
149+
- name: Get Commit Messages for Release Notes
150+
id: release-notes
151+
shell: pwsh
152+
run: |
153+
# Get the previous tag
154+
$previousTag = git describe --tags --abbrev=0 "v${{ steps.version.outputs.version }}~1" 2>$null
155+
if ($LASTEXITCODE -ne 0) {
156+
# If no previous tag, get last 10 commits
157+
$commits = git log --oneline --pretty=format:"- %s (%h)" -n 10
158+
} else {
159+
# Get commits since the previous tag
160+
$commits = git log --oneline --pretty=format:"- %s (%h)" "$previousTag..v${{ steps.version.outputs.version }}"
161+
}
162+
163+
$releaseNotes = @"
164+
## Changes in version ${{ steps.version.outputs.version }}
165+
166+
$($commits -join "`n")
167+
168+
## Installation
169+
``````powershell
170+
Install-Module -Name PSBlogger -RequiredVersion ${{ steps.version.outputs.version }}
171+
``````
172+
"@
173+
174+
# Escape for GitHub Actions
175+
$releaseNotes = $releaseNotes -replace "`r`n", "`n" -replace "`n", "%0A"
176+
echo "release-notes=$releaseNotes" >> $env:GITHUB_OUTPUT
177+
178+
- name: Create GitHub Release
179+
uses: actions/create-release@v1
180+
env:
181+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
182+
with:
183+
tag_name: v${{ steps.version.outputs.version }}
184+
release_name: Release v${{ steps.version.outputs.version }}
185+
body: ${{ steps.release-notes.outputs.release-notes }}
186+
draft: false
187+
prerelease: false
188+
189+
- name: Notify Success
190+
if: success()
191+
shell: pwsh
192+
run: |
193+
Write-Host "🎉 Successfully published PSBlogger v${{ steps.version.outputs.version }} to PowerShell Gallery!"
194+
Write-Host "📦 Module is now available at: https://www.powershellgallery.com/packages/PSBlogger/${{ steps.version.outputs.version }}"
195+
Write-Host "🏷️ Created release: https://github.com/${{ github.repository }}/releases/tag/v${{ steps.version.outputs.version }}"

0 commit comments

Comments
 (0)