3

There is an open bug on jenkins 2.0 pipeline scripts relating to included regions in git, so it means for a large mono-repo as in my case each checkin to master will cause multiple pipelines to be kicked off which is not the desired behavior. So to visualize:

top-level:
->application folder 1
->application folder 2

What I want to do is to do a git fetch first so then I can do a git diff to see if anything in the particular folder has changed and if it has then run the pipeline for that particular folder and not do anything if nothing changed

Code I have is below:

node{
git credentialsId: 'cred id', url:    'ssh://git@git-repo:1234/app/mono-repo.git'
ret = sh(script: 'git fetch; git diff origin/master remotes/origin/master | grep "folder-name"', returnStatus: true)
    if(ret == 0){
        doSomething()
   }else{
        doNothing()
   }
}

The issue I have that the git fetch fails due a permissions error, I can use a the checkout but then I cannot get the diff before hand which is not what. Is there a way of using the u tiling the git fetch using the credentias?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
morrrowgi
  • 171
  • 3
  • 10

2 Answers2

2

It might help to simply get the references to tags. Note, I believe this is not equivalent to git fetch --tags. See Does "git fetch --tags" include "git fetch"? for example.

git([branches: [
        [name: '*/master'], 
        [name: 'refs/tags/*:refs/tags/*']], 
     credentialsId: CREDENTIALS_ID_GIT,
     url: REPO])

I noticed that on the Jenkin's console the Git plugin is performing a git fetch --tags, so by default the Git Plugin may already provide this functionality. Please check on this.

I'd like to also add this solution:

withCredentials(
    [usernamePassword(
        credentialsId: CREDENTIALS_ID_GIT, 
        passwordVariable: 'GIT_PASSWORD', 
        usernameVariable: 'GIT_USERNAME')]) {
        sh("git fetch --tags https://${GIT_USERNAME}:${GIT_PASSWORD}@${REPO}")
    }
Tr1gZer0
  • 1,482
  • 11
  • 18
-2

Make sure you are using SSH credentials with the correct user. You can check this answer, which is summarized by the capture below :

Defining SSH credentials for Jenkins

In particular, make sure that in the url ssh://git@git-repo:1234/app/mono-repo.git the git@ part is your actual SSH user. In my case it is the jenkins user, so I would use ssh://jenkins@git-repo:1234/app/mono-repo.git instead.

Community
  • 1
  • 1
Pom12
  • 7,622
  • 5
  • 50
  • 69