-
Bug
-
Resolution: Unresolved
-
Major
-
Powered by SuggestiMate
In current release version prune works after fetch. (yeah, I saw this bugfix https://issues.jenkins-ci.org/browse/JENKINS-10348)
But here the bad situation:
1) Create "abc" branch in git repo.
2) Build this branch by Jenkins.
3) Abandon commit and delete branch.
4) Create "abc/asd" branch, trying to build it... and we got a problem.
UPD: my bad, now I see what happens with Prune in refactoring branch. But the problem is still there - Prune executes after fetch, not before it.
[JENKINS-18834] Initial git fetch ignore prune settings
Code changed in jenkins
User: Sergey Mylnikov
Path:
src/main/java/hudson/plugins/git/extensions/impl/PruneStaleBranch.java
http://jenkins-ci.org/commit/git-plugin/4f48d8e661a20ee7a676fec1d1a09077d63694f6
Log:
[FIXED JENKINS-18834] moving Prune before Fetch
This problematic behavior is still present in Jenkins 1.596 with git plugin version 2.3.4.
I see a git fetch <repo> --prune which is not sufficient.
There should be something along the lines of "git remote prune origin" BEFORE the fetch.
packadal can you describe the sequence of steps which cause a problem for you?
the sequence is the exact same as described in the bug.
1) create brach foo
- Jenkins build
2) delete branch foo
3) create branch foo/bar - Jenkins build
/!\ Problem !
This is because the git remote prune is not performed before the fetch.
Adding the "prune stale remote-tracking branches" does not perfor the prune before, but seems to make a git fetch --prune, which does not fix the problem.
Can confirm this is still an issue. If the git remote prune was performed before the fetch, this wouldn't be a problem.
Following the same steps as packadal, I get this error in the console.
OpenShift Build my-project/deploy-my-service-1786 from ssh://git@bitbucket.example.com:7999/ui/my-service.git Checking out git ssh://git@bitbucket.example.com:7999/ui/my-service.git into /var/lib/jenkins/jobs/my-project/jobs/my-project-deploy-my-service/workspace@script to read Jenkinsfile using credential my-project-bitbucket-jenkins-user > git rev-parse --is-inside-work-tree # timeout=10 Fetching changes from the remote Git repository > git config remote.origin.url ssh://git@bitbucket.example.com:7999/ui/my-service.git # timeout=10 Fetching upstream changes from ssh://git@bitbucket.example.com:7999/ui/my-service.git > git --version # timeout=10 using GIT_SSH to set credentials my-project-bitbucket-jenkins-user > git fetch --tags --progress ssh://git@bitbucket.example.com:7999/ui/my-service.git +refs/heads/*:refs/remotes/origin/* ERROR: Error fetching remote repo 'origin' hudson.plugins.git.GitException: Failed to fetch from ssh://git@bitbucket.example.com:7999/ui/my-service.git at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:894) at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1161) at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1192) at org.jenkinsci.plugins.workflow.steps.scm.SCMStep.checkout(SCMStep.java:120) at org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition.create(CpsScmFlowDefinition.java:144) at org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition.create(CpsScmFlowDefinition.java:67) at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:293) at hudson.model.ResourceController.execute(ResourceController.java:97) at hudson.model.Executor.run(Executor.java:429) Caused by: hudson.plugins.git.GitException: Command "git fetch --tags --progress ssh://git@bitbucket.example.com:7999/ui/my-service.git +refs/heads/*:refs/remotes/origin/*" returned status code 1: stdout: stderr: From ssh://bitbucket.example.com:7999/ui/my-service * [new branch] automation/testpi-9 -> origin/automation/testpi-9 error: 'refs/remotes/origin/feature/foo' exists; cannot create 'refs/remotes/origin/feature/foo/bar' ! [new branch] feature/foo/bar -> origin/feature/foo/bar (unable to update local ref) error: some local refs could not be updated; try running 'git remote prune ssh://git@bitbucket.example.com:7999/ui/my-service.git' to remove any old, conflicting branches at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2042) at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:1761) at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.access$400(CliGitAPIImpl.java:72) at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(CliGitAPIImpl.java:442) at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:892) ... 8 more ERROR: Error fetching remote repo 'origin' ERROR: Maximum checkout retry attempts reached, aborting Finished: FAILURE
etoews that surprises me, since the --prune argument is added to the git fetch command which is used to fetch the latest updates from the remote repository.
Since the command in the output you provided does not include the --prune argument, that must indicate that the initial fetch is not honoring the prune argument. There are (unfortunately) two calls to git fetch when the git plugin retrieves changes from the remote repository. The second fetch is likely the one that would include the --prune argument. A future optimization will be needed to remove that extra call to git fetch or a specific fix for this bug to cause that initial fetch to include the --prune argument.
You might also try adding the "Honor refspecs in initial clone" option that is available in the "Advanced clone options". It might have a side-effect of including the --prune argument in the first fetch of the repository.
I've written an automated test that confirms the bug exists.
markewaite Thanks a lot for taking the time to confirm the bug and write a test.
I should also have mentioned my current versions. They are:
- Jenkins: 2.176.2
- Git: 3.10.0
- Git client: 2.7.7
Unfortunately, specifying any Additional Behaviours on a pipeline doesn't work for us. We're using Jenkins with OpenShift and it automatically overwrites any such config changes.
I see JGit referenced in a lot of issues relating to pruning. Is a possible workaround simply using the JGit plugin?
I would be surprised if JGit resolved it, but it is easy to check. Follow the instructions to enable JGit, then add
gitTool: 'jgit'
to the checkout scm., The Pipeline syntax helper in Jenkins will suggest the correct configuration once you've enabled JGit.
Over 3 years later, and this is still an outstanding problem/bug.
I tried using the preSCMbuildstep plugin with the following job DSL to deal with the issue:
wrappers {
preScmSteps {
steps {
shell('test -d .git && git remote prune origin')
}
}
}
Unfortunately, we don't have the git credentials available that are defined for this job, so this won't work.
Using this "works" but is really unpleasant for larger repos:
scm { git { remote { # ... extensions { pruneBranches() pruneStaleBranch() wipeWorkspace() } } } }
bugfix for refactoring branch:
https://github.com/jenkinsci/git-plugin/pull/161
(https://github.com/sapone/git-plugin/commit/e78f74a18423840ded26d720cf348496da7b4600)