public static BuildTargetResult CommitChanges(BuildTargetContext c) { Cmd("git", "add", ".") .Execute() .EnsureSuccessful(); string userName = s_config.UserName; string email = s_config.Email; Cmd("git", "commit", "-m", PullRequestTitle, "--author", $"{userName} <{email}>") .EnvironmentVariable("GIT_COMMITTER_NAME", userName) .EnvironmentVariable("GIT_COMMITTER_EMAIL", email) .Execute() .EnsureSuccessful(); string remoteUrl = $"github.com/{s_config.GitHubOriginOwner}/{s_config.GitHubProject}.git"; string remoteBranchName = $"UpdateDependencies{DateTime.UtcNow.ToString("yyyyMMddhhmmss")}"; string refSpec = $"HEAD:refs/heads/{remoteBranchName}"; string logMessage = $"git push https://{remoteUrl} {refSpec}"; BuildReporter.BeginSection("EXEC", logMessage); CommandResult pushResult = Cmd("git", "push", $"https://{userName}:{s_config.Password}@{remoteUrl}", refSpec) .QuietBuildReporter() // we don't want secrets showing up in our logs .CaptureStdErr() // git push will write to StdErr upon success, disable that .CaptureStdOut() .Execute(); var message = logMessage + $" exited with {pushResult.ExitCode}"; if (pushResult.ExitCode == 0) { BuildReporter.EndSection("EXEC", message.Green(), success: true); } else { BuildReporter.EndSection("EXEC", message.Red().Bold(), success: false); } pushResult.EnsureSuccessful(suppressOutput: true); c.SetRemoteBranchName(remoteBranchName); return(c.Success()); }
public static BuildTargetResult CommitChanges(BuildTargetContext c) { CommandResult statusResult = Cmd("git", "status", "--porcelain") .CaptureStdOut() .Execute(); statusResult.EnsureSuccessful(); bool hasModifiedFiles = !string.IsNullOrWhiteSpace(statusResult.StdOut); bool hasUpdatedDependencies = c.GetDependencyInfos().Where(d => d.IsUpdated).Any(); if (hasModifiedFiles != hasUpdatedDependencies) { return(c.Failed($"'git status' does not match DependencyInfo information. Git has modified files: {hasModifiedFiles}. DependencyInfo is updated: {hasUpdatedDependencies}.")); } if (!hasUpdatedDependencies) { c.Warn("Dependencies are currently up to date"); return(c.Success()); } string userName = s_config.UserName; string email = s_config.Email; string commitMessage = GetCommitMessage(c); Cmd("git", "commit", "-a", "-m", commitMessage, "--author", $"{userName} <{email}>") .EnvironmentVariable("GIT_COMMITTER_NAME", userName) .EnvironmentVariable("GIT_COMMITTER_EMAIL", email) .Execute() .EnsureSuccessful(); string remoteUrl = $"github.com/{s_config.GitHubOriginOwner}/{s_config.GitHubProject}.git"; string remoteBranchName = $"UpdateDependencies{DateTime.UtcNow.ToString("yyyyMMddhhmmss")}"; string refSpec = $"HEAD:refs/heads/{remoteBranchName}"; string logMessage = $"git push https://{remoteUrl} {refSpec}"; BuildReporter.BeginSection("EXEC", logMessage); CommandResult pushResult = Cmd("git", "push", $"https://{userName}:{s_config.Password}@{remoteUrl}", refSpec) .QuietBuildReporter() // we don't want secrets showing up in our logs .CaptureStdErr() // git push will write to StdErr upon success, disable that .CaptureStdOut() .Execute(); var message = logMessage + $" exited with {pushResult.ExitCode}"; if (pushResult.ExitCode == 0) { BuildReporter.EndSection("EXEC", message.Green(), success: true); } else { BuildReporter.EndSection("EXEC", message.Red().Bold(), success: false); } pushResult.EnsureSuccessful(suppressOutput: true); c.SetRemoteBranchName(remoteBranchName); return(c.Success()); }