-
Notifications
You must be signed in to change notification settings - Fork 0
153 lines (141 loc) · 5.93 KB
/
PublishModule.yml
File metadata and controls
153 lines (141 loc) · 5.93 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
# 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
isPrerelease:
description: "Is this a prerelease version?"
required: false
default: false
type: boolean
dry_run:
type: boolean
description: "If true, skip actual publishing and just validate the workflow logic."
required: false
default: false
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:
- uses: actions/checkout@v4
- name: Check if version is already published
id: check_if_versions_bumped
shell: pwsh
run: |
Install-Module BuildHelpers
Import-Module BuildHelpers
Set-BuildEnvironment -Force
[version]$githubVersion = Get-MetaData -Path $env:BHPSModuleManifest -PropertyName 'ModuleVersion' -ErrorAction 'Stop'
Write-Host "Current Version: $githubVersion"
# Override version if specified as input
if (-not [String]::IsNullOrEmpty('${{ inputs.version }}')) {
# Split version and prerelease suffix if present
if('${{ inputs.version }}' -match '^(?<version>\d+\.\d+\.\d+)(-(?<prerelease>.+))?$') {
$githubVersion = [version]$matches['version']
if ($matches['prerelease']) {
$prereleaseSuffix = $matches['prerelease']
}
} else {
Write-Warning "Invalid version format: '${{ inputs.version }}'. Expected format: '1.2.3' or '1.2.3-beta'"
}
Write-Host "Module version was specified in workflow: $githubVersion"
Update-MetaData -Path $env:BHPSModuleManifest -PropertyName ModuleVersion -Value $githubVersion -ErrorAction 'Stop'
}
$moduleSplat = @{
Name = $env:BHProjectName
}
# Handle pre-release versions
$isPrerelease = '${{ inputs.isPrerelease }}' -eq 'true'
Write-Host "Prerelease: $isPrerelease"
if ($isPrerelease) {
$PSData = Get-MetaData -Path $env:BHPSModuleManifest -PropertyName PrivateData.PSData -ErrorAction 'Stop'
$moduleSplat['AllowPrerelease'] = $true
$moduleSplat['RequiredVersion'] = "$githubVersion-$($PSData.Prerelease)"
} else {
$moduleSplat['AllowPrerelease'] = $false
$moduleSplat['RequiredVersion'] = $githubVersion
}
Write-Host "Version to Publish: $($moduleSplat.RequiredVersion)"
# Check if exact version exists in PSGallery
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
}
}
# Strip prerelease suffix for changelog lookup
$versionForChangelog = "$($moduleSplat.RequiredVersion)" -replace '-.*$', ''
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
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'
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