1+ name : Publish Monkey365 to PowerShell Gallery
2+
3+ on :
4+ release :
5+ types : [published]
6+
7+ jobs :
8+ PublishMonkeyToGallery :
9+ runs-on : windows-latest
10+ steps :
11+ - uses : actions/checkout@v4
12+ - name : Publish Monkey365
13+ shell : powershell
14+ env :
15+ NUGET_KEY : ${{ secrets.PSGALLERY }}
16+ 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)
27+ }
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'."
33+ }
34+ $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
40+ }
41+ Else{
42+ $version = ("{0}-{1}" -f $manifest.version,$prerelease)
43+ }
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'."
50+ }
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+ }
0 commit comments