11name : Publish Monkey365 to PowerShell Gallery
22
33on :
4+ workflow_dispatch :
45 release :
5- types : [published]
6+ types :
7+ - published
8+
9+ permissions :
10+ contents : read
611
712jobs :
8- PublishMonkeyToGallery :
13+ publish :
14+ name : Publish Monkey365
915 runs-on : windows-latest
16+
1017 steps :
11- - uses : actions/checkout@v6
18+ - name : Checkout released source
19+ uses : actions/checkout@v6
20+ with :
21+ fetch-depth : 0
22+ ref : ${{ github.event.release.tag_name || github.ref }}
23+
1224 - name : Publish Monkey365
1325 shell : powershell
1426 env :
1527 NUGET_KEY : ${{ secrets.PSGALLERY }}
1628 run : |
17- $remove = @('.git', '.github', '.gitignore', 'docs', 'mkdocs.yml')
18- Write-Output "INFO: Preparing Windows-based GitHub runner for publishing module to the PowerShell Gallery."
19- Write-Output "INFO: Setting the PowerShell Gallery as a trusted repository."
20- Set-PSRepository psgallery -InstallationPolicy trusted
21- Write-Output "INFO: Locating module manifest in '$env:GITHUB_WORKSPACE'."
22- $moduleManifest = Get-ChildItem -Path $env:GITHUB_WORKSPACE -Filter *.psd1
23- If ($moduleManifest) {
24- Write-Output ("SUCCESS: Manifest {0} found in {1}." -f $moduleManifest.FullName, $env:GITHUB_WORKSPACE)
25- } Else {
26- Write-Output ("FAILURE: Manifest not found in {0}." -f $env:GITHUB_WORKSPACE)
29+ $ErrorActionPreference = 'Stop'
30+ Set-StrictMode -Version Latest
31+
32+ if ([string]::IsNullOrWhiteSpace($env:NUGET_KEY)) {
33+ throw "The PSGALLERY repository secret is missing or empty."
2734 }
28- If ($moduleManifest.Name -match '^(.*)\.psd1$') {
29- $moduleName = $Matches[1]
30- Write-Output "SUCCESS: Determining module name from manifest file name '$moduleName'."
31- } Else {
32- Write-Error "FAILED: Determining module name from manifest file name '$moduleName'."
35+
36+ Write-Host "PowerShell version: $($PSVersionTable.PSVersion)"
37+ Write-Host "Workspace: $env:GITHUB_WORKSPACE"
38+ Write-Host "Ref: $env:GITHUB_REF"
39+
40+ Install-PackageProvider `
41+ -Name NuGet `
42+ -MinimumVersion 2.8.5.201 `
43+ -Force
44+
45+ Set-PSRepository `
46+ -Name PSGallery `
47+ -InstallationPolicy Trusted
48+
49+ $moduleManifests = @(
50+ Get-ChildItem `
51+ -Path $env:GITHUB_WORKSPACE `
52+ -Filter '*.psd1' `
53+ -File
54+ )
55+
56+ if ($moduleManifests.Count -ne 1) {
57+ $found = $moduleManifests.FullName -join ', '
58+ throw "Expected exactly one root module manifest, found $($moduleManifests.Count): $found"
3359 }
60+
61+ $moduleManifest = $moduleManifests[0]
3462 $manifest = Test-ModuleManifest -Path $moduleManifest.FullName
35- $prerelease = $manifest.PrivateData.PSData['Prerelease']
36- If ($prerelease -and $prerelease.StartsWith('-')){
37- $version = ("{0}{1}" -f $manifest.version,$prerelease)
38- } ElseIf($null -eq $prerelease){
39- $version = $manifest.version
63+
64+ $moduleName = $moduleManifest.BaseName
65+ $prerelease = $manifest.PrivateData.PSData.Prerelease
66+
67+ if ([string]::IsNullOrWhiteSpace($prerelease)) {
68+ $version = $manifest.Version.ToString()
69+ }
70+ else {
71+ $version = '{0}-{1}' -f (
72+ $manifest.Version.ToString(),
73+ $prerelease.TrimStart('-')
74+ )
75+ }
76+
77+ Write-Host "Module: $moduleName"
78+ Write-Host "Version: $version"
79+
80+ $modulePath = Join-Path $env:RUNNER_TEMP $moduleName
81+ New-Item -Path $modulePath -ItemType Directory -Force | Out-Null
82+
83+ $excludedItems = @(
84+ '.git',
85+ '.github',
86+ '.gitignore',
87+ 'docs',
88+ 'mkdocs.yml'
89+ )
90+
91+ Get-ChildItem -Path $env:GITHUB_WORKSPACE -Force |
92+ Where-Object Name -NotIn $excludedItems |
93+ Copy-Item -Destination $modulePath -Recurse -Force
94+
95+ $stagedManifest = Join-Path $modulePath "$moduleName.psd1"
96+
97+ if (-not (Test-Path -LiteralPath $stagedManifest)) {
98+ throw "Staged module manifest not found: $stagedManifest"
99+ }
100+
101+ Test-ModuleManifest -Path $stagedManifest | Out-Null
102+
103+ $findParameters = @{
104+ Name = $moduleName
105+ RequiredVersion = $version
106+ Repository = 'PSGallery'
107+ ErrorAction = 'SilentlyContinue'
40108 }
41- Else{
42- $version = ("{0}-{1}" -f $manifest.version,$prerelease)
109+
110+ if ($prerelease) {
111+ $findParameters.AllowPrerelease = $true
43112 }
44- $modulePath = Join-Path -Path $env:RUNNER_TEMP -ChildPath $moduleName
45- $createModulePath = New-Item -Path $modulePath -ItemType Directory -Force
46- If ($createModulePath) {
47- Write-Output "SUCCESS: Creating staging path '$modulePath'."
48- } Else {
49- Write-Error "FAILED: Creating staging path '$modulePath'."
113+
114+ $existingModule = Find-Module @findParameters
115+
116+ if ($existingModule) {
117+ Write-Host "$moduleName $version is already published."
118+ exit 0
50119 }
51- Write-Output "INFO: Setting location to the GitHub workspace at '$env:GITHUB_WORKSPACE'."
52- Set-Location $env:GITHUB_WORKSPACE
53- Write-Output "INFO: Publishing module to the PowerShell Gallery."
54- Get-ChildItem -Force | Where-Object { $_.Name -notin $remove } | Copy-Item -Destination $modulePath -Recurse
55- #Get-ChildItem -Depth 5 -Path $modulePath | Format-Table -AutoSize
56- $moduleManifest_ = Join-Path -Path $modulePath -ChildPath ("{0}.psd1" -f $moduleName)
57- If (Test-Path -Path $moduleManifest_) {
58- #Check if module is already loaded
59- If($prerelease){
60- $module = Find-Module -Name $moduleName -RequiredVersion ("{0}" -f $version) -AllowPrerelease -ErrorAction Ignore
61- }
62- Else{
63- $module = Find-Module -Name $moduleName -RequiredVersion ("{0}" -f $version) -ErrorAction Ignore
64- }
65- If($null -eq $module){
66- Publish-Module -Path $modulePath -NuGetApiKey $env:NUGET_KEY -Force
67- Start-Sleep -Seconds 30
68- If($prerelease){
69- $module = Find-Module -Name $moduleName -RequiredVersion ("{0}" -f $version) -AllowPrerelease
70- }
71- Else{
72- $module = Find-Module -Name $moduleName -RequiredVersion ("{0}" -f $version)
73- }
74- If ($module) {
75- Write-Output "SUCCESS: Publishing module '$moduleName' version '$version' to PowerShell Gallery."
76- }
77- Else {
78- Write-Error "FAILED: Publishing module '$moduleName' version '$version' to PowerShell Gallery."
79- }
80- }
81- Else{
82- Write-Output "SUCCESS: Module '$moduleName' version '$version' is already available in PowerShell Gallery."
83- }
84- } Else {
85- Write-Error "FAILED: Module manifest file not found at path '$moduleManifest_'."
86- }
120+
121+ Publish-Module `
122+ -Path $modulePath `
123+ -Repository PSGallery `
124+ -NuGetApiKey $env:NUGET_KEY `
125+ -Force `
126+ -Verbose
127+
128+ Write-Host "Publish request completed for $moduleName $version."
0 commit comments