Jenkins Git Plugin not pulling latest changes before building job
Solution 1
I believe Jenkins pulls the changes and builds in it's own tmp directory. So, your repository directory isn't getting updated although Jenkins is properly building the new code in it's own sandbox.
My solution to this has been to add a "git pull" step in my build process like so:
When a new commit is delivered to my GitHub repo:
1. Build my project
If successful, perform the following post-build steps:
1. Execute Shell:
cd /your/repo/directory/
git pull
You can obviously modify the "git pull" command to do whatever you need to do if a 'pull' doesn't work for you.
Solution 2
Relates me to scenario where workspace wasn't getting cleaned-up, used:
Source Code Management--> Additional Behaviours --> Clean after checkout
Other option is to use Workspace Cleanup Plugin
Solution 3
For folks using Jenkins pipeline with git plugin, use Wipe Out repository & force clone
under Additional Behaviours
of SCM
section.
Solution 4
I know the question is old, but there is another way to do this. In the Build Environment section, select "Delete workspace before build starts"
See the screenshot below,
This will actually clean the workspace every time and hence you will get the updated code.
Solution 5
Try to insert your branch path with this format:
refs/remotes/<remoteRepoName>/<branchName>
Tracks/checks out the specified branch.
E.g. refs/remotes/origin/master
polarice
Updated on July 09, 2022Comments
-
polarice almost 2 years
I am working with Jenkins CI and am trying to properly configure my jobs to use git.
I have the git plugin installed and configured for one of my jobs. When I build the job, I expect it to pull the latest changes for the branch I specify and then continue with the rest of the build process (e.g., unit tests, etc.).
When I look at the console output, I see
> git fetch --tags --progress ssh://gerrit@git-dev/Util +refs/heads/*:refs/remotes/origin/* > git rev-parse origin/some_branch^{commit} Checking out Revision <latest_SHA1> (origin/some_branch) > git config core.sparsecheckout > git checkout -f <latest_SHA1> > git rev-list <latest_SHA1>
I see that the plugin fetches and checks out the proper commit hash, but when the tests run it seems as though the repo wasn't updated at all. If I go into the repository in Jenkins, I see there that the latest changes were never pulled.
Shouldn't it pull before it tries to build?
I have git 1.8.5 installed on my Jenkins machine, which is a recommended version. https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin
After checking other similar sounding questions on SO, their answers weren't helpful for my problem.
-
HRVHackers about 8 yearsThank you, that got me on the right track. What ended up working for me was: git pull -s recursive -X theirs origin myBranch
-
mhristache over 7 yearsThis is actually the correct way to do it. So I am wondering why this answer is not the selected one.
-
Abhijeet almost 7 yearsThis option is available on 2.6.
-
Cyral about 6 yearsThis is in 'Build triggers' > 'Pipeline' > 'Additional Behaviours' for me
-
hiaclibe about 6 yearsSee also solution from Abhijeet.
-
Shafiq al-Shaar about 5 yearsI've been trying to solve this issue for 2 days. This made it work. I have a features branch
feature/xx
that Jenkins kept getting old commits instead of the latest one. -
alphaguy almost 5 yearsWhere did you include the git pull in the jenkins configurations ? I am confused please help me out.
-
Mostafa Lavaei about 4 yearsIt will remove all untracked/ignored files too. Is there a way to only reset tracked files?
-
trebor over 3 yearsThis requires the Workspace Cleanup Plugin - plugins.jenkins.io/ws-cleanup.
-
trebor over 3 yearsThis requires the Workspace Cleanup Plugin - plugins.jenkins.io/ws-cleanup.
-
Alexander Samoylov almost 3 yearsFor me (in a scripted pipeline case) the workaround was to add "checkout scm" call, without that the workspace and the "Recent Changes" were not updated. The drawback seems to be the workspace cleaning which is run by default in this case (as I see from the log). Just pulling seems to be not implemented yet, I found a ticket about this problem: issues.jenkins.io/browse/JENKINS-52916.