Skip to content

Commit 3650530

Browse files
committed
Dump and kill process kill upon timeout.
1 parent a1113cd commit 3650530

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+660
-350
lines changed

.teamcity/settings.kts

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ object DebugBuild : BuildType({
2727

2828
name = "Build [Debug]"
2929

30-
artifactRules = "+:artifacts/publish/public/**/*=>artifacts/publish/public\n+:artifacts/publish/private/**/*=>artifacts/publish/private\n+:artifacts/testResults/**/*=>artifacts/testResults\n+:artifacts/logs/**/*=>logs\n"
30+
artifactRules = "+:artifacts/publish/public/**/*=>artifacts/publish/public\n+:artifacts/publish/private/**/*=>artifacts/publish/private\n+:artifacts/testResults/**/*=>artifacts/testResults\n+:artifacts/logs/**/*=>logs\n+:artifacts/dumps/**/*=>dumps\n"
3131

3232
params {
33-
text("BuildArguments", "", label = "Build Arguments", description = "Arguments to append to the 'Build' build step.", allowEmpty = true)
33+
param("PrepareImage.Timeout", "120")
34+
param("Build.Timeout", "35")
35+
text("Build.Arguments", "", label = "DockerBuild.ps1 Arguments", description = "Arguments to append to the 'Build' build step.", allowEmpty = true)
3436
text("DefaultBranch", "develop/2023.2", label = "Default Branch", description = "The default branch of this build configuration.")
35-
text("TimeOut", "60", label = "Time-Out", description = "Timeout, in minutes.", regex = """\d+""", validationMessage = "The timeout has to be an integer number.")
3637
}
3738

3839
vcs {
@@ -48,6 +49,7 @@ object DebugBuild : BuildType({
4849
}
4950
noProfile = false
5051
scriptArgs = "-BuildImage -ImageName postsharpengineering-2023.2"
52+
executionTimeoutMin = "%PrepareImage.Timeout%.toInt()"
5153
}
5254
powerShell {
5355
name = "Build"
@@ -56,12 +58,10 @@ object DebugBuild : BuildType({
5658
path = "DockerBuild.ps1"
5759
}
5860
noProfile = false
59-
scriptArgs = "-ImageName postsharpengineering-2023.2 -NoBuildImage test --configuration Debug --buildNumber %build.number% --buildType %system.teamcity.buildType.id% %BuildArguments%"
61+
scriptArgs = "-ImageName postsharpengineering-2023.2 -NoBuildImage test --configuration Debug --buildNumber %build.number% --buildType %system.teamcity.buildType.id% %Build.Arguments%"
62+
executionTimeoutMin = "%Build.Timeout%.toInt()"
6063
}
6164
}
62-
failureConditions {
63-
executionTimeoutMin = 60
64-
}
6565

6666
requirements {
6767
equals("env.BuildAgentType", "docker-win-x64-md")
@@ -89,12 +89,13 @@ object ReleaseBuild : BuildType({
8989

9090
name = "Build [Release]"
9191

92-
artifactRules = "+:artifacts/publish/public/**/*=>artifacts/publish/public\n+:artifacts/publish/private/**/*=>artifacts/publish/private\n+:artifacts/testResults/**/*=>artifacts/testResults\n+:artifacts/logs/**/*=>logs\n"
92+
artifactRules = "+:artifacts/publish/public/**/*=>artifacts/publish/public\n+:artifacts/publish/private/**/*=>artifacts/publish/private\n+:artifacts/testResults/**/*=>artifacts/testResults\n+:artifacts/logs/**/*=>logs\n+:artifacts/dumps/**/*=>dumps\n"
9393

9494
params {
95-
text("BuildArguments", "", label = "Build Arguments", description = "Arguments to append to the 'Build' build step.", allowEmpty = true)
95+
param("PrepareImage.Timeout", "120")
96+
param("Build.Timeout", "35")
97+
text("Build.Arguments", "", label = "DockerBuild.ps1 Arguments", description = "Arguments to append to the 'Build' build step.", allowEmpty = true)
9698
text("DefaultBranch", "develop/2023.2", label = "Default Branch", description = "The default branch of this build configuration.")
97-
text("TimeOut", "60", label = "Time-Out", description = "Timeout, in minutes.", regex = """\d+""", validationMessage = "The timeout has to be an integer number.")
9899
}
99100

100101
vcs {
@@ -110,6 +111,7 @@ object ReleaseBuild : BuildType({
110111
}
111112
noProfile = false
112113
scriptArgs = "-BuildImage -ImageName postsharpengineering-2023.2"
114+
executionTimeoutMin = "%PrepareImage.Timeout%.toInt()"
113115
}
114116
powerShell {
115117
name = "Build"
@@ -118,12 +120,10 @@ object ReleaseBuild : BuildType({
118120
path = "DockerBuild.ps1"
119121
}
120122
noProfile = false
121-
scriptArgs = "-ImageName postsharpengineering-2023.2 -NoBuildImage test --configuration Release --buildNumber %build.number% --buildType %system.teamcity.buildType.id% %BuildArguments%"
123+
scriptArgs = "-ImageName postsharpengineering-2023.2 -NoBuildImage test --configuration Release --buildNumber %build.number% --buildType %system.teamcity.buildType.id% %Build.Arguments%"
124+
executionTimeoutMin = "%Build.Timeout%.toInt()"
122125
}
123126
}
124-
failureConditions {
125-
executionTimeoutMin = 60
126-
}
127127

128128
requirements {
129129
equals("env.BuildAgentType", "docker-win-x64-md")
@@ -142,12 +142,13 @@ object PublicBuild : BuildType({
142142

143143
name = "Build [Public]"
144144

145-
artifactRules = "+:artifacts/publish/public/**/*=>artifacts/publish/public\n+:artifacts/publish/private/**/*=>artifacts/publish/private\n+:artifacts/testResults/**/*=>artifacts/testResults\n+:artifacts/logs/**/*=>logs\n"
145+
artifactRules = "+:artifacts/publish/public/**/*=>artifacts/publish/public\n+:artifacts/publish/private/**/*=>artifacts/publish/private\n+:artifacts/testResults/**/*=>artifacts/testResults\n+:artifacts/logs/**/*=>logs\n+:artifacts/dumps/**/*=>dumps\n"
146146

147147
params {
148-
text("BuildArguments", "", label = "Build Arguments", description = "Arguments to append to the 'Build' build step.", allowEmpty = true)
148+
param("PrepareImage.Timeout", "120")
149+
param("Build.Timeout", "35")
150+
text("Build.Arguments", "", label = "DockerBuild.ps1 Arguments", description = "Arguments to append to the 'Build' build step.", allowEmpty = true)
149151
text("DefaultBranch", "develop/2023.2", label = "Default Branch", description = "The default branch of this build configuration.")
150-
text("TimeOut", "60", label = "Time-Out", description = "Timeout, in minutes.", regex = """\d+""", validationMessage = "The timeout has to be an integer number.")
151152
}
152153

153154
vcs {
@@ -163,6 +164,7 @@ object PublicBuild : BuildType({
163164
}
164165
noProfile = false
165166
scriptArgs = "-BuildImage -ImageName postsharpengineering-2023.2"
167+
executionTimeoutMin = "%PrepareImage.Timeout%.toInt()"
166168
}
167169
powerShell {
168170
name = "Build"
@@ -171,12 +173,10 @@ object PublicBuild : BuildType({
171173
path = "DockerBuild.ps1"
172174
}
173175
noProfile = false
174-
scriptArgs = "-ImageName postsharpengineering-2023.2 -NoBuildImage test --configuration Public --buildNumber %build.number% --buildType %system.teamcity.buildType.id% %BuildArguments%"
176+
scriptArgs = "-ImageName postsharpengineering-2023.2 -NoBuildImage test --configuration Public --buildNumber %build.number% --buildType %system.teamcity.buildType.id% %Build.Arguments%"
177+
executionTimeoutMin = "%Build.Timeout%.toInt()"
175178
}
176179
}
177-
failureConditions {
178-
executionTimeoutMin = 60
179-
}
180180

181181
requirements {
182182
equals("env.BuildAgentType", "docker-win-x64-md")
@@ -198,9 +198,11 @@ object PublicDeployment : BuildType({
198198
type = Type.DEPLOYMENT
199199

200200
params {
201-
text("PublishArguments", "", label = "Publish Arguments", description = "Arguments to append to the 'Publish' build step.", allowEmpty = true)
201+
param("PrepareImage.Timeout", "120")
202+
param("Publish.Timeout", "15")
203+
text("Publish.Arguments", "", label = "DockerBuild.ps1 Arguments", description = "Arguments to append to the 'Publish' build step.", allowEmpty = true)
202204
text("DefaultBranch", "develop/2023.2", label = "Default Branch", description = "The default branch of this build configuration.")
203-
text("TimeOut", "30", label = "Time-Out", description = "Timeout, in minutes.", regex = """\d+""", validationMessage = "The timeout has to be an integer number.")
205+
text("Timeout.All", "30", label = "Time-Out", description = "Timeout, in minutes.", regex = """\d+""", validationMessage = "The timeout has to be an integer number.")
204206
}
205207

206208
vcs {
@@ -216,6 +218,7 @@ object PublicDeployment : BuildType({
216218
}
217219
noProfile = false
218220
scriptArgs = "-BuildImage -ImageName postsharpengineering-2023.2"
221+
executionTimeoutMin = "%PrepareImage.Timeout%.toInt()"
219222
}
220223
powerShell {
221224
name = "Publish"
@@ -224,7 +227,8 @@ object PublicDeployment : BuildType({
224227
path = "DockerBuild.ps1"
225228
}
226229
noProfile = false
227-
scriptArgs = "-ImageName postsharpengineering-2023.2 -NoBuildImage publish --configuration Public %PublishArguments%"
230+
scriptArgs = "-ImageName postsharpengineering-2023.2 -NoBuildImage publish --configuration Public %Publish.Arguments%"
231+
228232
}
229233
}
230234
failureConditions {
@@ -262,9 +266,11 @@ object VersionBump : BuildType({
262266
name = "Version Bump"
263267

264268
params {
265-
text("BumpArguments", "", label = "Bump Arguments", description = "Arguments to append to the 'Bump' build step.", allowEmpty = true)
269+
param("PrepareImage.Timeout", "120")
270+
param("Bump.Timeout", "15")
271+
text("Bump.Arguments", "", label = "DockerBuild.ps1 Arguments", description = "Arguments to append to the 'Bump' build step.", allowEmpty = true)
266272
text("DefaultBranch", "develop/2023.2", label = "Default Branch", description = "The default branch of this build configuration.")
267-
text("TimeOut", "15", label = "Time-Out", description = "Timeout, in minutes.", regex = """\d+""", validationMessage = "The timeout has to be an integer number.")
273+
text("Timeout.All", "15", label = "Time-Out", description = "Timeout, in minutes.", regex = """\d+""", validationMessage = "The timeout has to be an integer number.")
268274
}
269275

270276
vcs {
@@ -280,6 +286,7 @@ object VersionBump : BuildType({
280286
}
281287
noProfile = false
282288
scriptArgs = "-BuildImage -ImageName postsharpengineering-2023.2"
289+
executionTimeoutMin = "%PrepareImage.Timeout%.toInt()"
283290
}
284291
powerShell {
285292
name = "Bump"
@@ -288,7 +295,8 @@ object VersionBump : BuildType({
288295
path = "DockerBuild.ps1"
289296
}
290297
noProfile = false
291-
scriptArgs = "-ImageName postsharpengineering-2023.2 -NoBuildImage bump %BumpArguments%"
298+
scriptArgs = "-ImageName postsharpengineering-2023.2 -NoBuildImage bump %Bump.Arguments%"
299+
292300
}
293301
}
294302
failureConditions {

DockerBuild.ps1

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ function New-EnvJson
5353
{
5454
$moduleName = "Az.KeyVault"
5555

56-
if (-not (Get-Module -ListAvailable -Name $moduleName)) {
56+
if (-not (Get-Module -ListAvailable -Name $moduleName))
57+
{
5758
Write-Error "The required module '$moduleName' is not installed. Please install it with: Install-Module -Name $moduleName"
5859
exit 1
5960
}
@@ -158,18 +159,16 @@ if (-not (Test-Path $dockerContextDirectory))
158159

159160

160161
# Prepare volume mappings
161-
$volumeMappings = @("-v", "${SourceDirName}:${SourceDirName}")
162-
$MountPoints = @($SourceDirName)
163-
164-
# We must add a MountPoint anyway so the directory is created in the container.
165-
$MountPoints += "c:\packages"
162+
$VolumeMappings = @("-v", "${SourceDirName}:${SourceDirName}")
163+
$MountPoints = @($SourceDirName, "c:\packages")
164+
$GitDirectories = @($SourceDirName)
166165

167-
# Define static Git system directory for mapping
166+
# Define static Git system directory for mapping. This used by Teamcity as an LFS parent repo.
168167
$gitSystemDir = "$BuildAgentPath\system\git"
169168

170169
if (Test-Path $gitSystemDir)
171170
{
172-
$volumeMappings += @("-v", "${gitSystemDir}:${gitSystemDir}:ro")
171+
$VolumeMappings += @("-v", "${gitSystemDir}:${gitSystemDir}:ro")
173172
$MountPoints += $gitSystemDir
174173
}
175174

@@ -184,27 +183,27 @@ if (-not $NoNuGetCache)
184183
New-Item -ItemType Directory -Force -Path $nugetCacheDir | Out-Null
185184
}
186185

187-
$volumeMappings += @("-v", "${nugetCacheDir}:c:\packages")
186+
$VolumeMappings += @("-v", "${nugetCacheDir}:c:\packages")
188187
}
189188

190189
# Mount VS Remote Debugger
191-
if ( $VsDebug)
190+
if ($VsDebug)
192191
{
193-
if ( -not $env:DevEnvDir)
192+
if (-not $env:DevEnvDir)
194193
{
195194
Write-Host "Environment variable 'DevEnvDir' is not defined." -ForegroundColor Red
196195
exit 1
197196
}
198197

199-
$remoteDebuggerHostDir = "$($env:DevEnvDir)Remote Debugger\x64"
200-
if ( -not (Test-Path $remoteDebuggerHostDir))
198+
$remoteDebuggerHostDir = "$( $env:DevEnvDir )Remote Debugger\x64"
199+
if (-not (Test-Path $remoteDebuggerHostDir))
201200
{
202201
Write-Host "Directory '$remoteDebuggerHostDir' does not exist." -ForegroundColor Red
203202
exit 1
204203
}
205204

206205
$remoteDebuggerContainerDir = "C:\msvsmon"
207-
$volumeMappings += @("-v", "${remoteDebuggerHostDir}:${remoteDebuggerContainerDir}:ro")
206+
$VolumeMappings += @("-v", "${remoteDebuggerHostDir}:${remoteDebuggerContainerDir}:ro")
208207
$MountPoints += $remoteDebuggerContainerDir
209208

210209
}
@@ -221,14 +220,21 @@ if (Test-Path $sourceDependenciesDir)
221220
if (-not [string]::IsNullOrEmpty($targetPath) -and (Test-Path $targetPath))
222221
{
223222
Write-Host "Found symbolic link '$( $link.Name )' -> '$targetPath'" -ForegroundColor Cyan
224-
$volumeMappings += @("-v", "${targetPath}:${targetPath}:ro")
223+
$VolumeMappings += @("-v", "${targetPath}:${targetPath}:ro")
225224
$MountPoints += $targetPath
225+
$GitDirectories += $targetPath
226226
}
227227
else
228228
{
229229
Write-Host "Warning: Symbolic link '$( $link.Name )' target '$targetPath' does not exist or is invalid" -ForegroundColor Yellow
230230
}
231231
}
232+
233+
$sourceDirectories = Get-ChildItem -Path $sourceDependenciesDir -Force | Where-Object { $_.LinkType -eq $null }
234+
foreach ($sourceDirectory in $sourceDirectories)
235+
{
236+
$GitDirectories += $sourceDirectory
237+
}
232238
}
233239

234240
# Execute auto-generated DockerMounts.g.ps1 script to add more directory mounts.
@@ -239,16 +245,18 @@ if (Test-Path $dockerMountsScript)
239245
. $dockerMountsScript
240246
}
241247

242-
$mountPointsArg = $MountPoints -Join ";"
248+
$mountPointsAsString = $MountPoints -Join ";"
249+
$gitDirectoriesAsString = $GitDirectories -Join ";"
243250

244-
Write-Host "Volume mappings: " @volumeMappings -ForegroundColor Gray
245-
Write-Host "Mount points: " $mountPointsArg -ForegroundColor Gray
251+
Write-Host "Volume mappings: " @VolumeMappings -ForegroundColor Gray
252+
Write-Host "Mount points: " $mountPointsAsString -ForegroundColor Gray
253+
Write-Host "Git directories: " $gitDirectoriesAsString -ForegroundColor Gray
246254

247255
# Building the image.
248256
if (-not $NoBuildImage)
249257
{
250258
Write-Host "Building the image." -ForegroundColor Green
251-
Get-Content -Raw Dockerfile | docker build -t $ImageName --build-arg SRC_DIR="$SourceDirName" --build-arg MOUNTPOINTS="$mountPointsArg" -f - $dockerContextDirectory
259+
Get-Content -Raw Dockerfile | docker build -t $ImageName --build-arg GITDIRS="$gitDirectoriesAsString" --build-arg MOUNTPOINTS="$mountPointsAsString" -f - $dockerContextDirectory
252260
if ($LASTEXITCODE -ne 0)
253261
{
254262
Write-Host "Docker build failed with exit code $LASTEXITCODE" -ForegroundColor Red
@@ -269,12 +277,12 @@ if (-not $BuildImage)
269277
Write-Host "Building the product in the container." -ForegroundColor Green
270278

271279
# Prepare Build.ps1 arguments
272-
if ( $VsDebug )
280+
if ($VsDebug)
273281
{
274282
$BuildArgs = @("-VsDebug") + $BuildArgs
275283
}
276284

277-
if ( $Interactive )
285+
if ($Interactive)
278286
{
279287
$pwshArgs = "-NoExit"
280288
$BuildArgs = @("-Interactive") + $BuildArgs
@@ -287,13 +295,13 @@ if (-not $BuildImage)
287295
}
288296

289297
$buildArgsString = $BuildArgs -join " "
290-
$volumeMappingsAsString = $volumeMappings -join " "
298+
$VolumeMappingsAsString = $VolumeMappings -join " "
291299
$dockerArgsAsString = $dockerArgs -join " "
292300

293301

294-
Write-Host "Executing: ``docker run --rm --memory=12g $volumeMappingsAsString -w $SourceDirName $dockerArgsAsString $ImageName pwsh $pwshArgs -Command `"& .\Build.ps1 $buildArgsString`"``." -ForegroundColor Cyan
302+
Write-Host "Executing: ``docker run --rm --memory=12g $VolumeMappingsAsString -w $SourceDirName $dockerArgsAsString $ImageName pwsh $pwshArgs -Command `"& .\Build.ps1 $buildArgsString`"``." -ForegroundColor Cyan
295303

296-
docker run --rm --memory=12g @volumeMappings -w $SourceDirName @dockerArgs $ImageName pwsh $pwshArgs -Command "& .\Build.ps1 $buildArgsString"
304+
docker run --rm --memory=12g @VolumeMappings -w $SourceDirName @dockerArgs $ImageName pwsh $pwshArgs -Command "& .\Build.ps1 $buildArgsString"
297305
if ($LASTEXITCODE -ne 0)
298306
{
299307
Write-Host "Docker run (build) failed with exit code $LASTEXITCODE" -ForegroundColor Red

Dockerfile

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ SHELL ["powershell", "-Command"]
99

1010
# Prepare environment
1111
ENV PSExecutionPolicyPreference=Bypass
12-
ENV POWERSHELL_UPDATECHECK=FALSE
12+
ENV POWERSHELL_UPDATECHECK=Off
1313
ENV TEMP=C:\Temp
1414
ENV TMP=C:\Temp
1515
ENV RUNNING_IN_DOCKER=TRUE
@@ -74,7 +74,14 @@ ENV NUGET_PACKAGES=c:\packages
7474
ENV DOTNET_NOLOGO=1
7575

7676
# Configure git
77-
ARG SRC_DIR
78-
RUN git config --global --add safe.directory $env:SRC_DIR/; `
79-
git config --global user.name $env:GIT_USER_NAME; `
77+
ARG GITDIRS
78+
RUN if ($env:GITDIRS) { `
79+
$gitdirs = $env:GITDIRS -split ';'; `
80+
foreach ($dir in $gitdirs) { `
81+
if ($dir) { `
82+
git config --global --add safe.directory $dir/; `
83+
} `
84+
} `
85+
}
86+
RUN git config --global user.name $env:GIT_USER_NAME; `
8087
git config --global user.email $env:GIT_USER_EMAIL;

src/PostSharp.Engineering.BuildTools/AppExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using PostSharp.Engineering.BuildTools.Tools.Csproj;
1818
using PostSharp.Engineering.BuildTools.Tools.Git;
1919
using PostSharp.Engineering.BuildTools.Tools.NuGet;
20+
using PostSharp.Engineering.BuildTools.Tools.Processes;
2021
using PostSharp.Engineering.BuildTools.Tools.TeamCity;
2122
using PostSharp.Engineering.BuildTools.Tools.XmlDoc;
2223
using Spectre.Console.Cli;
@@ -220,6 +221,10 @@ internal static void AddCommands( this CommandApp app, Product product )
220221
.WithData( data )
221222
.WithDescription( "Kill all compiler processes" );
222223

224+
tools.AddCommand<DumpAndKillCommand>( "dump-and-kill" )
225+
.WithData( data )
226+
.WithDescription( "Dump and kill a given process and all its descendants" );
227+
223228
tools.AddBranch(
224229
"csproj",
225230
csproj => csproj.AddCommand<AddProjectReferenceCommand>( "add-project-reference" )

src/PostSharp.Engineering.BuildTools/Build/BuildCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public static bool Execute( BuildContext context, BuildSettings settings )
127127
// We have to read the version from the file we have generated - using MSBuild, because it contains properties.
128128
var buildInfo = BuildArguments.Read( context, settings.BuildConfiguration );
129129

130-
var privateArtifactsDirectory = product.GetPrivateArtifactsDirectory( context, buildInfo );
130+
var privateArtifactsDirectory = product.GetPrivateArtifactsDirectory( context );
131131

132132
// Build solutions.
133133
IEnumerable<Solution> solutionsToBuild;
@@ -167,7 +167,7 @@ public static bool Execute( BuildContext context, BuildSettings settings )
167167
}
168168
}
169169

170-
var publicArtifactsDirectory = product.GetPublicArtifactsDirectory( context, buildInfo );
170+
var publicArtifactsDirectory = product.GetPublicArtifactsDirectory( context );
171171

172172
// Allow for some customization before we create the zip file and copy to the public directory.
173173
var eventArgs = new BuildCompletedEventArgs( context, settings, buildInfo, privateArtifactsDirectory, publicArtifactsDirectory );

0 commit comments

Comments
 (0)