Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sync two git repositories Jenkins Pipeline

I want to syncronize two repositories each time a build is been made, I have seen this script but I don't know how to set the remote branch with credentials too.

# clone the reposotory
git clone --bare $ORIGIN_URL

# add a remote repository
cd $REPO_NAME
git remote add --mirror=fetch repo1 $REPO1_URL

# update the local copy from the first repository
git fetch origin --tags

# update the local copy with the second repository
git fetch repo1 --tags

# sync back the 2 repositories
git push origin --all
git push origin --tags
git push repo1 --all
git push repo1 --tags

Pipeline:

node('centos-small') {
    sh 'git config --global user.email "[email protected]"'
    sh 'git config --global user.name "ci-bot"'
    git credentialsId: 'JenkinsGit', url: 'git url'
}

I don't know how to set the credentials to push changes to remote repo. git push repo1 --all git push repo1 --tags

like image 711
Mikel Sanchez Avatar asked Oct 28 '25 11:10

Mikel Sanchez


1 Answers

This should be triggered when something is pushed to the first repo (via webhook or similar)

node('centos-small') {
    stage('Set Git Config'){
        sh 'git config --global user.email "[email protected]"'
        sh 'git config --global user.name "ci-bot"'
        sh 'git config --global credential.helper cache'
        sh "git config --global credential.helper 'cache --timeout=3600'"
    }
    stage('Set Git Credentials'){
        git credentialsId: 'JenkinsGit', url: '${TFS_REPO}'
        git credentialsId: 'Second', url: '${SECOND_REPO}'
    }

    stage('Syncronize TFS-SECOND'){
        sh 'git clone --bare ${TFS_REPO} tfs'
        dir("tfs") {
            //add a remote repository
            sh 'git remote add --mirror=fetch second ${SECOND_REPO}'
            // update the local copy from the first repository
            sh 'git fetch origin --tags'

            // update the local copy with the second repository
            sh 'git fetch second --tags'

            // sync back the second repository
            sh 'git push second --all'
            sh 'git push second --tags'
        }
    }
}
like image 184
Mikel Sanchez Avatar answered Oct 29 '25 23:10

Mikel Sanchez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!