Home > database >  How to merge code from one Git repo A to Git repo B using Jenkins
How to merge code from one Git repo A to Git repo B using Jenkins

Time:07-19

I want to merge a code from Git repo A to Git repo B. Basically we do development in git repo A and have all code reviews and merged to main branch of git repo A. Whenever any pull request merges into repo A, I want to push the same code to another repo that is repo B. The purpose of doing this is to ensure both repo A and repo B is always same. Both Repositories belong to the same Organization.

Is this possible at all via WebHooks? if yes, please share me any guidance or document.

Also please share me if there is any other easy way.

CodePudding user response:

If you want to have the same history in both repos you can simply add a second repo as a remote and push the code from A to B. You can read more about working with remotes here.

A Jenkins pipeline will look like something below. You can trigger the Pipeline either by a Webhook or a Polling Timer.

pipeline {
    agent any
    stages {
        stage('Sync Repos') {
            steps {
                sh """
                  git clone -b main [email protected]:ORG/A.git
                  cd A  
                  git remote add B [email protected]:ORG/B.git
                  git push B main
                """
            }
        }
    }
}
  • Related