Skip to content

publish to powershell gallery #79

publish to powershell gallery

publish to powershell gallery #79

name: publish to powershell gallery
on:
push:
branches: [master]
paths:
- "**.psd1"
workflow_dispatch:
jobs:
publishmodule:
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
- name: Validate and publish module
shell: pwsh
run: |
# Module path and name
$ModulePath = ".\code365scripts.openai"
$ModuleName = "code365scripts.openai"
Write-Host "🚀 Starting PowerShell module publishing: $ModuleName" -ForegroundColor Green
# 1. Validate module manifest
Write-Host "📋 Validating module manifest..." -ForegroundColor Yellow
try {
$manifest = Test-ModuleManifest -Path "$ModulePath\$ModuleName.psd1"
Write-Host "✅ Module manifest validation successful" -ForegroundColor Green
Write-Host " Module version: $($manifest.Version)" -ForegroundColor Cyan
Write-Host " Exported functions: $($manifest.ExportedFunctions.Keys -join ', ')" -ForegroundColor Cyan
}
catch {
Write-Error "❌ Module manifest validation failed: $_"
exit 1
}
# 2. Install required dependencies
Write-Host "📦 Installing PowerShellGet and required modules..." -ForegroundColor Yellow
if (-not (Get-Module -ListAvailable -Name PowerShellGet)) {
Install-Module -Name PowerShellGet -Force -AllowClobber -Scope CurrentUser
}
# 3. Set PowerShell Gallery as trusted repository
Write-Host "🔒 Configuring PowerShell Gallery..." -ForegroundColor Yellow
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
# 4. Install required modules if specified in manifest
$data = Import-PowerShellDataFile "$ModulePath\$ModuleName.psd1"
if($data.RequiredModules) {
Write-Host "📦 Installing required modules..." -ForegroundColor Yellow
$data.RequiredModules | Foreach-Object {
Write-Host " Installing: $($_.ModuleName) version $($_.ModuleVersion)" -ForegroundColor Cyan
Install-Module -Name $_.ModuleName -RequiredVersion $_.ModuleVersion -Scope CurrentUser -Force
}
}
# 5. Check if module already exists and version comparison
Write-Host "🔍 Checking if module exists in PowerShell Gallery..." -ForegroundColor Yellow
try {
$existingModule = Find-Module -Name $ModuleName -ErrorAction Stop
Write-Host "📦 Found existing module version: $($existingModule.Version)" -ForegroundColor Cyan
if ($manifest.Version -le $existingModule.Version) {
Write-Error "⚠️ Current module version ($($manifest.Version)) is not higher than published version ($($existingModule.Version))"
exit 1
}
}
catch {
Write-Host "📦 This is a new module, proceeding with first-time publishing" -ForegroundColor Green
}
# 6. Clean up unnecessary files for publishing
Write-Host "🧹 Cleaning up unnecessary files..." -ForegroundColor Yellow
if (Test-Path ".git") { Remove-Item -Path ".git" -Recurse -Force }
if (Test-Path ".github") { Remove-Item -Path ".github" -Recurse -Force }
if (Test-Path ".vscode") { Remove-Item -Path ".vscode" -Recurse -Force }
# 7. Publish module
Write-Host "🚀 Publishing module to PowerShell Gallery..." -ForegroundColor Yellow
try {
Publish-Module -Path $ModulePath -NuGetApiKey "${{ secrets.NUGETKEY }}" -Verbose
Write-Host "🎉 Module published successfully!" -ForegroundColor Green
Write-Host "📦 Module link: https://www.powershellgallery.com/packages/$ModuleName" -ForegroundColor Cyan
}
catch {
Write-Error "❌ Module publishing failed: $_"
exit 1
}
Write-Host "✅ Publishing process completed!" -ForegroundColor Green