5

I would like to be able to see which files have changed between my current branch and the master branch during a jenkins build.

in my jenkinsfile i have the below code.

git diff-tree -r --no-commit-id --name-only ${env.GIT_COMMIT} origin/master

but i get the below error

fatal: ambiguous argument 'master': unknown revision or path not in the working tree.

Any help would be appreciated..it'd be great if someone could tell me what i'm doing wrong or devise an alternate solution that will give me the filepaths of files that have changed.

fyi, the above command works locally, but not in jenkins.

gurpsone
  • 463
  • 7
  • 17

2 Answers2

9

I finally found the fix for this. @vonc pointed me in the right direction, but the link he provided didn't make any sense.

Here's the link that helped me.

https://issues.jenkins-ci.org/browse/JENKINS-45666?page=com.atlassian.jira.plugin.system.issuetabpanels%3Achangehistory-tabpanel

You have to add a refspec in the jenkins settings for the project. If you are using github organisations you have to apply in the organisations settings as you can't change a specifc repos settings. Anyway, this works for me.

Add this as a refspec.

+refs/heads/master:refs/remotes/@{remote}/master
gurpsone
  • 463
  • 7
  • 17
0

That should means there is no master branch checked out in the Jenkins workspace.
This is typical of a cloned repo whose default branch is not master.

Make sure to a pre-build-step in jenkins:

git checkout -b master origin/master

Then try again your diff.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I've checked the settings for the repo in github and the master branch is the default branch. is that what you meant? – gurpsone Jan 19 '19 at 14:42
  • @gurpsone Yes. But you still can try and add in your Jenkins job, as a pre-build step, the checkout I mention, just to see if that makes any difference. – VonC Jan 20 '19 at 00:16
  • + git checkout -b master origin/master fatal: 'origin/master' is not a commit and a branch 'master' cannot be created from it – gurpsone Jan 20 '19 at 14:50
  • @gurpsone So... the remote repo has no master branch apparently. Or the fetch rule didn't fetch all remote branches. – VonC Jan 20 '19 at 15:29
  • how do i make sure it fetches all remote branches? The remote repo definitely has a master branch. – gurpsone Jan 20 '19 at 17:26
  • thanks for your replies..i just wanted to confirm, if you meant that i should put the fetch in my jenkinsfile? – gurpsone Jan 20 '19 at 17:37
  • @gurpsone yes, or specify a refspec bringing all branches, as in https://stackoverflow.com/a/45112838/6309 – VonC Jan 20 '19 at 17:41
  • the fetch inside the jenkinsfile hangs until timeout. I don't understand the refspec...thanks anyway. – gurpsone Jan 20 '19 at 19:40