Skip to content

Commit 5589f63

Browse files
gfraiteurclaude
andcommitted
Use dynamic memory defaults in DockerBuild.ps1
Replace hardcoded -Memory default with value from ContainerHostRequirements.Memory. Add 4GB extra for Claude mode at runtime. Remove -Memory argument from TeamCity settings. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 1dc5c70 commit 5589f63

File tree

5 files changed

+538
-523
lines changed

5 files changed

+538
-523
lines changed

DockerBuild.ps1

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ param(
2020
[string]$Dockerfile, # Path to custom Dockerfile (defaults to Dockerfile or Dockerfile.claude based on -Claude).
2121
[switch]$NoInit, # Do not generate or call Init.g.ps1 (skips git config, safe.directory, etc).
2222
[string]$Isolation = 'process', # Docker isolation mode (process or hyperv).
23-
[string]$Memory = '16g', # Docker memory limit.
23+
[string]$Memory, # Docker memory limit. Default calculated from $DefaultMemoryGb.
2424
[int]$Cpus = [Environment]::ProcessorCount, # Docker CPU limit (defaults to host's CPU count).
2525
[string[]]$Mount, # Additional directories to mount from host (readonly by default, append :w for writable). Supports * and ** glob patterns.
2626
[Parameter(ValueFromRemainingArguments)]
@@ -31,8 +31,15 @@ param(
3131
# These settings are replaced by the generate-scripts command.
3232
$EngPath = 'eng'
3333
$EnvironmentVariables = 'AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,AZ_IDENTITY_USERNAME,AZURE_CLIENT_ID,AZURE_CLIENT_SECRET,AZURE_DEVOPS_TOKEN,AZURE_DEVOPS_USER,AZURE_TENANT_ID,DOC_API_KEY,DOWNLOADS_API_KEY,ENG_USERNAME,GIT_USER_EMAIL,GIT_USER_NAME,GITHUB_AUTHOR_EMAIL,GITHUB_REVIEWER_TOKEN,GITHUB_TOKEN,IS_POSTSHARP_OWNED,IS_TEAMCITY_AGENT,MetalamaLicense,NUGET_ORG_API_KEY,PostSharpLicense,SIGNSERVER_SECRET,TEAMCITY_CLOUD_TOKEN,TYPESENSE_API_KEY,VS_MARKETPLACE_ACCESS_TOKEN,VSS_NUGET_EXTERNAL_FEED_ENDPOINTS'
34+
$DefaultMemoryGb = 8
3435
####
3536

37+
# Calculate default memory: add 4GB for Claude mode
38+
if (-not $Memory) {
39+
$memoryGb = if ($Claude) { $DefaultMemoryGb + 4 } else { $DefaultMemoryGb }
40+
$Memory = "${memoryGb}g"
41+
}
42+
3643
$ErrorActionPreference = "Stop"
3744
$dockerContextDirectory = "$EngPath/docker-context"
3845

src/PostSharp.Engineering.BuildTools/ContinuousIntegration/TeamCity/BuildSteps/PowerShellScriptBuildStep.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public PowerShellScriptBuildStep(
4141
else
4242
{
4343
this.ScriptPath = "DockerBuild.ps1";
44-
this.ScriptArguments = $"-Script {scriptPath} -ImageName {dockerSpec.ImageName} -Memory {dockerSpec.Memory}G -NoBuildImage {scriptArguments} {buildParameterValue}";
44+
this.ScriptArguments = $"-Script {scriptPath} -ImageName {dockerSpec.ImageName} -NoBuildImage {scriptArguments} {buildParameterValue}";
4545
}
4646

4747
if ( areCustomArgumentsAllowed )

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ param(
2020
[string]$Dockerfile, # Path to custom Dockerfile (defaults to Dockerfile or Dockerfile.claude based on -Claude).
2121
[switch]$NoInit, # Do not generate or call Init.g.ps1 (skips git config, safe.directory, etc).
2222
[string]$Isolation = 'process', # Docker isolation mode (process or hyperv).
23-
[string]$Memory = '16g', # Docker memory limit.
23+
[string]$Memory, # Docker memory limit. Default calculated from $DefaultMemoryGb.
2424
[int]$Cpus = [Environment]::ProcessorCount, # Docker CPU limit (defaults to host's CPU count).
2525
[string[]]$Mount, # Additional directories to mount from host (readonly by default, append :w for writable). Supports * and ** glob patterns.
2626
[Parameter(ValueFromRemainingArguments)]
@@ -31,8 +31,15 @@ param(
3131
# These settings are replaced by the generate-scripts command.
3232
$EngPath = '<ENG_PATH>'
3333
$EnvironmentVariables = '<ENVIRONMENT_VARIABLES>'
34+
$DefaultMemoryGb = <DEFAULT_MEMORY_GB>
3435
####
3536

37+
# Calculate default memory: add 4GB for Claude mode
38+
if (-not $Memory) {
39+
$memoryGb = if ($Claude) { $DefaultMemoryGb + 4 } else { $DefaultMemoryGb }
40+
$Memory = "${memoryGb}g"
41+
}
42+
3643
$ErrorActionPreference = "Stop"
3744
$dockerContextDirectory = "$EngPath/docker-context"
3845

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,49 @@
1-
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.
2-
3-
using PostSharp.Engineering.BuildTools.Build;
4-
using System;
5-
using System.Collections.Generic;
6-
using System.IO;
7-
using System.Linq;
8-
9-
namespace PostSharp.Engineering.BuildTools.Utilities;
10-
11-
internal static class EmbeddedResourceHelper
12-
{
13-
public static void ExtractScript( BuildContext context, string fileName, string targetDirectory )
14-
{
15-
var product = context.Product;
16-
var replacements = new Dictionary<string, string>();
17-
replacements.Add( "<ENG_PATH>", product.EngineeringDirectory );
18-
replacements.Add( "<ENVIRONMENT_VARIABLES>", string.Join( ",", EnvironmentVariableNames.All.OrderBy( x => x ) ) );
19-
replacements.Add( "<PRODUCT_NAME>", product.ProductNameWithoutDot );
20-
21-
ExtractResource( context, fileName, targetDirectory, replacements );
22-
}
23-
24-
public static void ExtractResource(
25-
BuildContext context,
26-
string fileName,
27-
string targetDirectory,
28-
IReadOnlyDictionary<string, string>? replacements = null )
29-
{
30-
var targetPath = Path.Combine( context.RepoDirectory, targetDirectory, fileName );
31-
32-
using var resource = typeof(EmbeddedResourceHelper).Assembly.GetManifestResourceStream( $"PostSharp.Engineering.BuildTools.Resources.{fileName}" )
33-
?? throw new InvalidOperationException( $"Cannot find the resource {fileName}." );
34-
35-
using var reader = new StreamReader( resource );
36-
var text = reader.ReadToEnd();
37-
38-
if ( replacements != null )
39-
{
40-
foreach ( var replacement in replacements )
41-
{
42-
text = text.Replace( replacement.Key, replacement.Value, StringComparison.Ordinal );
43-
}
44-
}
45-
46-
TextFileHelper.WriteIfDifferent( targetPath, text, context );
47-
}
1+
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.
2+
3+
using PostSharp.Engineering.BuildTools.Build;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Linq;
8+
9+
namespace PostSharp.Engineering.BuildTools.Utilities;
10+
11+
internal static class EmbeddedResourceHelper
12+
{
13+
public static void ExtractScript( BuildContext context, string fileName, string targetDirectory )
14+
{
15+
var product = context.Product;
16+
var replacements = new Dictionary<string, string>();
17+
replacements.Add( "<ENG_PATH>", product.EngineeringDirectory );
18+
replacements.Add( "<ENVIRONMENT_VARIABLES>", string.Join( ",", EnvironmentVariableNames.All.OrderBy( x => x ) ) );
19+
replacements.Add( "<PRODUCT_NAME>", product.ProductNameWithoutDot );
20+
replacements.Add( "<DEFAULT_MEMORY_GB>", (product.DockerSpec?.Memory ?? 8).ToString( System.Globalization.CultureInfo.InvariantCulture ) );
21+
22+
ExtractResource( context, fileName, targetDirectory, replacements );
23+
}
24+
25+
public static void ExtractResource(
26+
BuildContext context,
27+
string fileName,
28+
string targetDirectory,
29+
IReadOnlyDictionary<string, string>? replacements = null )
30+
{
31+
var targetPath = Path.Combine( context.RepoDirectory, targetDirectory, fileName );
32+
33+
using var resource = typeof(EmbeddedResourceHelper).Assembly.GetManifestResourceStream( $"PostSharp.Engineering.BuildTools.Resources.{fileName}" )
34+
?? throw new InvalidOperationException( $"Cannot find the resource {fileName}." );
35+
36+
using var reader = new StreamReader( resource );
37+
var text = reader.ReadToEnd();
38+
39+
if ( replacements != null )
40+
{
41+
foreach ( var replacement in replacements )
42+
{
43+
text = text.Replace( replacement.Key, replacement.Value, StringComparison.Ordinal );
44+
}
45+
}
46+
47+
TextFileHelper.WriteIfDifferent( targetPath, text, context );
48+
}
4849
}

0 commit comments

Comments
 (0)