-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPublish-ToGitHubPackages.ps1
More file actions
69 lines (58 loc) · 2.09 KB
/
Copy pathPublish-ToGitHubPackages.ps1
File metadata and controls
69 lines (58 loc) · 2.09 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
[CmdletBinding()]
param(
[string]$WorkingDirectory = $PSScriptRoot
)
$ErrorActionPreference = 'Stop'
if ($WorkingDirectory) {
Set-Location -Path $WorkingDirectory
}
$moduleName = 'LibreDevOpsHelpers'
$modulePath = Join-Path '.' $moduleName
$psd1Path = Join-Path $modulePath "$moduleName.psd1"
# GitHub Packages settings
$githubOwner = $env:GITHUB_REPOSITORY_OWNER
$githubToken = $env:GITHUB_TOKEN
$repoName = 'GitHubPackages'
$githubUri = "https://nuget.pkg.github.com/$githubOwner/index.json"
if (-not $githubToken) {
throw 'No GitHub token found. Set GITHUB_TOKEN.'
}
# Fail early if the manifest is invalid rather than during upload.
Write-Host "Validating manifest: $psd1Path"
$version = (Test-ModuleManifest -Path $psd1Path).Version.ToString()
# Register GitHub repository (if not already)
if (-not (Get-PSResourceRepository -Name $repoName -ErrorAction SilentlyContinue)) {
Register-PSResourceRepository -Name $repoName -Uri $githubUri
}
try {
Write-Host 'Publishing to GitHub Packages...'
$publishSplat = @{
Path = $psd1Path
Repository = $repoName
ApiKey = $githubToken
SkipDependenciesCheck = $true
}
$manifestData = Import-PowerShellDataFile -Path $psd1Path
if ($manifestData.RequiredModules) {
$publishSplat.SkipModuleManifestValidate = $true
}
try {
Publish-PSResource @publishSplat
Write-Host "Done publishing $moduleName $version to GitHub Packages."
}
catch {
# Idempotency: the feed returns a conflict 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 the pipeline.
if ($_.Exception.Message -match '409|already exists|conflict') {
Write-Host "$moduleName $version is already published to GitHub Packages; nothing to do."
}
else {
throw
}
}
}
finally {
Write-Host 'Unregistering GitHubPackages repository...'
Unregister-PSResourceRepository -Name $repoName -ErrorAction SilentlyContinue
}