-
Notifications
You must be signed in to change notification settings - Fork 1
183 lines (167 loc) · 6.95 KB
/
powershell-release.yml
File metadata and controls
183 lines (167 loc) · 6.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# Reusable workflow. Call from a tag-triggered workflow in a consuming repo:
#
# on:
# push:
# tags: ['v*']
#
# jobs:
# release:
# uses: PowerShellOrg/.github/.github/workflows/powershell-release.yml@main
# with:
# module-name: MyModule
# secrets:
# PSGALLERY_API_KEY: ${{ secrets.PSGALLERY_API_KEY }}
#
# The PSGALLERY_API_KEY secret must be set in the consuming repository.
# See docs/maintainer-onboarding.md for the key naming and issuance process.
#
# Task names are fixed convention: Init, Build, Test, Analyze, Publish
# The Publish task must read $env:PSGALLERY_API_KEY.
# spell-checker:ignore BHPS PSGALLERY pwsh
name: Publish Module
concurrency:
group: publish-${{ github.ref }}
cancel-in-progress: false
permissions:
contents: write
on:
workflow_call:
inputs:
version:
description: "The version to publish. Leave empty to use the version in the module manifest."
required: false
type: string
force:
type: boolean
description: "If true, bypass the PSGallery version existence check. Use when re-triggering a failed publish job (pattern: force=true, create_release=false, publish=true)."
required: false
default: false
dry_run:
type: boolean
description: "If true, skip actual publishing and just validate the workflow logic."
required: false
default: false
create_release:
type: boolean
description: "If false, skip creating the GitHub release and tag."
required: false
default: true
publish:
type: boolean
description: "If false, skip publishing to PowerShell Gallery."
required: false
default: true
secrets:
PS_GALLERY_KEY:
description: "The API key for publishing to the PowerShell Gallery."
required: true
jobs:
check_version:
name: Check Version
timeout-minutes: 15
runs-on: windows-latest
outputs:
version_bumped: ${{ steps.check_if_versions_bumped.outputs.BUMPED }}
module_name: ${{ steps.check_if_versions_bumped.outputs.MODULE_NAME }}
new_version: ${{ steps.check_if_versions_bumped.outputs.NEW_VERSION }}
steps:
- name: Checkout
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Bootstrap
shell: pwsh
run: ./build.ps1 -Task 'Init' -Bootstrap
- name: Check if version is already published
id: check_if_versions_bumped
shell: pwsh
run: |
Import-Module BuildHelpers
Set-BuildEnvironment -Force
[version]$manifestVersion = Get-MetaData -Path $env:BHPSModuleManifest -PropertyName 'ModuleVersion' -ErrorAction 'Stop'
$PSData = Get-MetaData -Path $env:BHPSModuleManifest -PropertyName PrivateData.PSData -ErrorAction 'Stop'
$prereleaseSuffix = $PSData.Prerelease
Write-Host "Manifest Version: $manifestVersion"
Write-Host "Manifest Prerelease: $prereleaseSuffix"
# Override version if specified as input
if (-not [String]::IsNullOrEmpty('${{ inputs.version }}')) {
if ('${{ inputs.version }}' -match '^(?<version>\d+\.\d+\.\d+)(-(?<prerelease>.+))?$') {
$manifestVersion = [version]$matches['version']
$prereleaseSuffix = $matches['prerelease'] # null if no suffix = stable release
Write-Host "Version override: $manifestVersion, Prerelease override: $prereleaseSuffix"
} else {
Write-Warning "Invalid version format: '${{ inputs.version }}'. Expected format: '1.2.3' or '1.2.3-beta'"
}
Update-MetaData -Path $env:BHPSModuleManifest -PropertyName ModuleVersion -Value $manifestVersion -ErrorAction 'Stop'
}
$moduleSplat = @{ Name = $env:BHProjectName }
if (-not [String]::IsNullOrEmpty($prereleaseSuffix)) {
$moduleSplat['AllowPrerelease'] = $true
$moduleSplat['RequiredVersion'] = "$manifestVersion-$prereleaseSuffix"
} else {
$moduleSplat['AllowPrerelease'] = $false
$moduleSplat['RequiredVersion'] = "$manifestVersion"
}
Write-Host "Version to Publish: $($moduleSplat.RequiredVersion)"
try {
$existingModule = Find-Module @moduleSplat -ErrorAction Stop
$bumped = $false
Write-Host "Version $($moduleSplat.RequiredVersion) already exists in PSGallery - skipping publish"
} catch {
if ($_.CategoryInfo.Category -eq 'ObjectNotFound') {
$bumped = $true
Write-Host "Version $($moduleSplat.RequiredVersion) not found in PSGallery - will publish"
} else {
Write-Warning "Failed to check existing version: $($_.Exception.Message)"
$bumped = $false
}
}
Add-Content -LiteralPath $env:GITHUB_OUTPUT -Value "BUMPED=$bumped" -Confirm:$false -Encoding UTF8
Add-Content -LiteralPath $env:GITHUB_OUTPUT -Value "MODULE_NAME=$env:BHProjectName" -Confirm:$false -Encoding UTF8
Add-Content -LiteralPath $env:GITHUB_OUTPUT -Value "NEW_VERSION=$($moduleSplat.RequiredVersion)" -Confirm:$false -Encoding UTF8
create_release:
name: Create GitHub Release
runs-on: ubuntu-latest
needs:
- check_version
if: (needs.check_version.outputs.version_bumped == 'True' || inputs.force) && inputs.create_release
steps:
- uses: actions/checkout@v4
- name: Read and validate changelog (keepachangelog)
id: changelog
uses: mindsers/changelog-reader-action@v2
with:
validation_level: warn
version: ${{ needs.check_version.outputs.new_version }}
path: ./CHANGELOG.md
- name: Create GitHub Release
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ needs.check_version.outputs.new_version }}
name: Release ${{ steps.changelog.outputs.version }}
body: ${{ steps.changelog.outputs.changes }}
prerelease: ${{ steps.changelog.outputs.status == 'prereleased' }}
draft: ${{ steps.changelog.outputs.status == 'unreleased' }}
publish:
name: Publish Module
runs-on: windows-latest
needs:
- check_version
if: (needs.check_version.outputs.version_bumped == 'True' || inputs.force) && inputs.publish
steps:
- uses: actions/checkout@v4
- name: Publish to PSGallery
shell: pwsh
env:
PSGALLERY_API_KEY: ${{ secrets.PS_GALLERY_KEY }}
PACKAGE: ${{ needs.check_version.outputs.module_name }}
VERSION: ${{ needs.check_version.outputs.new_version }}
DRY_RUN: ${{ inputs.dry_run || false }}
run: |
if ($env:DRY_RUN -eq 'true') {
Write-Host "DRY RUN: Would publish module to PSGallery"
Write-Host "Module: $env:PACKAGE"
Write-Host "Version: $env:VERSION"
exit 0
}
./build.ps1 -Task 'Publish' -Bootstrap