|
| 1 | +name: Publish PowerShell Module |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + paths: |
| 6 | + - 'vN.AWSSSO/**' |
| 7 | + release: |
| 8 | + types: [released] |
| 9 | + workflow_dispatch: |
| 10 | + inputs: |
| 11 | + GHDeploymentEnv: |
| 12 | + description: 'GH Deployment Environment to use for this workflow run' |
| 13 | + required: true |
| 14 | + default: 'Dev' |
| 15 | + type: choice |
| 16 | + options: |
| 17 | + - Dev |
| 18 | + - bogus_for_failure_testing |
| 19 | + |
| 20 | +defaults: |
| 21 | + run: |
| 22 | + shell: pwsh |
| 23 | + |
| 24 | +jobs: |
| 25 | + publish-module: |
| 26 | + name: 🧾 Publish Module |
| 27 | + runs-on: ubuntu-latest |
| 28 | + ## if the event is a release, then the GHDeploymentEnv is prod, else if workflow_dispatch, then use the input, else dev |
| 29 | + environment: ${{ github.event_name == 'release' && 'prod' || (github.event_name == 'workflow_dispatch' && github.event.inputs.GHDeploymentEnv || 'dev') }} |
| 30 | + steps: |
| 31 | + - name: 🌿 Checkout repository (ref '${{ github.ref_name }}') |
| 32 | + uses: actions/checkout@v5 |
| 33 | + |
| 34 | + - name: Ensure given PS Gallery is registered ('${{ vars.PSGALLERY_URI }}') |
| 35 | + run: | |
| 36 | + if (-not ($oExistingPSResRepoRegistration = Get-PSResourceRepository -Name '${{ vars.PSGALLERY_DISPLAYNAME }}' -ErrorAction:SilentlyContinue)) { |
| 37 | + Write-Verbose -Verbose "PSResource Repository '${{ vars.PSGALLERY_DISPLAYNAME }}' (URL '${{ vars.PSGALLERY_URI }}') is not registered. Registering now" |
| 38 | + $hshParamForRegisterPSRepository = @{ |
| 39 | + Name = '${{ vars.PSGALLERY_DISPLAYNAME }}' |
| 40 | + URI = '${{ vars.PSGALLERY_URI }}' |
| 41 | + ErrorAction = "Stop" |
| 42 | + Verbose = $true |
| 43 | + } |
| 44 | + Register-PSResourceRepository @hshParamForRegisterPSRepository |
| 45 | + } |
| 46 | + else { |
| 47 | + Write-Verbose -Verbose "PSResource Repository '${{ vars.PSGALLERY_DISPLAYNAME }}' is already registered (URL '$($oExistingPSResRepoRegistration.Uri)'). Skipping registration." |
| 48 | + } |
| 49 | +
|
| 50 | + - name: 📦 Publish module to PSGallery |
| 51 | + id: publish_ps_module |
| 52 | + run: | |
| 53 | + Get-ChildItem -Path . -Recurse -Filter *.psd1 | ForEach-Object { |
| 54 | + Write-Verbose -Verbose "Found module manifest: '$($_.FullName)'" |
| 55 | + try { |
| 56 | + Test-ModuleManifest -Path $_.FullName -ErrorAction:Stop -OutVariable oPSModuleInfo |
| 57 | + $hshParamForPublishPSResource = @{ |
| 58 | + ApiKey = '${{ secrets.PSGALLERY_APIKEY }}' |
| 59 | + Repository = '${{ vars.PSGALLERY_DISPLAYNAME }}' |
| 60 | + Path = $_.FullName |
| 61 | + ErrorAction = "Stop" |
| 62 | + Verbose = $true |
| 63 | + } |
| 64 | + Publish-PSResource @hshParamForPublishPSResource |
| 65 | + Add-Content -Path $env:GITHUB_OUTPUT -Encoding utf8 -Value "strModuleInfo_JSON=$($oPSModuleInfo | Select-Object -Property Name, Description, Author, @{n="VersionString"; e={$_.Version.ToString()}} | Convertto-Json -Compress)" |
| 66 | + } |
| 67 | + ## throw the error if any, so future steps know outcome |
| 68 | + catch {throw $_} |
| 69 | + } |
| 70 | +
|
| 71 | + - name: 📝 Write summary of publishing |
| 72 | + if: always() |
| 73 | + run: | |
| 74 | + $strResultOfPublish = switch ('${{ steps.publish_ps_module.outcome }}') { |
| 75 | + "success" {"✅ Succeeded"} |
| 76 | + "failure" {"😡 Failed"} |
| 77 | + "cancelled" {"❌ Canceled"} |
| 78 | + "skipped" {"🦘 Skipped"} |
| 79 | + } |
| 80 | + $oPSModuleInfo_fromJson = @" |
| 81 | + ${{ steps.publish_ps_module.outputs.strModuleInfo_JSON }} |
| 82 | + "@ | ConvertFrom-Json |
| 83 | + Add-Content -Path $env:GITHUB_STEP_SUMMARY -Encoding utf8 -Value (@' |
| 84 | + # Publish Summary |
| 85 | + | What | Value | |
| 86 | + |------|-------| |
| 87 | + Module Name | `{0}` |
| 88 | + Version | `{1}` |
| 89 | + Description | {2} |
| 90 | + Author | {3} |
| 91 | + Gallery URI | ${{ vars.PSGALLERY_URI }} |
| 92 | + Publishing Result | {4} |
| 93 | + '@ -f $oPSModuleInfo_fromJson.Name, $oPSModuleInfo_fromJson.VersionString, $oPSModuleInfo_fromJson.Description, $oPSModuleInfo_fromJson.Author, $strResultOfPublish) |
0 commit comments