Jenkinsfile SCM Checkout stage, how to match remote pull request head with local build head

1,042 views
Skip to first unread message

Amila Gunathilake

unread,
Feb 18, 2020, 8:09:43 PM2/18/20
to Jenkins Users

Hi all,

I'm having the below issue.

I was change my code to use GIT Reference repository in Jenkinsfile code such as below.


image-2020-02-18-04-21-49-733.png



So I made a new pull request in GIT-HUB for the purpose of merge these changes into my master branch.

But when the Pull-Request running I saw in my builder machine (workspace) its not downloading (fetching) the relevant Remote Pull-Request Head/Branch/ because in my workspace (builder-machine) local branch always checkout with the master branch and fetching from the master branch and not checkout for the correct Pull Request Head. 

image-2020-02-18-04-44-45-417.png



eg: Above screenshot is the way that my local workspace's .git > HEAD file looks like in the Pull Request I created after above code changes. And as you can see in the HEAD file it's automatically checkout in to the master branch instead of checking out into the relevant Pull-Request hash (tag). 

  • But below is the correct screenshot I captured from any of other Pull Requests, which should be the correct way to show the checkout branch or the HEAD.

image-2020-02-18-04-46-58-927.png


I hope you will understand my situation here? Please let me know if you guys need further information.

I throughly suggest that I want some code to replace above Yellow color highlighted sections. 

So please could anyone please give some advice regarding this issue ?  


Mark Waite

unread,
Feb 19, 2020, 8:39:30 PM2/19/20
to Jenkins Users
The value of 'branches' that you're using is incorrect.  In the triggers section you're declaring that if the env.BRANCH_NAME is master, then perform builds on a specific schedule.  Use the same env.BRANCH_NAME in the branches section by changing from:

