private/templates/template.deploy.ps1
<# .SYNOPSIS Deploy the Module to PSGallery .DESCRIPTION This script will deploy the module to the PSGallery .NOTES You will need to create a NuGet API Key for the PSGallery at https://www.powershellgallery.com/account/apikeys .LINK https://raw.githubusercontent.com/rulasg/DemoPsModule/main/deploy.ps1 .EXAMPLE # Deploy the module to the PSGallery without prompting > Deploy.ps1 -Force -NuGetApiKey "<API Key>"" .EXAMPLE # Deploy the module to the PSGallery using PAT on enviroment variable > $env:NUGETAPIKEY = <API Key> > ./deploy.ps1 #> [CmdletBinding( SupportsShouldProcess, ConfirmImpact='High' )] param( # The NuGet API Key for the PSGallery [Parameter(Mandatory=$false)] [string]$NuGetApiKey, # Force the deploy without prompting for confirmation [Parameter(Mandatory=$false)] [switch]$Force, # Force deploying package to the gallery. Equivalente to Import-Module -Force [Parameter(Mandatory=$false)] [switch]$ForceDeploy ) # check that $NuggetApiKey is null or whitespace # If it is use environment variable $env:NugetApiKey if ( [string]::IsNullOrWhiteSpace($NuGetApiKey) ) { if ( [string]::IsNullOrWhiteSpace($env:NUGETAPIKEY) ) { Write-Error -Message '$Env:NUGETAPIKEY is not set. Try running `$Env:NUGETAPIKEY = (Find-DocsFile nugetapikey | rsk | Get-SecretData).Get()`' return } $NuGetApiKey = $env:NUGETAPIKEY } # look for psd1 file on the same folder as this script $psdPath = Get-ChildItem -Path $PSScriptRoot -Filter *.psd1 # check if $psd is set if ( $null -eq $psdPath ) { Write-Error -Message 'No psd1 file found' return } # check if $psd is a single file if ( $psdPath.Count -gt 1 ) { Write-Error -Message 'More than one psd1 file found' return } # Display Module Information $psd1 = Import-PowerShellDataFile -Path $psdPath $psd1 $psd1.PrivateData.PSData # Confirm if not forced if ($Force -and -not $Confirm){ $ConfirmPreference = 'None' } # Deploy the module with ShouldProcess (-whatif, -confirm) if ($PSCmdlet.ShouldProcess($psdPath, "Deploy-Module")) { $message ="Deploying {0} {1} {2} to PSGallery ..." -f $($psdPath.Name), $($psd1.ModuleVersion), $($psd1.PrivateData.pSData.Prerelease) # show an empty line Write-Information -InformationAction Continue -Message "" Write-Information -InformationAction Continue -Message $message Deploy-Module -Name $psdPath -NuGetApiKey $NuGetApiKey -Force:$ForceDeploy } |