Sync two git repositories Jenkins Pipeline

守給你的承諾、 提交于 2020-01-13 06:18:06

问题


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 "jenkins@xxx.com"'
    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


回答1:


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 "test@test.com"'
        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'
        }
    }
}


来源:https://stackoverflow.com/questions/46530742/sync-two-git-repositories-jenkins-pipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!