Skip to content

Commit f638e7d

Browse files
committed
Add Publish task
1 parent 66fd1c8 commit f638e7d

5 files changed

Lines changed: 101 additions & 1 deletion

File tree

PowerShellBuild/PowerShellBuild.psd1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@{
22
RootModule = 'PowerShellBuild.psm1'
3-
ModuleVersion = '0.1.1'
3+
ModuleVersion = '0.2.0'
44
GUID = '15431eb8-be2d-4154-b8ad-4cb68a488e3d'
55
Author = 'Brandon Olin'
66
CompanyName = 'Community'
@@ -15,6 +15,7 @@
1515
'Build-PSBuildUpdatableHelp'
1616
'Clear-PSBuildOutputFolder'
1717
'Initialize-PSBuild'
18+
'Publish-PSBuildModule'
1819
'Test-PSBuildPester'
1920
'Test-PSBuildScriptAnalysis'
2021
)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
function Publish-PSBuildModule {
2+
<#
3+
.SYNOPSIS
4+
Publishes a module to the defined PowerShell repository.
5+
.DESCRIPTION
6+
Publishes a module to the defined PowerShell repository.
7+
.PARAMETER Path
8+
The path to the module to publish.
9+
.PARAMETER Version
10+
The version of the module to publish.
11+
.PARAMETER Repository
12+
The PowerShell repository name to publish to.
13+
.PARAMETER ApiKey
14+
The API key to use to authenticate to the PowerShell repository with.
15+
.PARAMETER Credential
16+
The credential to use to authenticate to the PowerShell repository with.
17+
.EXAMPLE
18+
PS> Publish-PSBuildModule -Path .\Output\0.1.0\MyModule -Version 0.1.0 -Repository PSGallery -ApiKey 12345
19+
20+
Publish version 0.1.0 of the module at path .\Output\0.1.0\MyModule to the PSGallery repository using an API key.
21+
.EXAMPLE
22+
PS> Publish-PSBuildModule -Path .\Output\0.1.0\MyModule -Version 0.1.0 -Repository PSGallery -Credential $myCred
23+
24+
Publish version 0.1.0 of the module at path .\Output\0.1.0\MyModule to the PSGallery repository using a PowerShell credential.
25+
#>
26+
[cmdletbinding(DefaultParameterSetName = 'ApiKey')]
27+
param(
28+
[parameter(Mandatory)]
29+
[ValidateScript({
30+
if (-not (Test-Path -Path $_ )) {
31+
throw 'Folder does not exist'
32+
}
33+
if (-not (Test-Path -Path $_ -PathType Container)) {
34+
throw 'The Path argument must be a folder. File paths are not allowed.'
35+
}
36+
$true
37+
})]
38+
[System.IO.FileInfo]$Path,
39+
40+
[parameter(Mandatory)]
41+
[string]$Version,
42+
43+
[parameter(Mandatory)]
44+
[string]$Repository,
45+
46+
[parameter(Mandatory, ParameterSetName = 'ApiKey')]
47+
[string]$ApiKey,
48+
49+
[parameter(Mandatory, ParameterSetName = 'Credential')]
50+
[pscredential]$Credential
51+
)
52+
53+
Write-Verbose "Publishing version [$Version] to repository [$Repository]..."
54+
55+
$publishParams = @{
56+
Path = $Path
57+
Repository = $Repository
58+
WhatIf = $true
59+
}
60+
switch ($PSCmdlet.ParameterSetName) {
61+
'Credential' { $publishParams.Credential = $Credential }
62+
'ApiKey' { $publishParams.NuGetApiKey = $ApiKey }
63+
}
64+
Publish-Module @publishParams
65+
}

PowerShellBuild/build.properties.ps1

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,16 @@ $testOutputFormat = 'NUnitXml'
119119
$docsRootDir = Join-Path -Path $projectRoot -ChildPath 'docs'
120120

121121
#endregion
122+
123+
#region Publishing
124+
125+
# PowerShell repository name to publish modules to
126+
$psRepository = 'PSGallery'
127+
128+
# API key to authenticate to PowerShell repository with
129+
$psRepositoryApiKey = $env:PSGALLERY_API_KEY
130+
131+
# Credential to authenticate to PowerShell repository with
132+
$psRepositoryCredential = $null
133+
134+
#endregion

PowerShellBuild/psakeFile.ps1

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,23 @@ task GenerateUpdatableHelp -depends BuildHelp -requiredVariables $genUpdatableHe
153153
Build-PSBuildUpdatableHelp -DocsPath $docsRootDir -OutputPath $updatableHelpOutDir
154154
}
155155

156+
task Publish -depends Test -requiredVariables psRepository, moduleVersion, moduleOutDir {
157+
Assert -conditionToCheck ($psRepositoryApiKey -or $psRepositoryCredential) -failureMessage "API key or credential not defined to authenticate with $psRepository with."
158+
159+
$publishParams = @{
160+
Path = $moduleOutDir
161+
Version = $moduleVersion
162+
Repository = $psRepository
163+
}
164+
if ($psRepositoryApiKey) {
165+
$publishParams.ApiKey = $psRepositoryApiKey
166+
} else {
167+
$publishParams.Credential = $psRepositoryCredential
168+
}
169+
170+
Publish-PSBuildModule @publishParams
171+
} -description 'Publish module to the defined PowerShell repository'
172+
156173
task ? -description 'Lists the available tasks' {
157174
'Available tasks:'
158175
$psake.context.Peek().Tasks.Keys | Sort-Object

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ These primary tasks are the main tasks you'll typically call as part of PowerShe
5050
| Pester | Build | Run Pester tests
5151
| Test | Analyze, Pester | Run combined tests
5252
| BuildHelp | Build, GenerateMarkdown, GenerateMAML | Build all help files
53+
| Publish | Test | Publish module to defined PowerShell repository
5354

5455
### Secondary Tasks
5556

@@ -92,6 +93,9 @@ You can override these in either psake or Invoke-Build to match your environment
9293
| $testOutputFile | $null | Output file path Pester will save test results to
9394
| $testOutputFormat | NUnitXml | Test output format to use when saving Pester test results
9495
| $docsRootDir | $projectRoot/docs | Directory PlatyPS markdown documentation will be saved to
96+
| $psRepository | PSGallery | PowerShell repository name to publish
97+
| $psRepositoryApiKey | $env:PSGALLERY_API_KEY | API key to authenticate to PowerShell repository with
98+
| $psRepositoryCredential | $null | Credential to authenticate to PowerShell repository with. Overrides `$psRepositoryApiKey` if defined
9599

96100
## Examples
97101

0 commit comments

Comments
 (0)