-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPublish-ToPSGallery.ps1
More file actions
65 lines (53 loc) · 2.08 KB
/
Copy pathPublish-ToPSGallery.ps1
File metadata and controls
65 lines (53 loc) · 2.08 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
[CmdletBinding()]
param(
[string]$WorkingDirectory = $PSScriptRoot,
[string]$ApiKey
)
$ErrorActionPreference = 'Stop'
if ($WorkingDirectory) {
Set-Location -Path $WorkingDirectory
}
# Define module name and path
$moduleName = 'LibreDevOpsHelpers'
$modulePath = Join-Path '.' $moduleName
$psd1Path = Join-Path $modulePath "$moduleName.psd1"
# Resolve the API key: explicit parameter first, then the local env var, then the
# name used by the CI workflow.
if (-not $ApiKey) {
$ApiKey = $Env:PSGALLERY_TOKEN
}
if (-not $ApiKey) {
$ApiKey = $Env:NUGET_API_KEY
}
if (-not $ApiKey) {
throw 'No API key found. Set PSGALLERY_TOKEN or NUGET_API_KEY, or pass -ApiKey.'
}
# Fail early if the manifest is invalid rather than during upload.
Write-Host "Validating manifest: $psd1Path"
Test-ModuleManifest -Path $psd1Path | Out-Null
# Register PowerShell Gallery as a PSResource repository (if not already)
if (-not (Get-PSResourceRepository -Name 'PSGallery' -ErrorAction SilentlyContinue)) {
Register-PSResourceRepository -Name 'PSGallery' -Uri 'https://www.powershellgallery.com/api/v2' -Trusted
}
# Idempotency: PSGallery returns 409 if the version already exists. Skip cleanly so a
# re-run (for example a push that touches a psd1 without a version bump) does not fail.
$version = (Test-ModuleManifest -Path $psd1Path).Version.ToString()
$existing = Find-PSResource -Name $moduleName -Version "[$version]" -Repository PSGallery -ErrorAction SilentlyContinue
if ($existing) {
Write-Host "$moduleName $version is already published to PSGallery; nothing to do."
return
}
Write-Host 'Publishing to PowerShell Gallery...'
$publishSplat = @{
Path = $psd1Path
Repository = 'PSGallery'
ApiKey = $ApiKey
SkipDependenciesCheck = $true
}
# Handle the RequiredModules edge case
$manifestData = Import-PowerShellDataFile -Path $psd1Path
if ($manifestData.RequiredModules) {
$publishSplat.SkipModuleManifestValidate = $true
}
Publish-PSResource @publishSplat
Write-Host "Done publishing $moduleName $($manifestData.ModuleVersion) to PSGallery."