Search code examples
gitjenkinsautomation

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


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.


Solution

  • 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 git@github.com:ORG/A.git
                      cd A  
                      git remote add B git@github.com:ORG/B.git
                      git push B main
                    """
                }
            }
        }
    }