Skip to content

Commit 8c4af1e

Browse files
committed
New attempt to fix git authentication for downstream merge and other.
1 parent 8ed2ab3 commit 8c4af1e

File tree

8 files changed

+48
-71
lines changed

8 files changed

+48
-71
lines changed

src/PostSharp.Engineering.BuildTools/Build/Bumping/BumpCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static bool Execute( BuildContext context, BumpSettings settings )
3333
return false;
3434
}
3535

36-
if ( !GitHelper.ConfigureCredentials( context ) )
36+
if ( !GitHelper.TryConfigureCredentials( context ) )
3737
{
3838
console.WriteError( "Cannot configure git credentials." );
3939

src/PostSharp.Engineering.BuildTools/Build/Publishing/PostPublishCommand.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,15 @@ internal class PostPublishCommand : BaseCommand<PublishSettings>
1919
public static bool Execute( BuildContext context, PublishSettings settings )
2020
{
2121
context.Console.WriteHeading( "Finishing publishing." );
22-
23-
GitHelper.ConfigureCredentials( context );
2422

25-
if ( !MasterGenerator.TryWriteFiles( context, settings ) )
23+
if ( !GitHelper.TryConfigureCredentials( context ) )
2624
{
2725
return false;
2826
}
2927

30-
if ( context.IsContinuousIntegrationBuild )
28+
if ( !MasterGenerator.TryWriteFiles( context, settings ) )
3129
{
32-
// When on TeamCity, Git user credentials are set to TeamCity.
33-
if ( !TeamCityHelper.TrySetGitIdentityCredentials( context ) )
34-
{
35-
return false;
36-
}
30+
return false;
3731
}
3832

3933
var product = context.Product;

src/PostSharp.Engineering.BuildTools/Build/Publishing/PrePublishCommand.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ public static bool Execute( BuildContext context, PublishSettings settings )
2121
{
2222
var product = context.Product;
2323

24-
GitHelper.ConfigureCredentials( context );
24+
if ( !GitHelper.TryConfigureCredentials( context ) )
25+
{
26+
return false;
27+
}
2528

2629
if ( !MasterGenerator.TryWriteFiles( context, settings ) )
2730
{
@@ -39,15 +42,6 @@ public static bool Execute( BuildContext context, PublishSettings settings )
3942
return false;
4043
}
4144

42-
if ( context.IsContinuousIntegrationBuild )
43-
{
44-
// When on TeamCity, Git user credentials are set to TeamCity.
45-
if ( !TeamCityHelper.TrySetGitIdentityCredentials( context ) )
46-
{
47-
return false;
48-
}
49-
}
50-
5145
var sourceBranch = context.Product.DependencyDefinition.Branch;
5246

5347
if ( context.Branch != sourceBranch )

src/PostSharp.Engineering.BuildTools/Build/Publishing/PublishCommand.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public static bool Execute( BuildContext context, PublishSettings settings )
9494
return false;
9595
}
9696

97-
if ( !GitHelper.ConfigureCredentials( context ) )
97+
if ( !GitHelper.TryConfigureCredentials( context ) )
9898
{
9999
context.Console.WriteError( "Cannot configure git credentials." );
100100

@@ -149,13 +149,9 @@ public static bool Execute( BuildContext context, PublishSettings settings )
149149
// For consolidated deployments, this is part of the post-deployment step.
150150
if ( !product.ProductFamily.HasConsolidatedBuild && !settings.IsStandalone )
151151
{
152-
if ( context.IsContinuousIntegrationBuild )
152+
if ( !GitHelper.TryConfigureCredentials( context ) )
153153
{
154-
// When on TeamCity, Git user credentials are set to TeamCity.
155-
if ( !TeamCityHelper.TrySetGitIdentityCredentials( context ) )
156-
{
157-
return false;
158-
}
154+
return false;
159155
}
160156

161157
if ( !GitIntegrationHelper.TryAddTagToLastCommit( context, settings ) )

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ param(
1313
[string]$BuildAgentPath = 'C:\BuildAgent',
1414
[switch]$LoadEnvFromKeyVault, # Forces loading environment variables form the key vault.
1515
[switch]$StartVsmon, # Enable the remote debugger.
16+
[switch]$KillAll, # Kill all containers before building the image.
1617
[Parameter(ValueFromRemainingArguments)]
1718
[string[]]$BuildArgs # Arguments passed to `Build.ps1` within the container.
1819
)
@@ -254,6 +255,12 @@ Write-Host "Volume mappings: " @VolumeMappings -ForegroundColor Gray
254255
Write-Host "Mount points: " $mountPointsAsString -ForegroundColor Gray
255256
Write-Host "Git directories: " $gitDirectoriesAsString -ForegroundColor Gray
256257

258+
# Kill all containers
259+
docker ps -q | ForEach-Object {
260+
Write-Host "Killing container
261+
docker rm -f $_
262+
}
263+
257264
# Building the image.
258265
if (-not $NoBuildImage)
259266
{
@@ -287,23 +294,23 @@ if (-not $BuildImage)
287294
if ($Interactive)
288295
{
289296
$pwshArgs = "-NoExit"
290-
$BuildArgs = @("-Interactive") + $BuildArgs
291-
$dockerArgs = @("-it")
297+
$BuildArgs = @("-Interactive") + $BuildArgs
298+
$dockerArgs = @("-it")
292299
}
293300
else
294301
{
295302
$pwshArgs = "-NonInteractive"
296-
$dockerArgs = @()
303+
$dockerArgs = @()
297304
}
298305
299306
$buildArgsString = $BuildArgs -join " "
300-
$VolumeMappingsAsString = $VolumeMappings -join " "
301-
$dockerArgsAsString = $dockerArgs -join " "
307+
$VolumeMappingsAsString = $VolumeMappings -join " "
308+
$dockerArgsAsString = $dockerArgs -join " "
302309
303310
304-
Write-Host "Executing: ``docker run --rm --memory=12g $VolumeMappingsAsString -w $SourceDirName $dockerArgsAsString $ImageName pwsh $pwshArgs -Command `"& .\Build.ps1 $buildArgsString`; exit `$LASTEXITCODE`"``." -ForegroundColor Cyan
311+
Write-Host "Executing: ``docker run --rm --memory= 12g $VolumeMappingsAsString -w $SourceDirName $dockerArgsAsString $ImageName pwsh $pwshArgs -Command `"& .\Build.ps1 $buildArgsString`; exit `$LASTEXITCODE`"``." -ForegroundColor Cyan
305312
306-
docker run --rm --memory=12g @VolumeMappings -w $SourceDirName @dockerArgs $ImageName pwsh $pwshArgs -Command "& .\Build.ps1 $buildArgsString`; exit `$LASTEXITCODE;"
313+
docker run --rm --memory=12g @VolumeMappings -w $SourceDirName @dockerArgs $ImageName pwsh $pwshArgs -Command "& .\Build.ps1 $buildArgsString`; exit `$LASTEXITCODE; "
307314
if ($LASTEXITCODE -ne 0)
308315
{
309316
Write-Host "Docker run (build) failed with exit code $LASTEXITCODE" -ForegroundColor Red

src/PostSharp.Engineering.BuildTools/Tools/Git/DownstreamMerge.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,9 @@ private static bool TryCheckPendingMerges( BuildContext context, BaseBuildSettin
135135
public static bool MergeDownstream( BuildContext context, DownstreamMergeSettings settings )
136136
{
137137
// When on TeamCity, Git user credentials are set to TeamCity.
138-
if ( context.IsContinuousIntegrationBuild )
138+
if ( !GitHelper.TryConfigureCredentials( context ) )
139139
{
140-
if ( !TeamCityHelper.TrySetGitIdentityCredentials( context ) )
141-
{
142-
return false;
143-
}
140+
return false;
144141
}
145142

146143
if ( !GitHelper.TryGetStatus( context, context.RepoDirectory, out var statuses ) )
@@ -364,7 +361,10 @@ private static bool TryMerge(
364361

365362
context.Console.WriteImportantMessage( $"Merging '{sourceBranch}' branch to '{targetBranch}' branch" );
366363

367-
GitHelper.ConfigureCredentials( context );
364+
if ( !GitHelper.TryConfigureCredentials( context ) )
365+
{
366+
return false;
367+
}
368368

369369
if ( !GitHelper.TryMerge( context, sourceBranch, targetBranch, "--no-commit --no-ff", true ) )
370370
{

src/PostSharp.Engineering.BuildTools/Tools/TeamCity/TeamCityHelper.cs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -92,34 +92,6 @@ public static string GetTeamCitySourceReadToken()
9292
return teamCitySourceReadToken;
9393
}
9494

95-
/// <summary>
96-
/// Attempts to set Git user credentials to TeamCity. Configurations are set only for the current repository.
97-
/// </summary>
98-
/// <param name="context"></param>
99-
/// <returns></returns>
100-
public static bool TrySetGitIdentityCredentials( BuildContext context )
101-
{
102-
if ( !ToolInvocationHelper.InvokeTool(
103-
context.Console,
104-
"git",
105-
"config user.name TeamCity",
106-
context.RepoDirectory ) )
107-
{
108-
return false;
109-
}
110-
111-
if ( !ToolInvocationHelper.InvokeTool(
112-
context.Console,
113-
"git",
114-
$"config user.email {TeamCityUsername}",
115-
context.RepoDirectory ) )
116-
{
117-
return false;
118-
}
119-
120-
return true;
121-
}
122-
12395
public static bool TriggerTeamCityBuild(
12496
BuildContext context,
12597
CommonCommandSettings settings,

src/PostSharp.Engineering.BuildTools/Utilities/GitHelper.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -604,15 +604,27 @@ public static bool TryGetLatestCommitDate( BuildContext context, [NotNullWhen( t
604604
return true;
605605
}
606606

607-
public static bool ConfigureCredentials( BuildContext context )
607+
private static bool _credentialsConfigured;
608+
609+
public static bool TryConfigureCredentials( BuildContext context )
608610
{
611+
if ( context is { IsRunningUnderContainer: false, IsContinuousIntegrationBuild: false } )
612+
{
613+
return true;
614+
}
615+
616+
if ( _credentialsConfigured )
617+
{
618+
return true;
619+
}
620+
609621
var console = context.Console;
610622
var environmentVariableName = context.Product.DependencyDefinition.VcsRepository.TokenEnvironmentVariableName;
611623

612-
console.WriteMessage( "Configuring git credentials." );
613-
614624
if ( RuntimeInformation.IsOSPlatform( OSPlatform.Windows ) )
615625
{
626+
console.WriteMessage( $"Configuring git credentials from {environmentVariableName}." );
627+
616628
var environmentVariableValue = Environment.GetEnvironmentVariable( environmentVariableName );
617629

618630
if ( string.IsNullOrEmpty( environmentVariableValue ) )
@@ -640,6 +652,8 @@ public static bool ConfigureCredentials( BuildContext context )
640652
throw new PlatformNotSupportedException();
641653
}
642654

655+
_credentialsConfigured = true;
656+
643657
return true;
644658
}
645659
}

0 commit comments

Comments
 (0)