Eliminating Manual Publishing: Auto-Publish Your Sitecore Project on Docker Up

Hello Everyone. In this blog, we will see one of the pain point while working locally with Docker and Sitecore AI. Developers often experience slowdowns in local environments due to the need for manual publishing. This becomes even more frequent given how Docker setups can sometimes become unstable or require restarting (docker down) because of various unpredictable issues.

When running your project locally using Docker (docker up), you typically:

  • Start containers
  • Wait for CM to be ready
  • Manually trigger publishing

This becomes repetitive, time-consuming, and error-prone. In this blog, we’ll explore how to eliminate manual publishing by enabling auto-publish during Docker startup, along with a real-world use case.

Now let's see, how we can implement the same.

Step 1: Update your project csproject file with this below entry. The RuntimeIdentifier = win element in a .csproj file tells .NET which operating system/runtime your application is targeting.

       
 <RuntimeIdentifier>win</RuntimeIdentifier>      
	   

Step 2: Update your up.ps1 script file to include the logic of triggering .NET publish when up script is executed. Below is the powerShell code which is added for accomplishing the same. This script can be modified according to thier project folder structure. I will explain the logic below what exactly I am doing here.

       
# build any local solution and copy xdt transforms into the CM build context
# adjust the paths below to match your project and transform names
Write-Host "Building custom .NET project and staging transforms" -ForegroundColor Green

# example: build the authoring solution (replace with your own project)
$solutionPath = Join-Path $RepoRoot "authoring\XmCloudAuthoring.sln"
if (Test-Path $solutionPath) {
    #dotnet build $solutionPath -c Release | Out-Null

    # publish the platform project to a temporary folder so we can copy outputs
    $publishFolder = Join-Path $RepoRoot "artifacts\publish"
    Write-Host "published folder: $publishFolder" -ForegroundColor Green
    # ensure the artifacts directory exists
    if (-not (Test-Path $publishFolder)) {
        New-Item -ItemType Directory -Path $publishFolder -Force #| Out-Null
    }
    Remove-Item $publishFolder\* -Recurse -Force -ErrorAction SilentlyContinue
    $platformProjectPath = Join-Path $RepoRoot "authoring\platform\Platform.csproj"
    Write-Host "Platform project path: $platformProjectPath" -ForegroundColor Green
    
    Write-Host "Restoring NuGet packages..." -ForegroundColor Green
    dotnet restore $solutionPath

# Then your existing MSBuild command follows...
    # 1. Locate MSBuild (Standard path for VS 2022)
    $msbuildPath = "C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Current\Bin\MSBuild.exe"
    
    if (Test-Path $msbuildPath) {
    Write-Host "Publishing Platform using MSBuild..." -ForegroundColor Green
    
    # 2. Run MSBuild with the specific Sitecore/Web settings
    & $msbuildPath $platformProjectPath `
    /p:Configuration=Release `
    /p:DeployOnBuild=true `
    /p:PublishUrl=$publishFolder `
    /p:WebPublishMethod=FileSystem `
    /p:DeployTarget=WebPublish `
    /p:RuntimeIdentifier=win `
    /m /v:m
    } else {
    Write-Error "MSBuild not found at $msbuildPath. Please verify your Visual Studio installation."
    }

    #dotnet publish $platformProjectPath -c Release -o $publishFolder #| Out-Null

    # determine host deploy path from .env
    $hostDeployLine = $envContent | Where-Object { $_ -imatch "^LOCAL_DEPLOY_PATH=" }
    if ($hostDeployLine) {
        $hostDeploy = $hostDeployLine.Split('=')[1]
        $dockerDeployPath = Join-Path $RepoRoot "local-containers"
        $deployPathWithOutPlatform = Join-Path $dockerDeployPath $hostDeploy
        Write-Host "deployPathWithOutPlatform: $deployPathWithOutPlatform" -ForegroundColor Green
        $hostPlatform = Join-Path $deployPathWithOutPlatform "platform"
        Write-Host "Copying published output to host deploy platform: $hostPlatform" -ForegroundColor Green
        if (Test-Path $publishFolder) {
            Copy-Item "$publishFolder\*" -Destination $hostPlatform\ -Recurse -Force
        }
    } else {
        Write-Host "[WARN] LOCAL_DEPLOY_PATH not found in .env, skipping publish copy" -ForegroundColor Yellow
    }

} else {
    Write-Host "[WARN] solution not found at $solutionPath, skipping build" -ForegroundColor Yellow
}

# copy any xdt files from the authoring platform project's xdts folder to the docker build custom directory
# Transforms should live under custom\xdts so that the container build can copy them
# into the deploy folder with the correct structure.
$platformXdts = Join-Path $RepoRoot "authoring\platform\xdts"
$customDir = Join-Path $RepoRoot "local-containers\docker\build\cm\custom\xdts"
if (-not (Test-Path $customDir)) {
    New-Item -ItemType Directory -Path $customDir -Force | Out-Null
}
if (Test-Path $platformXdts) {
    # copy recursively preserving the folder layout under xdts
    Get-ChildItem -Path $platformXdts -Recurse -File | ForEach-Object {
        $relative = $_.FullName.Substring($platformXdts.Length) -replace "^\\",""
        $dest = Join-Path $customDir $relative
        $destDir = Split-Path $dest -Parent
        if (-not (Test-Path $destDir)) { New-Item -ItemType Directory -Path $destDir -Force | Out-Null }
        Copy-Item $_.FullName -Destination $dest -Force
        Write-Host "Staged XDT: $($_.FullName) -> $dest" -ForegroundColor Green
    }
} else {
    Write-Host "[INFO] no platform xdts folder found at $platformXdts" -ForegroundColor Cyan
}      
	   

Now let's understand the implementation. I am creating an artifact folder which holds our published dll, conifg, xdts in that folder. This publishing will be done by using .NET MS build and publish manual command. Now once the artifact folder is used for transferring all the needed files and configuration to our Deploy-->Platform folder which is mapped to CM container.

Now whenever you are running up.ps1 script, the backend solution will be rebuild and published along. No need to trigger manual publishing again as it will make CM loading slow again.

Thankyou for reading and Happy learning.

You can check my other blogs too if interested. Blog Website

Comments

Popular posts from this blog

Sitecore XM Cloud Form Integration with Azure Function as Webhook

Sitecore 10.2 Installation using Windows PowerShell

Implementing Custom Link Provider in Sitecore XM Cloud