Skip to content

Commit 9604305

Browse files
committed
Improve Docker build. Generate Build.ps1.
1 parent 59de2c4 commit 9604305

File tree

9 files changed

+187
-80
lines changed

9 files changed

+187
-80
lines changed

Build.ps1

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,30 @@
1-
(& dotnet nuget locals http-cache -c) | Out-Null
2-
& dotnet run --project "$PSScriptRoot\eng\src\BuildEngineering.csproj" -- $args
3-
exit $LASTEXITCODE
1+
# The original of this file is in the PostSharp.Engineering repo.
2+
# You can generate this file using `./Build.ps1 generate-scripts`.
43

4+
[CmdletBinding(PositionalBinding = $false)]
5+
param(
6+
[switch]$Interactive, # Opens an interactive PowerShell session
7+
[switch]$VsDebug, # Enable the remote debugger.
8+
[Parameter(ValueFromRemainingArguments)]
9+
[string[]]$BuildArgs # Arguments passed to `Build.ps1` within the container.
10+
)
11+
12+
####
13+
# These settings are replaced by the generate-scripts command.
14+
$EngPath = 'eng'
15+
$ProductName = 'PostSharpEngineering'
16+
####
17+
18+
if ( $env:RUNNING_IN_DOCKER -and $env:START_REMOTE_DEBUGGER )
19+
{
20+
$vsmonport = 4024
21+
Write-Host "Starting Visual Studio Remote Debugger, listening at port $vsmonport." -ForegroundColor Cyan
22+
Start-Process -FilePath "C:\msvsmon\msvsmon.exe" `
23+
-ArgumentList "/noauth","/anyuser","/silent","/port:$vsmonport","/timeout:2147483647" `
24+
-NoNewWindow
25+
}
26+
27+
if ( -not $Interactive )
28+
{
29+
& dotnet run --project "$PSScriptRoot\$EngPath\src\Build$ProductName.csproj" -- $args
30+
}

DockerBuild.ps1

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ param(
1212
[string]$ImageName, # Image name (defaults to a name based on the directory).
1313
[string]$BuildAgentPath = 'C:\BuildAgent',
1414
[switch]$LoadEnvFromKeyVault, # Forces loading environment variables form the key vault.
15+
[switch]$VsDebug, # Enable the remote debugger.
1516
[Parameter(ValueFromRemainingArguments)]
1617
[string[]]$BuildArgs # Arguments passed to `Build.ps1` within the container.
1718
)
@@ -36,13 +37,13 @@ function New-EnvJson
3637
$envVarNames = $EnvironmentVariableList -split ',' | ForEach-Object { $_.Trim() } | Where-Object { $_ -ne '' }
3738

3839
# Build hashtable with environment variable values
39-
$secrets = @{ }
40+
$envVariables = @{ }
4041
foreach ($envVarName in $envVarNames)
4142
{
4243
$value = [Environment]::GetEnvironmentVariable($envVarName)
4344
if (-not [string]::IsNullOrEmpty($value))
4445
{
45-
$secrets[$envVarName] = $value
46+
$envVariables[$envVarName] = $value
4647
}
4748
}
4849

@@ -63,10 +64,10 @@ function New-EnvJson
6364
$secretWithValue = Get-AzKeyVaultSecret -VaultName "PostSharpBuildEnv" -Name $secret.Name
6465
$envName = $secretWithValue.Name -Replace "-", "_"
6566
$envValue = (ConvertFrom-SecureString $secretWithValue.SecretValue -AsPlainText)
66-
$secrets[$envName] = $envValue
67+
$envVariables[$envName] = $envValue
6768
}
6869
}
69-
70+
7071
# Convert to JSON and save
7172
$jsonPath = Join-Path $dockerContextDirectory "env.g.json"
7273

@@ -81,7 +82,7 @@ function New-EnvJson
8182
exit 1
8283
}
8384

84-
$secrets | ConvertTo-Json -Depth 10 | Set-Content -Path $jsonPath -Encoding UTF8
85+
$envVariables | ConvertTo-Json -Depth 10 | Set-Content -Path $jsonPath -Encoding UTF8
8586
Write-Host "Created secrets file: $jsonPath" -ForegroundColor Cyan
8687

8788

@@ -160,6 +161,9 @@ if (-not (Test-Path $dockerContextDirectory))
160161
$volumeMappings = @("-v", "${SourceDirName}:${SourceDirName}")
161162
$MountPoints = @($SourceDirName)
162163

164+
# We must add a MountPoint anyway so the directory is created in the container.
165+
$MountPoints += "c:\packages"
166+
163167
# Define static Git system directory for mapping
164168
$gitSystemDir = "$BuildAgentPath\system\git"
165169

@@ -183,8 +187,27 @@ if (-not $NoNuGetCache)
183187
$volumeMappings += @("-v", "${nugetCacheDir}:c:\packages")
184188
}
185189

186-
# We must add a MountPoint anyway so the directory is created in the container.
187-
$MountPoints += "c:\packages"
190+
# Mount VS Remote Debugger
191+
if ( $VsDebug)
192+
{
193+
if ( -not $env:DevEnvDir)
194+
{
195+
Write-Host "Environment variable 'DevEnvDir' is not defined." -ForegroundColor Red
196+
exit 1
197+
}
198+
199+
$remoteDebuggerHostDir = "$($env:DevEnvDir)Remote Debugger\x64"
200+
if ( -not (Test-Path $remoteDebuggerHostDir))
201+
{
202+
Write-Host "Directory '$remoteDebuggerHostDir' does not exist." -ForegroundColor Red
203+
exit 1
204+
}
205+
206+
$remoteDebuggerContainerDir = "C:\msvsmon"
207+
$volumeMappings += @("-v", "${remoteDebuggerHostDir}:${remoteDebuggerContainerDir}:ro")
208+
$MountPoints += $remoteDebuggerContainerDir
209+
210+
}
188211

