2

I have two git repositories, one was forked from the other.

I would like a Jenkins server to pull from repository A, build it and if everything builds successfully it should push it to repository B.

The problem is that having multiple SCMs in Jenkins require to checkout them both.

Ido Ran
  • 10,584
  • 17
  • 80
  • 143
  • Is one repo the upstream of the other (do they share the same commits)? because if that's the case you can change the [`remote`](https://git-scm.com/docs/git-remote) on th fly – Rik Sep 04 '17 at 13:45
  • Yes, they have shared history. Also they are two different systems, one is Github Enterprise and the other is Stash on-prem so I need Jenkins credentials to access them both. – Ido Ran Sep 04 '17 at 13:46
  • You can specify (with ssh config) different ssh keys for different endpoints. See [this post](https://stackoverflow.com/questions/7927750/specify-an-ssh-key-for-git-push-for-a-given-domain). Or you can overwrite the `SSH` command git uses by setting `GIT_SSH="ssh -i /path/to/private-key"` – Rik Sep 04 '17 at 13:48
  • 1
    I really prefer to use git built-in credentials and refrain from manual changes to the server it runs on. – Ido Ran Sep 04 '17 at 13:49
  • You mean you push to https? Because then you might be able to use [this](https://stackoverflow.com/questions/29776439/username-and-password-in-command-for-git-push). i don't really know how the built-in credentials work, because I mainly use ssh credentials from the user running the jenkins service – Rik Sep 04 '17 at 13:50

1 Answers1

0

Multiple SCM will not support the functionality fully. Writing the stages in the Pipeline would do this. In the following Snippet, assuming there is a file called dummy1.cfg in the First Repo A. We clone Repo A, post which we clone the second Repo B, move the dummy1.config file to the folder where we clone Repo B and push changes from there to Repo B.

pipeline {
agent any

stages {
    stage('Cloning Repo A') {
        steps {
            git branch: 'master',
            credentialsId: 'Innersource_Cred',
            url: 'https://innersource.com/scm/~i/repo_a.git'
        }

    }
    
stage('Clone Repo B') {
      steps {
             dir("/root/repo_b") {
               git branch: 'master',
               credentialsId: 'Innersource_Cred',
               url: 'https://innersource.com/scm/~i/repo_b.git'
             }       
      }     
    }
    
    stage('Move File') {
      steps {
               sh 'cp /var/jenkins_home/workspace/${JOB_NAME}/dummy1.cfg /root/repo_b'
      }     
    }

    stage('Push File') {
      steps {
             dir("/root/repo_b") {
                 withCredentials([usernamePassword(credentialsId: 'Innersource_Cred',
                 usernameVariable: 'Username',
                 passwordVariable: 'Password')]) {
                    sh ''' 
                    git pull https://${Username}:${Password}@innersource.com/scm/~i/repo_b.git
                    git add .
                    git commit -m "push to git"
                    git push https://${Username}:${Password}@innersource.com/scm/~i/repo_b.git
                    '''
                 }
             }
      }     
    }
}
 }

If Git Credentials are using key authentication, use Jenkins Plugin withCredentials([sshUserPrivateKey(credentialsId: 'Innersource_Credentials', keyFileVariable: 'SSH_KEY')]) { ''' Type commands here ''' }

Sonal
  • 579
  • 5
  • 7