Skip to content

Commit

Permalink
Release pipeline fail on Azure Git repo with LFS (#4038)
Browse files Browse the repository at this point in the history
* Added logic from Agent.Plugins.Repository.GitSourceProvider and Agent.Plugins.Repository.GitCliManager

* Added force tag for fetch

* Added GIT_LFS_SKIP_SMUDGE param

* Rollback not connected changes

* Test fixed

* Added debug output

* Rollback not connected changes

* Rollback not connected changes

* Rollback not connected changes

* Removed extra call
  • Loading branch information
Roman-Shchukin authored Nov 9, 2022
1 parent aef8296 commit f68f178
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
32 changes: 21 additions & 11 deletions src/Agent.Worker/Build/GitCommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public interface IGitCommandManager : IAgentService
bool EnsureGitLFSVersion(Version requiredVersion, bool throwOnNotMatch);

// setup git execution info, git location, version, useragent, execpath
Task LoadGitExecutionInfo(IExecutionContext context, bool useBuiltInGit);
Task LoadGitExecutionInfo(IExecutionContext context, bool useBuiltInGit, Dictionary<string, string> gitEnv = null);

// git init <LocalDir>
Task<int> GitInit(IExecutionContext context, string repositoryPath);
Expand Down Expand Up @@ -121,6 +121,11 @@ private static Encoding _encoding
private string _gitLfsPath = null;
private Version _gitLfsVersion = null;

private Dictionary<string, string> _gitEnv = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
{ "GIT_TERMINAL_PROMPT", "0" },
};

public bool EnsureGitVersion(Version requiredVersion, bool throwOnNotMatch)
{
ArgUtil.NotNull(_gitVersion, nameof(_gitVersion));
Expand Down Expand Up @@ -155,8 +160,19 @@ public bool EnsureGitLFSVersion(Version requiredVersion, bool throwOnNotMatch)
return _gitLfsVersion >= requiredVersion;
}

public async Task LoadGitExecutionInfo(IExecutionContext context, bool useBuiltInGit)
public async Task LoadGitExecutionInfo(IExecutionContext context, bool useBuiltInGit, Dictionary<string, string> gitEnv = null)
{
if (gitEnv != null)
{
foreach (var env in gitEnv)
{
if (!string.IsNullOrEmpty(env.Key))
{
_gitEnv[env.Key] = env.Value ?? string.Empty;
}
}
}

// Resolve the location of git.
if (useBuiltInGit)
{
Expand Down Expand Up @@ -256,7 +272,6 @@ public async Task<int> GitFetch(IExecutionContext context, string repositoryPath
//define options for fetch
string options = $"{tags} --prune {pruneTags} --progress --no-recurse-submodules {remoteName} {depth} {string.Join(" ", refSpec)}";


return await ExecuteGitCommandAsync(context, repositoryPath, "fetch", options, additionalCommandLine, cancellationToken);
}

Expand Down Expand Up @@ -727,14 +742,9 @@ private async Task<int> ExecuteGitCommandAsync(IExecutionContext context, string

private IDictionary<string, string> GetGitEnvironmentVariables(IExecutionContext context)
{
Dictionary<string, string> gitEnv = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
{ "GIT_TERMINAL_PROMPT", "0" },
};

if (!string.IsNullOrEmpty(_gitHttpUserAgentEnv))
{
gitEnv["GIT_HTTP_USER_AGENT"] = _gitHttpUserAgentEnv;
_gitEnv["GIT_HTTP_USER_AGENT"] = _gitHttpUserAgentEnv;
}

// Add the public variables.
Expand All @@ -755,10 +765,10 @@ private IDictionary<string, string> GetGitEnvironmentVariables(IExecutionContext
continue;
}

gitEnv[formattedKey] = pair.Value ?? string.Empty;
_gitEnv[formattedKey] = pair.Value ?? string.Empty;
}

return gitEnv;
return _gitEnv;
}
}
}
12 changes: 11 additions & 1 deletion src/Agent.Worker/Build/GitSourceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,19 @@ public async Task GetSourceAsync(

bool gitUseSecureParameterPassing = AgentKnobs.GitUseSecureParameterPassing.GetValue(executionContext).AsBoolean();

Dictionary<string, string> gitEnv = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

// Git-lfs will try to pull down asset if any of the local/user/system setting exist.
// If customer didn't enable `LFS` in their pipeline definition, we will use ENV to disable LFS fetch/checkout.
if (!gitLfsSupport)
{
gitEnv["GIT_LFS_SKIP_SMUDGE"] = "1";
executionContext.Debug("GIT_LFS_SKIP_SMUDGE variable set to 1");
}

// Initialize git command manager
_gitCommandManager = HostContext.GetService<IGitCommandManager>();
await _gitCommandManager.LoadGitExecutionInfo(executionContext, useBuiltInGit: !preferGitFromPath);
await _gitCommandManager.LoadGitExecutionInfo(executionContext, useBuiltInGit: !preferGitFromPath, gitEnv);

// Make sure the build machine met all requirements for the git repository
// For now, the requirement we have are:
Expand Down
2 changes: 1 addition & 1 deletion src/Test/L0/Worker/Build/GitSourceProviderL0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private Mock<IGitCommandManager> GetDefaultGitCommandMock()
.Setup(x => x.EnsureGitVersion(It.IsAny<Version>(), It.IsAny<bool>()))
.Returns(true);
_gitCommandManager
.Setup(x => x.LoadGitExecutionInfo(It.IsAny<IExecutionContext>(), It.IsAny<bool>()))
.Setup(x => x.LoadGitExecutionInfo(It.IsAny<IExecutionContext>(), It.IsAny<bool>(), null))
.Returns(Task.CompletedTask);
_gitCommandManager
.Setup(x => x.GitInit(It.IsAny<IExecutionContext>(), It.IsAny<string>()))
Expand Down

0 comments on commit f68f178

Please sign in to comment.