Skip to content

fix: fixing force push for gitops #5152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pkg/deployment/gitOps/git/GitOpsHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,19 @@ func (impl *GitOpsHelper) Pull(repoRoot string) (err error) {
return impl.gitCommandManager.Pull(ctx, repoRoot)
}

const PushErrorMessage = "failed to push some refs"

func (impl GitOpsHelper) CommitAndPushAllChanges(repoRoot, commitMsg, name, emailId string) (commitHash string, err error) {
start := time.Now()
defer func() {
util.TriggerGitOpsMetrics("CommitAndPushAllChanges", "GitService", start, err)
}()
ctx := git.BuildGitContext(context.Background()).WithCredentials(impl.Auth)
return impl.gitCommandManager.CommitAndPush(ctx, repoRoot, commitMsg, name, emailId)
commitHash, err = impl.gitCommandManager.CommitAndPush(ctx, repoRoot, commitMsg, name, emailId)
if err != nil && strings.Contains(err.Error(), PushErrorMessage) {
return commitHash, fmt.Errorf("%s %v", "push failed due to conflicts", err)
}
return commitHash, nil
}

func (impl *GitOpsHelper) pullFromBranch(ctx git.GitContext, rootDir string) (string, string, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (impl *GitCliManagerImpl) add(ctx GitContext, rootDir string) (response, er

func (impl *GitCliManagerImpl) push(ctx GitContext, rootDir string) (response, errMsg string, err error) {
impl.logger.Debugw("git push ", "location", rootDir)
cmd, cancel := impl.createCmdWithContext(ctx, "git", "-C", rootDir, "push", "origin", "master", "--force")
cmd, cancel := impl.createCmdWithContext(ctx, "git", "-C", rootDir, "push", "origin", "master")
defer cancel()
output, errMsg, err := impl.runCommandWithCred(cmd, ctx.auth)
impl.logger.Debugw("git add output", "root", rootDir, "opt", output, "errMsg", errMsg, "error", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (impl *GitManagerBaseImpl) runCommand(cmd *exec.Cmd) (response, errMsg stri
if err != nil {
exErr, ok := err.(*exec.ExitError)
if !ok {
return "", "", err
return "", "", fmt.Errorf("%s %v", outBytes, err)
}
errOutput := string(exErr.Stderr)
return "", errOutput, err
Expand Down