189212
# Discover symbolic links in source-dependencies and add their targets to mount points
190213
$sourceDependenciesDir = Join-Path $SourceDirName "source-dependencies"
@@ -241,33 +264,37 @@ else
241264
# Run the build within the container
242265
if (-not $BuildImage)
243266
{
244-
if ($Interactive)
267+
268+
# Delete now and not in the container because it's much faster and lock error messages are more relevant.
269+
Write-Host "Building the product in the container." -ForegroundColor Green
270+
271+
# Prepare Build.ps1 arguments
272+
$buildCommand = "$SourceDirName\Build.ps1"
273+
if ( $VsDebug )
245274
{
246-
docker run --rm -it --memory=12g @volumeMappings -w $SourceDirName $ImageName pwsh
247-
if ($LASTEXITCODE -ne 0)
248-
{
249-
Write-Host "Docker run (interactive) failed with exit code $LASTEXITCODE" -ForegroundColor Red
250-
exit $LASTEXITCODE
251-
}
275+
$BuildArgs = @("-VsDebug") + $BuildArgs
276+
}
277+
$buildArgsString = $BuildArgs -join " "
278+
$buildCommand += " $buildArgsString"
279+
280+
if ( $Interactive )
281+
{
282+
$pwshArgs = "-NoExit"
252283
}
253284
else
254285
{
255-
# Delete now and not in the container because it's much faster and lock error messages are more relevant.
256-
Write-Host "Building the product in the container." -ForegroundColor Green
257-
258-
# Prepare Build.ps1 arguments
259-
$buildCommand = "$SourceDirName\Build.ps1"
260-
$buildArgsString = $BuildArgs -join " "
261-
$buildCommand += " $buildArgsString"
262-
Write-Host "Executing in container: `".\Build.ps1 $buildArgsString`"." -ForegroundColor Cyan
286+
$pwshArgs = "-NonInteractive"
287+
}
288+
289+
Write-Host "Executing in container: `".\Build.ps1 $buildArgsString`"." -ForegroundColor Cyan
263290

264-
docker run --rm --memory=12g @volumeMappings -w $SourceDirName $ImageName pwsh -NonInteractive -Command $buildCommand
265-
if ($LASTEXITCODE -ne 0)
266-
{
267-
Write-Host "Docker run (build) failed with exit code $LASTEXITCODE" -ForegroundColor Red
268-
exit $LASTEXITCODE
269-
}
291+
docker run --rm --memory=12g @volumeMappings -w $SourceDirName $ImageName pwsh $pwshArgs -Command $buildCommand
292+
if ($LASTEXITCODE -ne 0)
293+
{
294+
Write-Host "Docker run (build) failed with exit code $LASTEXITCODE" -ForegroundColor Red
295+
exit $LASTEXITCODE
270296
}
297+
271298
}
272299
else
273300
{

eng/src/BuildEngineering.sln

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/PostSharp.Engineering.BuildTools/ContinuousIntegration/GenerateScriptsCommand.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ public static bool Execute( BuildContext context, CommonCommandSettings settings
2222
{
2323
return false;
2424
}
25+
26+
EmbeddedResourceHelper.ExtractScript( context, "Build.ps1", "" );
2527

26-
// Docket.
28+
// Docker.
2729
if ( product.UseDocker )
2830
{
2931
EmbeddedResourceHelper.ExtractScript( context, "DockerBuild.ps1", "" );

src/PostSharp.Engineering.BuildTools/Docker/EpilogueComponent.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ ARG MOUNTPOINTS
3535
3636
# Configure NuGet
3737
ENV NUGET_PACKAGES=c:\packages
38+
39+
# Configure .NET SDK
40+
ENV DOTNET_NOLOGO=1
3841
3942
# Configure git
4043
ARG SRC_DIR
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# The original of this file is in the PostSharp.Engineering repo.
2+
# You can generate this file using `./Build.ps1 generate-scripts`.
3+
4+
[CmdletBinding(PositionalBinding = $false)]
5+
param(
6+
[switch]$Interactive, # Opens an interactive PowerShell session
7+
[switch]$VsDebug, # Enable the remote debugger.
8+
[Parameter(ValueFromRemainingArguments)]
9+
[string[]]$BuildArgs # Arguments passed to `Build.ps1` within the container.
10+
)
11+
12+
####
13+
# These settings are replaced by the generate-scripts command.
14+
$EngPath = '<ENG_PATH>'
15+
$ProductName = '<PRODUCT_NAME>'
16+
####
17+
18+
if ( $VsDebug )
19+
{
20+
$vsmonport = 4024
21+
Write-Host "Starting Visual Studio Remote Debugger, listening at port $vsmonport." -ForegroundColor Cyan
22+
Start-Process -FilePath "C:\msvsmon\msvsmon.exe" `
23+
-ArgumentList "/noauth","/anyuser","/silent","/port:$vsmonport","/timeout:2147483647" `
24+
-NoNewWindow
25+
}
26+
27+
# Change the prompt and window title in Docker.
28+
if ( $env:RUNNING_IN_DOCKER )
29+
{
30+
function global:prompt {
31+
$host.UI.RawUI.WindowTitle = "[docker] " + (Get-Location).Path
32+
"[docker] $(Get-Location)> "
33+
}
34+
}
35+
36+
37+
if ( -not $Interactive -or $BuildArgs )
38+
{
39+
& dotnet run --project "$PSScriptRoot\$EngPath\src\Build$ProductName.csproj" -- $BuildArgs
40+
}

0 commit comments

Comments
 (0)