0

git pull doesn't work with Jenkins for some reason. I've updated some files on my local PC and pushed it to my Github. I can see the changes there. Then I run Jenkins build that has sh commands to pull from this repo and it says "Already up to date". Which is false as I clearly pushed new version to Github and if I go there and open up the file in my browser I can see the changes. This only happens with Jenkins running git pull because if I connect to my VM and run git pull myself it works fine and updates my files. Why does this happen?

My Jenkinsfile:

pipeline{
  agent any
  stages{
    stage("build"){
      steps{
        echo 'build'
        sh 'cd /var/atlassian/application-data/jira/scripts'
        sh 'eval "$(ssh-agent -s)"'
        sh 'ssh-add ~/.ssh/id_ed25519'
        sh 'git pull [email protected]:mightyajax/scripts.git'
      }
    }
    stage("test"){
      steps{
        echo 'test'
      }
    }
    stage("deploy"){
      steps{
        echo 'deploy'
      }
    }
  }
}

1 Answer 1

0

As far as I know, each sh '...' command in Jenkins is run through a separate /bin/sh process, so any changes to process-level environment only remain while that specific command is running.

For example, the cd in 'sh' line #1 only affects the first /bin/sh instance – as soon as that line is processed, the shell exits and all changes are lost. Line #2 is then processed in a brand new /bin/sh which inherits the original working directory from Jenkins. (So you're getting wrong results from "git pull" because you are in the wrong repository at that time.)

Similarly, when 'sh' line #2 sets some environment variables (namely SSH_AUTH_SOCK) through the eval, they are also immediately lost when the shell exits, so the subsequent 'ssh-add' doesn't know where the agent is.

Jenkins                                Interactive

jenkins                                sshd
├─ shell                               └─ shell
│  └─ cd /var/atlassian/etc               ├─ cd /var/atlassian/etc
├─ shell                                  ├─ eval $(ssh-agent)
│  └─ eval $(ssh-agent)                   ├─ ssh-add
├─ shell                                  └─ git pull
│  └─ ssh-add
└─ shell
   └─ git pull

To avoid this, run everything as a single script:

sh: 'cd /var/foo && eval $(ssh-agent) && ssh-add && git pull <url>'

You could also avoid the need for a manual cd by using the -C option telling Git to do it on its own: git -C /var/foo pull <url>.

1
  • worked for me thanks
    – J. Doe
    Commented Jul 21, 2022 at 5:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .