Try the following code; it removes the need for IGenericBuildMasterContext altogether and uses an async method to replace the Task.Run call:
public override async Task ExecuteAsync(IOperationExecutionContext context)
{
// Clone Repo to directory
CloneOptions co = new CloneOptions();
co.CredentialsProvider = (_url, _user, _cred) => new LibGit2Sharp.SecureUsernamePasswordCredentials { Username = Username, Password = Password };
var path = context.ResolvePath(CloneTo);
path = Repository.Clone(Path.Combine(BaseUrl, RepositoryName), path, co);
// Get Commit hash
var repo = new Repository(path);
var commitHash = repo.Head.Tip.Sha;
// Get local + remote branch names
var localBranchName = "refs/heads/" + Branch;
var remoteBranchName = "refs/remotes/origin/" + Branch;
Log(new SimpleLogMessage(Inedo.Diagnostics.MessageLevel.Debug, $"Local Branch set to {localBranchName}", "Git", "", context));
Log(new SimpleLogMessage(Inedo.Diagnostics.MessageLevel.Debug, $"Remote Branch set to {remoteBranchName}", "Git", "", context));
// Check the remote branch exists
var trackingBranch = repo.Branches[remoteBranchName];
if (trackingBranch == null)
{
Log(new SimpleLogMessage(Inedo.Diagnostics.MessageLevel.Error, $"Branch {Branch} does not exist on the remote.", "Git", "", context));
throw new Exception($"Branch {Branch} does not exist on remote");
}
// Checkout branch
Log(new SimpleLogMessage(Inedo.Diagnostics.MessageLevel.Debug, $"Creating Branch {Branch} Locally", "Git", "", context));
Branch localBranch;
try
{
localBranch = repo.CreateBranch(Branch, trackingBranch.Tip);
}
catch
{
// Branch already exists - set it up
localBranch = repo.Branches[localBranchName];
}
Log(new SimpleLogMessage(Inedo.Diagnostics.MessageLevel.Debug, "Tracking remote branch", "Git", "", context));
repo.Branches.Update(localBranch, b => b.TrackedBranch = trackingBranch.CanonicalName);
Log(new SimpleLogMessage(Inedo.Diagnostics.MessageLevel.Debug, "Checking out branch", "Git", "", context));
Commands.Checkout(repo, Branch);
Log(new SimpleLogMessage(Inedo.Diagnostics.MessageLevel.Debug, $"Cloned branch {Branch} Successfully", "Git", "", context));
// If hash is specified, checkout that
if (!string.IsNullOrWhiteSpace(Commit))
{
Log(new SimpleLogMessage(Inedo.Diagnostics.MessageLevel.Debug, $"Commit Hash set. Attempting to checkout commit hash {Commit}", "Git", "", context));
repo.Reset(ResetMode.Hard, Commit);
}
// Make sure the commit hash is set correctly
commitHash = repo.Head.Tip.Sha;
Log(new SimpleLogMessage(Inedo.Diagnostics.MessageLevel.Information, $"Cloned Successfully: Hash is {commitHash}", "Git", "", context));
// Set commit hash
if (String.IsNullOrWhiteSpace(VariableName))
{
Log(new SimpleLogMessage(Inedo.Diagnostics.MessageLevel.Information, "No Commit Hash Variable Specified. Not setting.", "Git", "", context));
}
else
{
using (var db = new DB.Context())
{
var exec = await db.Builds_GetExecutionAsync(context.ExecutionId);
await db.Variables_CreateOrUpdatePackageVariableAsync(
Build_Id: exec.Build_Id,
Variable_Name: VariableName,
ValueType_Code: Domains.VariableValueType.Scalar,
Variable_Value: InedoLib.UTF8Encoding.GetBytes(commitHash),
Sensitive_Indicator: false,
EvaluateVariables_Indicator: false
);
Log(new SimpleLogMessage(Inedo.Diagnostics.MessageLevel.Information, $"Assigned commit hash {commitHash} to variable {VariableName}", "Git", "", context));
}
}
//Delete the .git Directory
repo.Dispose();
Helpers.DirectoryHelper.DeleteReadOnlyDirectory(path);
Helpers.DirectoryHelper.DeleteReadOnlyFile(context.ResolvePath(".gitignore"));
Log(new SimpleLogMessage(Inedo.Diagnostics.MessageLevel.Debug, "Removed .git directory and .gitignore", "Git", "", context));
}
However, I'm guessing this was written a while ago before the built-in Git operations handled this? We use the Git operations to perform this same functionality by combining two operations:
# Gets source from the BuildMaster repository and creates a release package variable named GitCommit
Git::Git-GetSource
(
Credentials: GitLab,
RepositoryUrl: https://gitlab.com/inedo/BuildMaster.git,
DiskPath: ~\Src,
RecurseSubmodules: true,
Branch: $Branch,
CommitHash => $commit
);
Set-ReleaseVariable GitCommit
(
Value: $commit,
Package: $PackageNumber
);