Skip to content

Commit 8fd6254

Browse files
committed
DockerBuild.ps1 - improvements.
1 parent 3c322c3 commit 8fd6254

File tree

1 file changed

+37
-24
lines changed

1 file changed

+37
-24
lines changed

src/PostSharp.Engineering.BuildTools/Resources/DockerBuild.ps1

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
# The original of this file is in the PostSharp.Engineering repo.
22
# You can generate this file using `./Build.ps1 generate-scripts`.
3+
34
[CmdletBinding(PositionalBinding = $false)]
45
param(
5-
[switch]$Interactive,
6-
[switch]$BuildImage,
7-
[switch]$NoBuildImage,
8-
[switch]$NoClean,
9-
[string]$ImageName,
6+
[switch]$Interactive, # Opens an interactive PowerShell session
7+
[switch]$BuildImage, # Only builds the image, but does not build the product.
8+
[switch]$NoBuildImage, # Does not build the image.
9+
[switch]$NoClean, # Does not clean up.
10+
[switch]$NoNuGetCache, # Does not mount the host nuget cache in the container.
11+
[switch]$KeepSecrets, # Does not override the secrets.g.json file.
12+
[string]$ImageName, # Image name (defaults to a name based on the directory).
1013
[string]$BuildAgentPath = 'C:\BuildAgent',
1114
[Parameter(ValueFromRemainingArguments)]
12-
[string[]]$BuildArgs
15+
[string[]]$BuildArgs # Arguments passed to `Build.ps1` within the container.
1316
)
1417

1518
# This setting is replaced by the generate-scripts command.
@@ -87,57 +90,67 @@ if (-not $env:IS_TEAMCITY_AGENT -and -not $NoClean)
8790
Get-ChildItem @("bin", "obj") -Recurse | Remove-Item -Force -Recurse -ProgressAction SilentlyContinue
8891
}
8992

90-
# Create secrets JSON file (after cleanup)
91-
$secretsJsonPath = New-SecretsJson -EnvironmentVariableList $EnvironmentVariables
93+
# Create secrets JSON file.
94+
if ( -not $KeepSecrets )
95+
{
96+
New-SecretsJson -EnvironmentVariableList $EnvironmentVariables
97+
}
9298

9399
# Get the source directory name from $PSScriptRoot
94100
$SourceDirName = $PSScriptRoot
95101

96102
# Start timing the entire process except cleaning
97103
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
98104

99-
100-
101105
# Ensure docker context directory exists and contains at least one file
102106
if (-not (Test-Path $dockerContextDirectory))
103107
{
104108
Write-Error "Docker context directory '$dockerContextDirectory' does not exist."
105109
exit 1
106110
}
107111

108-
# Create local NuGet cache directory if it doesn't exist
109-
$nugetCacheDir = Join-Path $env:USERPROFILE ".nuget\packages"
110-
Write-Host "NuGet cache directory: $nugetCacheDir" -ForegroundColor Cyan
111-
if (-not (Test-Path $nugetCacheDir))
112-
{
113-
Write-Host "Creating NuGet cache directory on host: $nugetCacheDir"
114-
New-Item -ItemType Directory -Force -Path $nugetCacheDir | Out-Null
115-
}
112+
113+
# Prepare volume mappings
114+
$volumeMappings = @("-v", "${SourceDirName}:${SourceDirName}")
115+
$MountPoints = @($SourceDirName)
116+
116117
# Define static Git system directory for mapping
117118
$gitSystemDir = "$BuildAgentPath\system\git"
118119

119-
# Prepare volume mappings
120-
$volumeMappings = @("-v", "${SourceDirName}:${SourceDirName}", "-v", "${nugetCacheDir}:c:\packages")
121-
$MountPoints = @($SourceDirName, "c:\packages")
122120
if (Test-Path $gitSystemDir)
123121
{
124122
$volumeMappings += @("-v", "${gitSystemDir}:${gitSystemDir}:ro")
125123
$MountPoints += $gitSystemDir
126124
}
127125

126+
# Mount the host NuGet cache in the container.
127+
if ( -not $NoNuGetCache )
128+
{
129+
$nugetCacheDir = Join-Path $env:USERPROFILE ".nuget\packages"
130+
Write-Host "NuGet cache directory: $nugetCacheDir" -ForegroundColor Cyan
131+
if (-not (Test-Path $nugetCacheDir))
132+
{
133+
Write-Host "Creating NuGet cache directory on host: $nugetCacheDir"
134+
New-Item -ItemType Directory -Force -Path $nugetCacheDir | Out-Null
135+
}
136+
137+
$volumeMappings += @("-v", "${nugetCacheDir}:c:\packages")
138+
}
139+
140+
# We must add a MountPoint anyway so the directory is created in the container.
141+
$MountPoints += "c:\packages"
142+
128143

129-
# Execute auto-generated DockerMounts script in current context
144+
# Execute auto-generated DockerMounts.g.ps1 script to add more directory mounts.
130145
$dockerMountsScript = Join-Path $EngPath 'DockerMounts.g.ps1'
131146
if (Test-Path $dockerMountsScript)
132147
{
133148
Write-Host "Importing Docker mount points from $dockerMountsScript" -ForegroundColor Cyan
134149
. $dockerMountsScript
135150
}
136151

137-
138152
$mountPointsArg = $MountPoints -Join ";"
139153

140-
141154
Write-Host "Volume mappings: " @volumeMappings -ForegroundColor Gray
142155
Write-Host "Mount points: " $mountPointsArg -ForegroundColor Gray
143156

0 commit comments

Comments
 (0)