branches: [[name: 'origin/* ]]

to:

branches: [[name: env.BRANCH_NAME]]

If the env.BRANCH_NAME is defined for the trigger case, then it should also be defined and available to use when defining the branch name.

The duplicate value of refspec will likely prevent the checkout from detecting any changes in your repository.  Change from:

refspec: "+refs/heads/*:refs/remotes/origin/*",
refspec: "+refs/heads/pr/*:refs/remotes/origin/pr/*",
 
to:

refspec: "+refs/heads/*:refs/remotes/origin/* +refs/heads/pr/*:refs/remotes/origin/pr/*",
 
The syntax you're using provides a value for refspec then replaces it with a different value for refspec.  The corrected syntax provides the default refspec plus the "pr" namespace refspec as a single argument so that both will be used.

Mark Waite

Amila Gunathilake

unread,
Feb 20, 2020, 2:11:27 AM2/20/20
to Jenkins Users


Hi Mark,

After I did your changes Jenkins log/console-output gives below error message.  

100Capture.PNG


101Capture.PNG


So as per the above error after your requested changes its not downloading code from remote repo.  Please see below screenshots of Builder node's workspace folder, which you can have any idea. 
 102Capture.PNG



In above Workspace folder as you can see there are no any other my remote repository files, only having the .git folder.  Which is because of the above error Couldn't find any revision to build


I think Couldn't find any revision to build error coming from after we changed branch name into "branches: [[name: env.BRANCH_NAME]]"

Please suggest any other ideas.  

Thanks
Amila

Amila Gunathilake

unread,
Feb 20, 2020, 2:21:26 AM2/20/20
to Jenkins Users

101Capture.PNG



Hi Mark,

After I did your changes Jenkins log/console-output gives below error message.  
100Capture.PNG

101Capture.PNG



So as per the above error after your requested changes its not downloading code from remote repo.  Please see below screenshots of Builder node's workspace folder, which you can have any idea. 
 

102Capture.PNG




In above Workspace folder as you can see there are no any other my remote repository files, only having the .git folder.  Which is because of the above error Couldn't find any revision to build


I think Couldn't find any revision to build error coming from after we changed branch name into "branches: [[name: env.BRANCH_NAME]]" 

Please suggest any other ideas.  

Thanks
Amila


Mark Waite

unread,
Feb 21, 2020, 12:00:49 AM2/21/20
to Jenkins Users
As far as I can tell, you want to:
  • Use declarative pipeline for clarity of the pipeline
  • Skip the default checkout so that you can tune the performance of checkout
  • Perform your own checkout using a reference repository
  • Use the same Jenkinsfile on branches and on pull requests so that pull requests can be merged into target branches easily
  • Checkout in the workspace to a branch with the same name as the branch on the git server
Based on those assumptions, I created a sample job and a pull request that targets the master branch and meets those requirements.  When a pull request is being evaluated, the changes on the pull request are detected and used.  When the master branch is being evaluated, its changes are detected and used.

Please refer to the sample job and the techniques used in the sample job.  Note that you will need the most recent release of the git plugin for references to the scm variable.

Mark Waite

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/d69b5c1c-e4f8-42e4-ba75-084ada0b653a%40googlegroups.com.

Amila Gunathilake

unread,
Feb 24, 2020, 9:04:27 AM2/24/20
to Jenkins Users


Hi Mark,

Thanks for your reply and your sample job really helped me.  Here my New Jenkinsfile checkout stage looks like. 

Capture.PNG

I had successfully build the Pull Request this time, when I'm building the PR below is the Console Output log I had from Jenkins.  

Capture.PNG






But after I merged my Pull Request into the master branch I'm just getting below Console output for checkout stage without the message "using Reference Repository"

Also after merge into master branch and after building in master branch "
alternates" file also missing from the workspace path - 'workspace\master411813f6\.git\objects\info

is it everything okay?   I cannot find weather reference repository is using or not ?  I'm 100% sure that my reference repository exists in build server. 

Thanks
Amila



On Friday, February 21, 2020 at 12:00:49 AM UTC-5, Mark Waite wrote:
As far as I can tell, you want to:
  • Use declarative pipeline for clarity of the pipeline
  • Skip the default checkout so that you can tune the performance of checkout
  • Perform your own checkout using a reference repository
  • Use the same Jenkinsfile on branches and on pull requests so that pull requests can be merged into target branches easily
  • Checkout in the workspace to a branch with the same name as the branch on the git server
Based on those assumptions, I created a sample job and a pull request that targets the master branch and meets those requirements.  When a pull request is being evaluated, the changes on the pull request are detected and used.  When the master branch is being evaluated, its changes are detected and used.

Please refer to the sample job and the techniques used in the sample job.  Note that you will need the most recent release of the git plugin for references to the scm variable.

Mark Waite

To unsubscribe from this group and stop receiving emails from it, send an email to jenkins...@googlegroups.com.

Amila Gunathilake

unread,
Feb 24, 2020, 9:18:04 AM2/24/20
to Jenkins Users


Hi Mark,

This is the new master branch Jenkins Console output I'm getting.  There is no message says that I'm using GIT reference repository like-wise my previous console output in Pull Request. 

Capture.PNG



So what will be the reason for this ?  Is everything okay with this console out-put ? 


Thanks,
Amila

Mark Waite

unread,
Feb 24, 2020, 10:17:07 PM2/24/20
to Jenkins Users
You can confirm that a reference repository is being used by looking for the file .git/objects/info/alternates in the repository.  

The file in my sample repository includes the text:

 /home/mwaite/git/bare/jenkins/jenkins.git/objects

To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/f5630876-867e-4b30-9de5-1d28fa8f8eec%40googlegroups.com.

Amila Gunathilake

unread,
Feb 25, 2020, 3:03:34 AM2/25/20
to Jenkins Users
Hi Mark,

Actually where I can find the file you mention "/home/mwaite/git/bare/jenkins/jenkins.git/objects"  in your repository MarkEWaite/jenkins-bugs ?

Finally I able to get use the GIT Reference repository in my builder machine.   But to get use that I had to replace my old Jenkins Configurations by giving the GIT reference repository path as below. 

Capture.PNG



 It's really surprised me because, as above screenshot when I include that reference-path configuration in Jenkins GUI only it starting to getting use my Jenkinsfile's  SCM checkout stage code configurations. 


Still I have a small issue as you can see below Jenkins console-output it says "
using Shallow fetch with depth 302 times.  (previously it was only one time. So I'm afraid that Jenkins uses both configurations such as Jenkins GUI and Jenkinsfile code configurations here. Is it okay since it may not do any harm to my builds ? 

Thanks again for your quick feedbacks Mr Mark,
Amila


On Monday, February 24, 2020 at 10:17:07 PM UTC-5, Mark Waite wrote:
You can confirm that a reference repository is being used by looking for the file .git/objects/info/alternates in the repository.  

The file in my sample repository includes the text:

 /home/mwaite/git/bare/jenkins/jenkins.git/objects

Amila Gunathilake

unread,
Feb 25, 2020, 3:06:26 AM2/25/20
to Jenkins Users


On Tuesday, February 25, 2020 at 3:03:34 AM UTC-5, Amila Gunathilake wrote:
Hi Mark,

Actually where I can find the file you mention "/home/mwaite/git/bare/jenkins/jenkins.git/objects"  in your repository MarkEWaite/jenkins-bugs ?

Finally I able to get use the GIT Reference repository in my builder machine.   But to get use that I had to replace my old Jenkins Configurations by giving the GIT reference repository path as below. 

 

Capture1.PNG



 It's really surprised me because, as above screenshot when I include that reference-path configuration in Jenkins GUI only it starting to getting use my Jenkinsfile's  SCM checkout stage code configurations. 


Still I have a small issue as you can see below Jenkins console-output it says "
using Shallow fetch with depth 302 times.  (previously it was only one time. So I'm afraid that Jenkins uses both configurations such as Jenkins GUI and Jenkinsfile code configurations here. Is it okay since it may not do any harm to my builds ? 
 

Capture.PNG

Mark Waite

unread,
Feb 25, 2020, 8:18:28 PM2/25/20
to Jenkins Users


On Monday, February 24, 2020 at 8:17:07 PM UTC-7, Mark Waite wrote:
You can confirm that a reference repository is being used by looking for the file .git/objects/info/alternates in the repository.  


That sentence is phrased incorrectly.

You can confirm that a reference repository is being used by looking for the file .git/objects/info/alternates in the workspace. (emphasis added)

Mark Waite

unread,
Feb 25, 2020, 8:23:44 PM2/25/20
to Jenkins Users


On Tuesday, February 25, 2020 at 1:06:26 AM UTC-7, Amila Gunathilake wrote:


On Tuesday, February 25, 2020 at 3:03:34 AM UTC-5, Amila Gunathilake wrote:
Hi Mark,

Actually where I can find the file you mention "/home/mwaite/git/bare/jenkins/jenkins.git/objects"  in your repository MarkEWaite/jenkins-bugs ?

Finally I able to get use the GIT Reference repository in my builder machine.   But to get use that I had to replace my old Jenkins Configurations by giving the GIT reference repository path as below. 

 

Capture1.PNG



 It's really surprised me because, as above screenshot when I include that reference-path configuration in Jenkins GUI only it starting to getting use my Jenkinsfile's  SCM checkout stage code configurations. 


That should not surprise you.  If you want to use a reference repository, then you'll need to define the location of that reference repository in each context where a checkout is performed.  Reading the Jenkinsfile from git is a checkout.

 

Still I have a small issue as you can see below Jenkins console-output it says "
using Shallow fetch with depth 302 times.  (previously it was only one time. So I'm afraid that Jenkins uses both configurations such as Jenkins GUI and Jenkinsfile code configurations here. Is it okay since it may not do any harm to my builds ? 
 

Capture.PNG


 

Yes, the git plugin is known to perform two fetch operations for each checkout.  It has been reported as https://issues.jenkins-ci.org/browse/JENKINS-49757 and as https://issues.jenkins-ci.org/browse/JENKINS-56404 .  There is a Google Summer of Code project idea that proposes to fix that by reducing it so that it only calls fetch once.


Mark Waite 
Reply all
Reply to author
Forward
0 new messages