Shall Git push be blocked when Jenkins build failed?

hello,

If a user pushed a piece of wrong code to remote gitea repo (e.g.:“git push -u origin main”) which will make Jenkins build failure. Ideally the jenkins shall block the user`s "push " to the remote gitea repo and rollback to the last HEAD.

to achive that, in the Jenkins http://localhost:8080/job/FreeType/configure menu, I selected “Push Only If Build Succeeds” in order to block the user push after a build failure.

But i checked the remote Gitea repo, the wrong code is still pushed to the gitea remote repo. and looks Jenkins does not block the wrong “push”.

What shall i do ? shall i add git commands(e.g.: git reset HEAD~1) to delete the wrong commit in Jenkins build steps ?

“Push Only If Build Succeeds” Is an option of the ”Git publisher” feature https://plugins.jenkins.io/git/#plugin-content-git-publisher that pushes from the Jenkins workspace to the remote repository. It is not intended for rolling back pushes that users gave already done.

Instead, consider requiring each developer to push to a feature branch and create a merge request (in GitLab) or pull request (in GitHub or Gitea) from the feature branch to the main branch. Jenkins can then build the code on the feature branch and tell the Git hosting software whether it is OK.

Gitea may have a feature that lets you prevent pull requests from being merged before Jenkins has checked them. In GitLab, How to use external status checks for merge requests seems to be the corresponding feature.