-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublish.ps1
More file actions
166 lines (149 loc) · 6.83 KB
/
Copy pathpublish.ps1
File metadata and controls
166 lines (149 loc) · 6.83 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
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSUseDeclaredVarsMoreThanAssignments', 'apiKey',
Justification = 'Variable is used in script blocks.'
)]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSUseDeclaredVarsMoreThanAssignments', 'prNumber',
Justification = 'Variable is used in script blocks.'
)]
[CmdletBinding()]
param()
$PSStyle.OutputRendering = 'Ansi'
Import-Module -Name 'PSModule' -Force
#region Load inputs
LogGroup 'Load inputs' {
$env:GITHUB_REPOSITORY_NAME = $env:GITHUB_REPOSITORY -replace '.+/'
$name = if ([string]::IsNullOrEmpty($env:PSMODULE_PUBLISH_PSMODULE_INPUT_Name)) {
$env:GITHUB_REPOSITORY_NAME
} else {
$env:PSMODULE_PUBLISH_PSMODULE_INPUT_Name
}
# Normalize to an absolute path anchored at the workspace root so that
# the resolved location agrees with where actions/download-artifact writes
# the artifact (workspace-root-relative), regardless of WorkingDirectory.
$modulePathInput = $env:PSMODULE_PUBLISH_PSMODULE_INPUT_ModulePath
$modulePathCandidate = if ([System.IO.Path]::IsPathRooted($modulePathInput)) {
Join-Path -Path $modulePathInput -ChildPath $name
} else {
Join-Path -Path $env:GITHUB_WORKSPACE -ChildPath $modulePathInput -AdditionalChildPath $name
}
if (-not (Test-Path -Path $modulePathCandidate -PathType Container)) {
Write-Error ("Module directory not found at [$modulePathCandidate]. " +
'Ensure the artifact contains a <ModulePath>/<Name>/ subdirectory layout.')
exit 1
}
$modulePath = Resolve-Path -Path $modulePathCandidate | Select-Object -ExpandProperty Path
$apiKey = $env:PSMODULE_PUBLISH_PSMODULE_INPUT_APIKey
$whatIf = $env:PSMODULE_PUBLISH_PSMODULE_INPUT_WhatIf -eq 'true'
Write-Host "Module name: [$name]"
Write-Host "Module path: [$modulePath]"
Write-Host "WhatIf: [$whatIf]"
}
#endregion Load inputs
#region Load PR information
LogGroup 'Load PR information' {
$githubEventJson = Get-Content -Raw $env:GITHUB_EVENT_PATH
$githubEvent = $githubEventJson | ConvertFrom-Json
$pull_request = $githubEvent.pull_request
if (-not $pull_request) {
throw 'GitHub event does not contain pull_request data. This script must be run from a pull_request event.'
}
$prNumber = $pull_request.number
}
#endregion Load PR information
#region Resolve version from manifest
# The manifest was stamped with the final version during Build-PSModule. This step is read-only
# to preserve artifact integrity (the tested artifact is identical to the published artifact).
LogGroup 'Resolve version from manifest' {
Add-PSModulePath -Path (Split-Path -Path $modulePath -Parent)
$manifestFilePath = Join-Path -Path $modulePath -ChildPath "$name.psd1"
Write-Host "Module manifest file path: [$manifestFilePath]"
if (-not (Test-Path -Path $manifestFilePath)) {
Write-Error "Module manifest file not found at [$manifestFilePath]"
exit 1
}
Show-FileContent -Path $manifestFilePath
$manifest = Test-ModuleManifest -Path $manifestFilePath -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
if ($manifest) {
Write-Host "Manifest validated: [$($manifest.Name)] v[$($manifest.Version)]"
} else {
Write-Host '::warning::Test-ModuleManifest returned warnings (e.g. unresolved RequiredModules). Continuing with data-file validation.'
}
try {
$manifestData = Import-PowerShellDataFile -Path $manifestFilePath
} catch {
Write-Error "Failed to import manifest data file [$manifestFilePath]: $($_.Exception.Message)"
exit 1
}
$moduleVersion = $manifestData.ModuleVersion
if (-not ($moduleVersion -match '^\d+\.\d+\.\d+$')) {
Write-Error ("ModuleVersion [$moduleVersion] must be in Major.Minor.Patch format. " +
'Ensure Build-PSModule has stamped the artifact with a final version.')
exit 1
}
if ($moduleVersion -eq '999.0.0') {
Write-Error ('ModuleVersion is the placeholder [999.0.0]. ' +
'The artifact was not stamped with a real version by the build step.')
exit 1
}
$prerelease = $manifestData.PrivateData.PSData.Prerelease
if ([string]::IsNullOrWhiteSpace($prerelease)) {
$prerelease = ''
$createPrerelease = $false
} else {
if ($prerelease -notmatch '^[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*$') {
Write-Error ("Prerelease label [$prerelease] is not a valid SemVer prerelease identifier. " +
'It must contain only alphanumerics, hyphens, and dots as separators.')
exit 1
}
$createPrerelease = $true
}
$releaseTag = if ($createPrerelease) { "$moduleVersion-$prerelease" } else { $moduleVersion }
[PSCustomObject]@{
ModuleVersion = $moduleVersion
Prerelease = $prerelease
CreatePrerelease = $createPrerelease
ReleaseTag = $releaseTag
PRNumber = $prNumber
} | Format-List | Out-String
# Expose release tag to subsequent steps so cleanup can exclude the just-published tag.
"PSMODULE_PUBLISH_PSMODULE_CONTEXT_ReleaseTag=$releaseTag" | Out-File -Path $env:GITHUB_ENV -Append -Encoding utf8NoBOM
}
#endregion Resolve version from manifest
#region Install module dependencies
LogGroup 'Install module dependencies' {
Resolve-PSModuleDependency -ManifestFilePath $manifestFilePath
}
#endregion Install module dependencies
#region Publish to PSGallery
LogGroup 'Publish to PSGallery' {
$releaseType = if ($createPrerelease) { 'New prerelease' } else { 'New release' }
$publishPSVersion = if ($createPrerelease) { "$moduleVersion-$prerelease" } else { $moduleVersion }
$psGalleryReleaseLink = "https://www.powershellgallery.com/packages/$name/$publishPSVersion"
Write-Host 'Publish module to PowerShell Gallery using API key from environment.'
if ($whatIf) {
Write-Host "Publish-PSResource -Path $modulePath -Repository PSGallery -ApiKey ***"
} else {
try {
Publish-PSResource -Path $modulePath -Repository PSGallery -ApiKey $apiKey
} catch {
Write-Error $_.Exception.Message
exit 1
}
}
if ($whatIf) {
Write-Host (
"gh pr comment $prNumber -b " +
"'✅ $releaseType`: PowerShell Gallery - [$name $publishPSVersion]($psGalleryReleaseLink)'"
)
} else {
Write-Host "::notice title=✅ $releaseType`: PowerShell Gallery - $name $publishPSVersion::$psGalleryReleaseLink"
gh pr comment $prNumber -b "✅ $releaseType`: PowerShell Gallery - [$name $publishPSVersion]($psGalleryReleaseLink)"
if ($LASTEXITCODE -ne 0) {
Write-Error 'Failed to comment on the pull request.'
exit $LASTEXITCODE
}
}
}
#endregion Publish to PSGallery
Write-Host "Gallery publishing complete. Version: [$releaseTag]"