Skip to content

Commit 942b028

Browse files
authored
Merge pull request #54 from fadwen/ci/github-release
ci(release): create the GitHub release after the Gallery upload
2 parents ce7ee6d + 20b57dc commit 942b028

2 files changed

Lines changed: 79 additions & 1 deletion

File tree

‎.github/workflows/release.yml‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,78 @@ jobs:
9393
throw 'The PSGALLERY_API_KEY repository secret is not set. Add it under Settings -> Secrets and variables -> Actions.'
9494
}
9595
./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+
}

‎CLAUDE.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ the publish itself is gated by `ShouldProcess`.
145145

146146
Bump `ModuleVersion`, update `CHANGELOG.md` and the manifest's `ReleaseNotes`, then
147147
`git tag v<ModuleVersion> && git push origin v<ModuleVersion>`. `.github/workflows/release.yml`
148-
re-runs every gate, rejects a tag that disagrees with the manifest, stages, verifies and publishes.
148+
re-runs every gate, rejects a tag that disagrees with the manifest, stages, verifies and publishes,
149+
then creates the tag's GitHub release from that version's `## [<version>]` CHANGELOG section. That
150+
section must exist and must not be empty, or the job fails; a dry run checks it too. A release that
151+
already exists for the tag is left as written.
149152
The API key is the `PSGALLERY_API_KEY` repository secret, passed as an environment variable.
150153
Locally the script resolves `-ApiKey`, then `$env:PSGALLERY_API_KEY`, then a SecretManagement
151154
secret named `PSGallery-ApiKey`.

0 commit comments

Comments
 (0)