同步两个git仓库的Jenkins Pipeline

4
我希望每次构建时同步两个代码仓库,我看到了这个脚本,但不知道如何设置远程分支和凭据。

# 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

管道:
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'
}

我不知道如何设置凭据来推送更改到远程仓库。 git push repo1 --all git push repo1 --tags


你曾经解决过它吗? - Karpik
嗨,尝试以下解决方案,我不知道这是否是最好的方法,但如果您添加: sh 'git config --global credential.helper cache' sh "git config --global credential.helper 'cache --timeout=3600'" 则凭据将得到维护,然后您可以随心所欲地进行操作。 - Mikel Sanchez
1个回答

9
这应该在第一个仓库被推送时触发(通过webhook或类似方式)。
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'
        }
    }
}

有没有插件支持这些命令? - FancyPants

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接