fbpx

Multi Project Build Scripts for Jenkins

Write-Host "This script is for building a single project (.csproj) within a Solution (.sln)"
Write-Host "and is a .NET Application using this specific build script"

# 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 = $false
Write-Host "NUGET PACK             : $nugetPack"
$nugetDeploy = $false
Write-Host "NUGET DEPLOY           : $nugetDeploy"


# Target framework (only utilized for display)
$framework = "net8.0"
Write-Host "TARGET FRAMEWORK       : $framework"

# 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\$projectName.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 $projectDir

Write-Host "DOTNET BUILD: dotnet clean $projectDir --configuration Release --verbosity quiet"
dotnet clean $projectDir --configuration "Release" --verbosity quiet

# Change to the $projectDir (ensure that we are in the project directory)
cd $projectDir
Write-Host "CURRENT DIRECTORY           : $projectDir"

# Run dotnet build
Write-Host "DOTNET BUILD: dotnet build $csprojectPath --configuration Release --verbosity quiet -p:PackageVersion=$version"
dotnet build $csprojectPath --configuration "Release" --verbosity quiet -p:PackageVersion=$version

# Run dotnet publish
Write-Host "DOTNET BUILD: dotnet publish $projectDir --configuration Release --verbosity quiet -p:PackageVersion=$version --framework $framework --output $nugetPublishDir"
dotnet publish $projectDir --configuration "Release" --verbosity quiet -p:PackageVersion=$version --framework "$framework" --output $nugetPublishDir

# Change to the $nugetPublishDir (ensure that we are in the publish directory)
cd $nugetPublishDir
Write-Host "CURRENT DIRECTORY           : $nugetPublishDir"

# NuGet operations
Write-Host "DOTNET BUILD: NUGET spec $projectName"
& $nugetEXE spec $projectName

Write-Host "DOTNET BUILD: Nuget PACK $projectName.nuspec -OutputDirectory $nugetPackageDir -Version $version"
& $nugetEXE pack "$projectName.nuspec" -OutputDirectory $nugetPackageDir -Version $version

Write-Host "DOTNET BUILD: Completed Tasks"