From f27b4e5724b7b46a014fd49bb2535bc2a87609b5 Mon Sep 17 00:00:00 2001 From: Rob Earlam Date: Tue, 28 Feb 2023 17:27:07 +1100 Subject: [PATCH] Simplified local docker setup for MVP site, switched over to .NET6 Hot reload --- docker-compose.override.yml | 3 +- docker/build/rendering/Development.ps1 | 158 ------------------ docker/build/rendering/Dockerfile | 18 +- .../rendering/Properties/launchSettings.json | 19 ++- 4 files changed, 13 insertions(+), 185 deletions(-) delete mode 100644 docker/build/rendering/Development.ps1 diff --git a/docker-compose.override.yml b/docker-compose.override.yml index 3da6020c..e5daeb8b 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -79,7 +79,6 @@ services: restart: ${DOCKER_RESTART} build: context: ./docker/build/rendering - target: ${BUILD_CONFIGURATION} args: DEBUG_BASE_IMAGE: ${NETCORE_BUILD_IMAGE} RELEASE_BASE_IMAGE: ${NETCORE_RELEASE_IMAGE} @@ -87,7 +86,7 @@ services: ENTRYPOINT_ASSEMBLY: Mvp.Project.MvpSite.dll ARTIFACTS_FOLDER: /artifacts/mvp-rendering volumes: - - ${LOCAL_DEPLOY_PATH}\mvp-rendering:C:\deploy + - .\:C:\app environment: ENTRYPOINT_ASSEMBLY: Mvp.Project.MvpSite.dll ASPNETCORE_ENVIRONMENT: "Development" diff --git a/docker/build/rendering/Development.ps1 b/docker/build/rendering/Development.ps1 deleted file mode 100644 index aa9f0b32..00000000 --- a/docker/build/rendering/Development.ps1 +++ /dev/null @@ -1,158 +0,0 @@ -# Homemade dotnet "watch" script, watches the deploy folder for changes to assembly files. -# Stops the running app, syncs the changed files and starts the app up again. No Author: @anderslaub - -[CmdletBinding()] -param( - [ValidateScript( { Test-Path $_ -PathType 'Container' })] - [string]$Path='C:\Deploy', - [ValidateScript( { Test-Path $_ -PathType 'Container' })] - [string]$Destination='C:\App', - [Parameter(Mandatory = $false)] - [int]$SleepMilliseconds = 1000 -) - -$ErrorActionPreference = "STOP" - -function Get-Timestamp { - "$(Get-Date -Format "HH:mm:ss:fff")" -} - -function Stop-DotNetApp { - param( - [Parameter(Mandatory = $true)] - $AppProcessId - ) - Stop-Process -Id $AppProcessId -ErrorAction SilentlyContinue - Write-Host ("$(Get-Timestamp): App process stopped, waiting $($SleepMilliseconds)ms for file lock release..") -ForegroundColor Magenta - Start-Sleep -Milliseconds $SleepMilliseconds # Give the app some time to shutdown and release file locks. -} - -function Start-DotNetApp { - param( - [Parameter(Mandatory = $true)] - $AppJobName - ) - $AppProcess = Start-Process "dotnet" -WorkingDirectory C:\app\ -NoNewWindow -Args $AppJobName -PassThru - Write-Host ("$(Get-Timestamp): App '$($AppJobName)' started in process '$($AppProcess.Id)'..") -ForegroundColor Green - $AppProcess.Id -} - - -function Sync-Delta { - param( - [Parameter(Mandatory = $true)] - $FilePath, - [Parameter(Mandatory = $true)] - $Source, - [Parameter(Mandatory = $true)] - $Destination - ) - - if ($FilePath.StartsWith($Destination)) { - Remove-Item $FilePath -Force -ErrorVariable Exc -ErrorAction SilentlyContinue | Out-Null - if ($Exc) { - $Exc - } - return; - } - - $DestinationPath = $FilePath.Replace($Source, $Destination) - If (-not (Test-Path $DestinationPath)) { - New-Item -ItemType File -Path $DestinationPath -Force | Out-Null - } - - Copy-Item -Path $FilePath -Destination $DestinationPath -Force -ErrorAction SilentlyContinue -ErrorVariable Exc | Out-Null - if ($Exc) { - $Exc - } -} - -function Get-WatchedFiles { - param ( - [Parameter(Mandatory = $true)] - $RootFolder - ) - if (Test-Path $RootFolder\wwwroot\) { - return (Get-ChildItem $RootFolder\*.dll,$RootFolder\*.json) + (Get-ChildItem $RootFolder\wwwroot\*.* -Recurse) - } - Get-ChildItem $RootFolder\*.dll,$RootFolder\*.json -} - -function Sync { - param( - [Parameter(Mandatory = $true)] - $Path, - [Parameter(Mandatory = $true)] - $Destination, - [Parameter(Mandatory = $true)] - $LastRunSourceFiles, - [Parameter(Mandatory = $true)] - $AppProcessId - ) - - $files = Get-WatchedFiles -RootFolder $Path - - $SourceFiles = $files | ForEach-Object { Get-FileHash -Path $_.FullName } - if ($null -eq $SourceFiles -or $SourceFiles.Count -eq 0) { - return $null - } - - $DestinationFiles = Get-WatchedFiles -RootFolder $Destination | ForEach-Object { Get-FileHash -Path $_.FullName } - - if ($null -eq $DestinationFiles) { - $Deltas = $SourceFiles - } - else { - $Deltas = @(Compare-Object -ReferenceObject $SourceFiles -DifferenceObject $DestinationFiles -Property Hash -PassThru) - } - $SourceDeltas = @(Compare-Object -ReferenceObject $SourceFiles.Hash -DifferenceObject $LastRunSourceFiles.Hash -PassThru) - $SourceFilesStable = $SourceDeltas.Count -eq 0 - - if ($Deltas.Count -gt 0 -and $SourceFilesStable) { - Write-Host ("$(Get-Timestamp): Changes detected. Stopping app.") -ForegroundColor Yellow - - Stop-DotNetApp $AppProcessId - $SyncErrors = $Deltas | % { Sync-Delta $_.Path $Path $Destination } - if ($null -ne $SyncErrors -band $SyncErrors.Count -gt 0) { - Write-Host ("$(Get-Timestamp): Errors occurred while syncing:`n`t$(($SyncErrors -join "`n`t"))") -ForegroundColor Red - } - Write-Host ("$(Get-Timestamp): Syncing done. Restarting app.") -ForegroundColor Yellow - - $AppProcessId = Start-DotNetApp $ENV:ENTRYPOINT_ASSEMBLY - } - @{ - SourceFiles = $SourceFiles - AppProcessId = $AppProcessId - } -} - -Write-Host ("$(Get-Timestamp): Sitecore Development ENTRYPOINT, starting...") - -if ($null -eq $ENV:ENTRYPOINT_ASSEMBLY -or !(Test-Path "C:\App\$($ENV:ENTRYPOINT_ASSEMBLY)")) { - Write-Host ("$(Get-Timestamp): ENTRYPOINT_ASSEMBLY environment variable is not set or the dll does not exist. Exiting..") -ForegroundColor Red - Exit 1 -} - -try { - Write-Host ("$(Get-Timestamp): Changes on '$($Path)', will deploy to '$($Destination)'...") - $LastRunSourceFiles = Get-WatchedFiles -RootFolder $Destination | ForEach-Object { Get-FileHash -Path $_.FullName } - $ProcessId = Start-DotNetApp -AppJobName $ENV:ENTRYPOINT_ASSEMBLY - Start-Sleep -Milliseconds $SleepMilliseconds - - while ($true) { - $SyncState = Sync -Path $Path -Destination $Destination -LastRunSourceFiles $LastRunSourceFiles -AppProcessId $ProcessId - if ($null -ne $SyncState) { - $LastRunSourceFiles = $SyncState.SourceFiles - $ProcessId = $SyncState.AppProcessId - } - Start-Sleep -Milliseconds $SleepMilliseconds - } -} -catch { - Write-Host ("$(Get-Timestamp): $($_.Exception.Message)") -ForegroundColor Red - Write-Host ("$(Get-Timestamp): $($_.Exception.StackTrace)") -ForegroundColor Red - Exit 1 -} -finally { - Write-Host ("$(Get-Timestamp): Development.ps1 stopped..") -ForegroundColor Magenta -} diff --git a/docker/build/rendering/Dockerfile b/docker/build/rendering/Dockerfile index bcb4d941..7c9ef215 100644 --- a/docker/build/rendering/Dockerfile +++ b/docker/build/rendering/Dockerfile @@ -11,23 +11,9 @@ FROM ${DEBUG_BASE_IMAGE} as debug ARG ARTIFACTS_FOLDER=/artifacts/mvp-rendering/ USER ContainerAdministrator -COPY ./Development.ps1 ./tools/ -WORKDIR /app -COPY --from=solution ${ARTIFACTS_FOLDER} ./ +WORKDIR /app/src/project/MvpSite/rendering USER ContainerUser ENV ASPNETCORE_URLS "http://+:80" EXPOSE 80 -ENTRYPOINT ["pwsh", "-File", "c:\\tools\\Development.ps1"] - -# release image (aspnet) -FROM ${RELEASE_BASE_IMAGE} as release -ARG ENTRYPOINT_ASSEMBLY -ARG ARTIFACTS_FOLDER=/artifacts/mvp-rendering/ - -ENV ENTRYPOINT_ASSEMBLY="${ENTRYPOINT_ASSEMBLY}" -WORKDIR /app -COPY --from=solution ${ARTIFACTS_FOLDER} ./ -EXPOSE 80 -EXPOSE 443 -ENTRYPOINT dotnet %ENTRYPOINT_ASSEMBLY% \ No newline at end of file +ENTRYPOINT ["dotnet", "watch"] \ No newline at end of file diff --git a/src/Project/MvpSite/rendering/Properties/launchSettings.json b/src/Project/MvpSite/rendering/Properties/launchSettings.json index 64980961..b90ccac0 100644 --- a/src/Project/MvpSite/rendering/Properties/launchSettings.json +++ b/src/Project/MvpSite/rendering/Properties/launchSettings.json @@ -1,6 +1,6 @@ -{ +{ "iisSettings": { - "windowsAuthentication": false, + "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:64048", @@ -16,18 +16,19 @@ } }, "Mvp.Project.MvpSite.Rendering": { + "commandName": "Project", + "launchBrowser": false, + "applicationUrl": "http://*:80", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "Mvp.Project.MvpSite.Rendering-VisualStudio": { "commandName": "Project", "launchBrowser": true, "applicationUrl": "https://localhost:5001;http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" - }, - "Docker": { - "commandName": "Docker", - "launchBrowser": true, - "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}", - "publishAllPorts": true, - "useSSL": true } } }