Skip to content

Publish

Publish #3

Workflow file for this run

name: Publish
on:
workflow_dispatch:
push:
tags:
- 'v*'
permissions:
contents: write
jobs:
publish:
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
- name: Install Dependencies
shell: pwsh
run: |
if (-not (Get-PackageProvider -Name NuGet -ErrorAction SilentlyContinue)) {
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Scope CurrentUser -Force
}
Set-PSRepository PSGallery -InstallationPolicy Trusted
Install-Module PSScriptAnalyzer -Scope CurrentUser -RequiredVersion 1.22.0 -Force
Install-Module Pester -Scope CurrentUser -RequiredVersion 5.7.1 -Force
Install-Module posh-git -Scope CurrentUser -Force
Install-Module git-aliases -Scope CurrentUser -Force
Install-Module PowerShellGet -Scope CurrentUser -RequiredVersion 2.2.5 -Force
- name: Run Lint and Tests
shell: pwsh
run: |
.\tools\ci.ps1 -CurrentShellOnly
- name: Validate Module Version Against Tag
if: startsWith(github.ref, 'refs/tags/v')
shell: pwsh
run: |
$manifest = Test-ModuleManifest -Path .\git-aliases-extra.psd1 -ErrorAction Stop
$tagVersion = '${{ github.ref_name }}'.TrimStart('v')
if ($manifest.Version.ToString() -ne $tagVersion) {
throw "Manifest version '$($manifest.Version)' does not match tag version '$tagVersion'."
}
- name: Validate and Stage Release Notes
id: module_meta
shell: pwsh
run: |
$manifestPath = Resolve-Path -LiteralPath .\git-aliases-extra.psd1 |
Select-Object -ExpandProperty Path -First 1
$manifest = Test-ModuleManifest -Path $manifestPath -ErrorAction Stop
$moduleVersion = $manifest.Version.ToString()
$releaseNotes = .\tools\get-release-notes.ps1 -Version $moduleVersion
if (-not $releaseNotes) {
throw "Release notes for version '$moduleVersion' are empty."
}
Update-ModuleManifest -Path $manifestPath -ReleaseNotes $releaseNotes
$releaseNotesPath = Join-Path $env:RUNNER_TEMP ("release-notes-{0}.md" -f $moduleVersion)
Set-Content -LiteralPath $releaseNotesPath -Value $releaseNotes -Encoding utf8
"module_version=$moduleVersion" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append
"release_notes_path=$releaseNotesPath" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append
Write-Host "Release notes for version '$moduleVersion' loaded from CHANGELOG.md."
- name: Prepare Publish Layout
id: prepare_publish
shell: pwsh
run: |
$publishPath = .\tools\prepare-publish.ps1 `
-SourcePath . `
-OutputPath (Join-Path $env:RUNNER_TEMP 'git-aliases-extra-publish')
"publish_path=$publishPath" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append
- name: Publish to PSGallery
shell: pwsh
env:
PSGALLERY_API_KEY: ${{ secrets.PSGALLERY_API_KEY }}
run: |
if (-not $env:PSGALLERY_API_KEY) {
throw "PSGALLERY_API_KEY secret is not set."
}
$publishPath = '${{ steps.prepare_publish.outputs.publish_path }}'
$stagedManifestPath = Join-Path $publishPath 'git-aliases-extra.psd1'
$manifest = Test-ModuleManifest -Path $stagedManifestPath -ErrorAction Stop
$moduleName = $manifest.Name
$moduleVersion = $manifest.Version.ToString()
$published = Find-Module -Name $moduleName -Repository PSGallery -ErrorAction SilentlyContinue |
Where-Object { $_.Version.ToString() -eq $moduleVersion }
if ($published) {
Write-Host "Version $moduleVersion is already published. Skipping Publish-Module."
exit 0
}
Publish-Module -Path $publishPath -Repository PSGallery -NuGetApiKey $env:PSGALLERY_API_KEY -ErrorAction Stop
- name: Create or Update GitHub Release
if: startsWith(github.ref, 'refs/tags/v')
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ github.ref_name }}
name: ${{ github.ref_name }}
body_path: ${{ steps.module_meta.outputs.release_notes_path }}
generate_release_notes: false