Here is a powershell build script for creating NUGET packages on a Jenkins Server.
I recently had to rewrite these due to moving Jenkins from one server to the next and went all out using variables defined.
Write-Host "This Script is for Compiling Nuget Packages"
# Print environment details
Write-Host "VERSION : $env:VERSION"
Write-Host "WORK SPACE PATH : $ENV:WORKSPACE"
$version = "$env:VERSION"
# Define solutionName, solutionDir, projectDir, and projectName
$solutionName = $env:SLN_NAME
Write-Host "SOLUTION NAME : $solutionName"
$solutionDir = $ENV:WORKSPACE
Write-Host "SOLUTION DIRECTORY : $solutionDir"
$projectDir = "$ENV:WORKSPACE\$ENV:JOB_NAME"
Write-Host "PROJECT DIRECTORY : $projectDir"
$projectName = $ENV:JOB_NAME
Write-Host "PROJECT NAME : $ENV:JOB_NAME"
# Define flags for nuget operations (not currently utilized properly except for display)
$nugetPack = $true
Write-Host "NUGET PACK : $nugetPack"
$nugetDeploy = $true
Write-Host "NUGET DEPLOY : $nugetDeploy"
# Target framework (only utilized for display)
$framework = "net8.0"
Write-Host "TARGET FRAMEWORK : $framework"
# Change to the $ENV:Workspace (ensure that we are in the workspace directory)
cd $ENV:Workspace
Write-Host "CURRENT DIRECTORY : $ENV:Workspace"
# Define NUGET paths, csproject name and directory, and NUGET Server URL and API Key
$nugetEXE = D:\build_tools\"your nuget location"\nuget.exe
$csprojectPath = "$projectDir\$ENV:JOB_NAME.csproj"
Write-Host "PROJECT PATH : $csprojectPath"
$nugetPackageDir = "$solutionDir\nupkgs"
Write-Host "NUGET PACKAGE DIRECTORY: $nugetPackageDir"
$nugetPublishDir = "$solutionDir\publish"
Write-Host "NUGET PUBLISH DIRECTORY: $nugetPublishDir"
$NugetServer = "https://"your nuget package server"/nuget"
Write-Host "NUGET SERVER URL : $NugetServer"
$NugetServerKey = "your nuget server key"
Write-Host "NUGET SERVER KEY : $NugetServerKey"
# Clean and Create necessary directories
if (Test-Path $nugetPackageDir) {
Write-Host "DOTNET BUILD: Delete Directory $nugetPackageDir"
Remove-Item -Path $nugetPackageDir -Recurse -Force
}
Write-Host "DOTNET BUILD: Create Directory $nugetPackageDir"
New-Item -ItemType Directory -Force -Path $nugetPackageDir
if (Test-Path $nugetPublishDir) {
Write-Host "DOTNET BUILD: Delete Directory $nugetPublishDir"
Remove-Item -Path $nugetPublishDir -Recurse -Force
}
Write-Host "DOTNET BUILD: Create Directory $nugetPublishDir"
New-Item -ItemType Directory -Force -Path $nugetPublishDir
# Define the Solution location
$slnPath = "$solutionDir\$solutionName.sln"
Write-Host "Solution Path : $slnPath"
# Run dotnet restore
Write-Host "DOTNET BUILD: dotnet restore $slnPath"
dotnet restore $slnPath -v q
# Run dotnet build
Write-Host "DOTNET BUILD: dotnet build $csprojectPath -c Release --version-suffix $version --verbosity quiet"
dotnet build $csprojectPath -c Release --version-suffix $version --verbosity quiet
# Change to the $projectDir (ensure that we are in the project directory)
cd $projectDir
Write-Host "CURRENT DIRECTORY : $projectDir"
# Run nuget pack
Write-Host "DOTNET BUILD: dotnet pack -c Release --no-restore -o $nugetPackageDir --version-suffix $version --verbosity quiet"
dotnet pack -c Release --no-restore -o $nugetPackageDir --version-suffix $version --verbosity quiet
# Display completed tasks
Write-Host "DOTNET BUILD: Completed Tasks"
# Define the NuGet Package Name
$NugetPackageName = "$ENV:JOB_NAME.$version.nupkg"
Write-Host "NUGET: Nuget Package Name: $NugetPackageName"
#Change to the $nugetPackageDir (ensure that we are in the NUPKG directory)
cd $nugetPackageDir
Write-Host "CURRENT PATH : $nugetPackageDir"
# Push the package to the NuGet server
Write-Host "NUGET: Push "$NugetPackageName" -k $NugetServerKey -s $NugetServer"
dotnet nuget push "$NugetPackageName" -k $NugetServerKey -s $NugetServer
# Display completed tasks
Write-Host "NUGET: Completed Tasks"