Competition: What’s your favorite Redgate tool? Enter now.

Publish a package to a nuget feed

PeterGPeterG Posts: 61 Silver 3
A script for publishing a nuget package to a feed:
# Input:
# Required
# $nugetServer = http://localhost:8080/nuget/
# $nugetPackagePath = C:\foo.nukpg
# 
# Optional
# $nugetAPIKey = 4003d786-cc37-4004-bfdf-c4f3e8ef9b3a

# For more information visit:
# http://docs.nuget.org/docs/reference/command-line-reference#Push_Command

# Requires nuget to be setup on agent



# Check if nuget is available
try {
# An error is thrown if nuget doesn't exist,
# but it's not terminating, which is not the behaviour we want
# so stop on all errors
	$temp = $ErrorActionPreference
	$ErrorActionPreference = "Stop"
	$nuget = Get-Command "nuget.exe"
	$ErrorActionPreference = $temp
} catch {
	throw "NuGet is unavailable"
}

$errors = ""
if ($null -eq $nugetServer) {
	$errors = $errors + ", nugetServer"
}
if ($null -eq $nugetPackagePath) {
	$errors = $errors + ", nugetPackagePath"
}

if (0 -ne $errors.length) {
	# Throw an error and stop deployment from continuing further
	throw ($errors.trim(", ") + " must be set to push a package")
}

nuget push -Source $nugetServer $nugetPackagePath $nugetAPIKey | Out-Default
Peter Gerrard

Software Engineer
Redgate Software
Sign In or Register to comment.