|
93 | 93 | throw 'The PSGALLERY_API_KEY repository secret is not set. Add it under Settings -> Secrets and variables -> Actions.' |
94 | 94 | } |
95 | 95 | ./Build/Publish-Module.ps1 -Confirm:$false |
| 96 | +
|
| 97 | + github-release: |
| 98 | + name: 'GitHub release' |
| 99 | + needs: publish |
| 100 | + # Without this job a tag published to the Gallery but showed no GitHub release, so the |
| 101 | + # repository page kept naming an older hand-made release as Latest. The notes are the |
| 102 | + # version's CHANGELOG section. They are built in every run, a dry run included, so a missing |
| 103 | + # section fails before a tag is pushed rather than after the upload. |
| 104 | + runs-on: ubuntu-latest |
| 105 | + # Creating the release is the only write this workflow makes to the repository |
| 106 | + permissions: |
| 107 | + contents: write |
| 108 | + |
| 109 | + steps: |
| 110 | + - name: Checkout |
| 111 | + uses: actions/checkout@v7 |
| 112 | + |
| 113 | + - name: Build the release notes from CHANGELOG.md |
| 114 | + shell: pwsh |
| 115 | + run: | |
| 116 | + $ErrorActionPreference = 'Stop' |
| 117 | + $manifest = @(Get-ChildItem -Path . -Filter *.psd1 -File) |
| 118 | + if ($manifest.Count -ne 1) { |
| 119 | + throw "Expected one module manifest at the repository root, found $($manifest.Count)." |
| 120 | + } |
| 121 | + $module = $manifest[0].BaseName |
| 122 | + $version = (Import-PowerShellDataFile -Path $manifest[0].FullName).ModuleVersion |
| 123 | +
|
| 124 | + # The section runs from the version's heading to the next '## [' heading |
| 125 | + $lines = @(Get-Content -Path ./CHANGELOG.md) |
| 126 | + $heading = '^' + [regex]::Escape("## [$version]") |
| 127 | + $start = -1 |
| 128 | + for ($i = 0; $i -lt $lines.Count; $i++) { |
| 129 | + if ($lines[$i] -match $heading) { $start = $i; break } |
| 130 | + } |
| 131 | + if ($start -lt 0) { |
| 132 | + throw "CHANGELOG.md has no '## [$version]' section to use as the release notes." |
| 133 | + } |
| 134 | + $end = $lines.Count |
| 135 | + for ($i = $start + 1; $i -lt $lines.Count; $i++) { |
| 136 | + if ($lines[$i] -match '^## \[') { $end = $i; break } |
| 137 | + } |
| 138 | + $section = if ($end -gt $start + 1) { ($lines[($start + 1)..($end - 1)] -join "`n").Trim() } |
| 139 | + if (-not $section) { |
| 140 | + throw "The '## [$version]' section of CHANGELOG.md is empty." |
| 141 | + } |
| 142 | +
|
| 143 | + $fence = '```' |
| 144 | + $history = "$env:GITHUB_SERVER_URL/$env:GITHUB_REPOSITORY/blob/v$version/CHANGELOG.md" |
| 145 | + $notes = @("${fence}powershell", "Update-PSResource $module", $fence, '', $section, '', |
| 146 | + "Full history: [CHANGELOG.md]($history)") -join "`n" |
| 147 | + Set-Content -Path ./release-notes.md -Value $notes -Encoding utf8 |
| 148 | + "RELEASE_TAG=v$version" | Add-Content -Path $env:GITHUB_ENV |
| 149 | + "RELEASE_TITLE=$module $version" | Add-Content -Path $env:GITHUB_ENV |
| 150 | + Write-Host "Release notes for $module $version ($($notes.Length) characters):" |
| 151 | + Write-Host $notes |
| 152 | +
|
| 153 | + - name: Create the GitHub release |
| 154 | + if: ${{ startsWith(github.ref, 'refs/tags/') && !inputs.dryRun }} |
| 155 | + shell: pwsh |
| 156 | + env: |
| 157 | + GH_TOKEN: ${{ github.token }} |
| 158 | + run: | |
| 159 | + $ErrorActionPreference = 'Stop' |
| 160 | + # A release made by hand before this job ran is kept as written |
| 161 | + gh release view $env:RELEASE_TAG *> $null |
| 162 | + if ($LASTEXITCODE -eq 0) { |
| 163 | + Write-Host "A release for $env:RELEASE_TAG already exists; leaving it unchanged." |
| 164 | + exit 0 |
| 165 | + } |
| 166 | + # --verify-tag refuses to create a tag; GitHub marks the release Latest by version |
| 167 | + gh release create $env:RELEASE_TAG --title $env:RELEASE_TITLE --notes-file ./release-notes.md --verify-tag |
| 168 | + if ($LASTEXITCODE -ne 0) { |
| 169 | + throw "gh release create failed with exit code $LASTEXITCODE." |
| 170 | + } |
0 commit comments