11# This is a basic workflow to help you get started with Actions
22
3- name : publish to powershell gallery
3+ name : Publish to PowerShell Gallery
44
55# Controls when the action will run.
66on :
7- # Triggers the workflow on push or pull request events but only for the master branch
8- # release:
9- # types: [created,published]
107 push :
11- branches : [master]
8+ branches : [ master ]
129 paths :
13- - " **.psd1"
14-
15- # Allows you to run this workflow manually from the Actions tab
10+ - " **.psd1" # run when manifest changes (usually when version bumps)
1611 workflow_dispatch :
1712
1813# A workflow run is made up of one or more jobs that can run sequentially or in parallel
1914jobs :
20- # This workflow contains a single job called "build"
21- publishmodule :
22- # The type of runner that the job will run on
15+ publish :
2316 runs-on : ubuntu-latest
24-
25- # Steps represent a sequence of tasks that will be executed as part of the job
2617 steps :
27- # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
28- - uses : actions/checkout@v4
29- # run powershell script to install the powershellextension module, ensure it can install from powershell gallery
30- - name : Install PowerShell Extension
18+ - name : Checkout
19+ uses : actions/checkout@v4
20+
21+ - name : Set up PowerShellGet
22+ shell : pwsh
23+ run : |
24+ $ErrorActionPreference = 'Stop'
25+ # Avoid prompts and ensure we can install to the current user on hosted runners
26+ if (-not (Get-PSRepository -Name 'PSGallery' -ErrorAction SilentlyContinue)) {
27+ Register-PSRepository -Name 'PSGallery' -SourceLocation 'https://www.powershellgallery.com/api/v2' -InstallationPolicy Trusted
28+ } else {
29+ Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted
30+ }
31+ Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -ErrorAction Stop
32+ Install-Module PowerShellGet -Scope CurrentUser -Force -AllowClobber -ErrorAction Stop
33+
34+ - name : Publish module (skip if version exists)
35+ env :
36+ NUGET_API_KEY : ${{ secrets.NUGETKEY }}
37+ shell : pwsh
3138 run : |
32- pwsh -Command "Install-Module -Name PowerShellGet -Force -AllowClobber"
33- pwsh -Command "Install-Module -Name PowerShellExtension -Force -AllowClobber"
34- - name : Publish Module to PowerShell Gallery
35- uses : pcgeek86/publish-powershell-module-action@v20
36- id : publish-module
37- with :
38- NuGetApiKey : ${{ secrets.NUGETKEY }}
39+ $ErrorActionPreference = 'Stop'
40+ $moduleFolder = Join-Path $PWD 'code365scripts.openai'
41+ if (-not (Test-Path $moduleFolder)) {
42+ throw "Module folder not found: $moduleFolder"
43+ }
44+
45+ $manifest = Get-ChildItem -Path $moduleFolder -Filter *.psd1 | Select-Object -First 1
46+ if (-not $manifest) { throw "No module manifest (*.psd1) found under $moduleFolder" }
47+
48+ $mm = Test-ModuleManifest -Path $manifest.FullName
49+ $localVersion = [version]$mm.Version
50+ # Use manifest base name as module name (matches folder and PSGallery name)
51+ $moduleName = $manifest.BaseName
52+
53+ Write-Host "Local module: $moduleName v$localVersion"
54+ $published = Find-Module -Name $moduleName -Repository PSGallery -ErrorAction SilentlyContinue
55+ if ($published -and ([version]$published.Version -ge $localVersion)) {
56+ Write-Host "A version ($($published.Version)) already exists on PSGallery for $moduleName. Skipping publish."
57+ exit 0
58+ }
59+
60+ if (-not $env:NUGET_API_KEY) { throw 'NuGet API key is not set. Configure secrets.NUGETKEY in the repository settings.' }
61+
62+ Publish-Module -Path $moduleFolder -Repository 'PSGallery' -NuGetApiKey $env:NUGET_API_KEY -Verbose
0 commit comments