813f76138c
Property-path navigation returns an empty string when the <location>
wrapper is absent from the published web.config, causing AppendChild
to fail. SelectSingleNode("//aspNetCore") works regardless of structure.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
119 lines
4.3 KiB
Plaintext
119 lines
4.3 KiB
Plaintext
pipeline {
|
|
agent { label 'appdev' }
|
|
|
|
environment {
|
|
DOTNET_CLI_HOME = "C:\\Windows\\Temp"
|
|
PUBLISH_DIR = "C:\\jenkins\\publish\\powdercoating"
|
|
DEPLOY_DIR = "C:\\inetpub\\wwwroot"
|
|
APP_POOL = "PCC"
|
|
WEB_PROJECT = "src\\PowderCoating.Web\\PowderCoating.Web.csproj"
|
|
INFRA_PROJECT = "src\\PowderCoating.Infrastructure\\PowderCoating.Infrastructure.csproj"
|
|
CONNECTION_STRING = "Server=.\\SQLEXPRESS;Database=PowderCoatingDb;Trusted_Connection=true;MultipleActiveResultSets=true;TrustServerCertificate=true"
|
|
}
|
|
|
|
stages {
|
|
|
|
stage('Checkout') {
|
|
steps {
|
|
checkout scm
|
|
}
|
|
}
|
|
|
|
stage('Restore') {
|
|
steps {
|
|
bat 'dotnet restore'
|
|
}
|
|
}
|
|
|
|
stage('Build') {
|
|
steps {
|
|
bat 'dotnet build --no-restore -c Release'
|
|
}
|
|
}
|
|
|
|
stage('Test') {
|
|
steps {
|
|
bat 'dotnet test --no-build -c Release --logger "trx;LogFileName=results.trx" --results-directory TestResults'
|
|
}
|
|
post {
|
|
always {
|
|
junit testResults: 'TestResults/*.trx', allowEmptyResults: true
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('Publish') {
|
|
steps {
|
|
bat "dotnet publish %WEB_PROJECT% -c Release --no-build -o \"%PUBLISH_DIR%\""
|
|
}
|
|
}
|
|
|
|
stage('Migrate Database') {
|
|
steps {
|
|
bat """
|
|
dotnet tool restore
|
|
dotnet ef database update --project %INFRA_PROJECT% --startup-project %WEB_PROJECT% --context ApplicationDbContext --connection "%CONNECTION_STRING%"
|
|
"""
|
|
}
|
|
}
|
|
|
|
stage('Deploy') {
|
|
steps {
|
|
// Stop app pool, copy files, restart app pool
|
|
bat """
|
|
C:\\Windows\\System32\\inetsrv\\appcmd stop apppool /apppool.name:%APP_POOL%
|
|
xcopy /E /Y /I "%PUBLISH_DIR%\\*" "%DEPLOY_DIR%\\"
|
|
"""
|
|
// Patch web.config to set Development environment — must survive every deploy
|
|
powershell '''
|
|
$path = "C:\\inetpub\\wwwroot\\web.config"
|
|
$xml = [xml](Get-Content $path)
|
|
|
|
# Use XPath so the structure (with or without <location> wrapper) doesn't matter
|
|
$aspNetCore = $xml.SelectSingleNode("//aspNetCore")
|
|
if ($null -eq $aspNetCore) {
|
|
Write-Error "Could not find aspNetCore element in web.config"
|
|
exit 1
|
|
}
|
|
|
|
# Ensure environmentVariables element exists
|
|
$envVarsNode = $aspNetCore.SelectSingleNode("environmentVariables")
|
|
if ($null -eq $envVarsNode) {
|
|
$envVarsNode = $xml.CreateElement("environmentVariables")
|
|
$aspNetCore.AppendChild($envVarsNode) | Out-Null
|
|
}
|
|
|
|
# Remove existing ASPNETCORE_ENVIRONMENT entry if present
|
|
$existing = $envVarsNode.SelectSingleNode("environmentVariable[@name='ASPNETCORE_ENVIRONMENT']")
|
|
if ($existing) { $envVarsNode.RemoveChild($existing) | Out-Null }
|
|
|
|
# Add fresh entry
|
|
$envVar = $xml.CreateElement("environmentVariable")
|
|
$envVar.SetAttribute("name", "ASPNETCORE_ENVIRONMENT")
|
|
$envVar.SetAttribute("value", "Development")
|
|
$envVarsNode.AppendChild($envVar) | Out-Null
|
|
|
|
$xml.Save($path)
|
|
Write-Host "web.config patched: ASPNETCORE_ENVIRONMENT=Development"
|
|
'''
|
|
bat 'C:\\Windows\\System32\\inetsrv\\appcmd start apppool /apppool.name:%APP_POOL%'
|
|
}
|
|
post {
|
|
failure {
|
|
// Make sure app pool is restarted even if copy fails
|
|
bat 'C:\\Windows\\System32\\inetsrv\\appcmd start apppool /apppool.name:%APP_POOL%'
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
post {
|
|
success {
|
|
echo 'Dev deployment completed successfully.'
|
|
}
|
|
failure {
|
|
echo 'Pipeline failed — check logs above.'
|
|
}
|
|
}
|
|
}
|