【问题标题】:Automated script to mirror a repository from gitlab to azure devops将存储库从 gitlab 镜像到 azure devops 的自动化脚本
【发布时间】:2022-11-09 09:42:20
【问题描述】:

要求是我需要将 gitlab 上的存储库镜像到天蓝色,保留所有历史记录。 我已经找到了一个脚本来执行此操作,并且在我的本地执行时它可以完美运行。 但是,我们想安排它,因此我们计划创建一个 jenkins 作业。

gitlab 的凭据在 jenkins 服务器上设置,ssh 密钥身份验证也在 jenkins 服务器和将触发管道的机器之间设置(我在我们网络中的 linux 服务器上运行管道,私有 SSH 密钥存储在 jenkins 上,SSH 公钥配置在 azure devops 平台上)

jenkins 脚本如下所示:`

pipeline {
    agent {label 'linuxNode'}



   stages {
        stage('mirror to azure') {
            steps {
                withCredentials([
                                gitUsernamePassword(credentialsId: 'KEY', gitToolName: 'Default'),
                                sshUserPrivateKey(credentialsId: 'KEY', keyFileVariable: '')]) {



                      sh '''#!/bin/bash
                             set -eufo pipefail



                           SOURCE_URL="https://gitlab-XXXXX.de/X/X/X/X/Y"
                            echo "source url taken"
                            TARGET_URL="git@ssh.dev.azure.com:v3/XXXX-XX/XX%20XX%20VV%20ZZ/XYXYX"
                            echo "target url taken"
                            WORKDIR="$(mktemp -d)"
                            
                            echo "Cloning from ${SOURCE_URL} into ${WORKDIR}..."
                            
                            git init --bare "${WORKDIR}"
                            cd "${WORKDIR}"
                            
                            git config remote.origin.url "${SOURCE_URL}"
                            git config --add remote.origin.fetch '+refs/heads/*:refs/heads/*'
                            git config --add remote.origin.fetch '+refs/tags/*:refs/tags/*'
                            git config --add remote.origin.fetch '+refs/notes/*:refs/notes/*'
                            git config remote.origin.mirror true
                            echo "we are before remote fetch"
                            git fetch --all
                            echo "we are after remote fetch"
                            
                            echo ""
                            echo "Cloned to ${WORKDIR}; pushing to ${TARGET_URL}"
                            git config http.proxy http://XXXX.XXXXX.XX:0000
                            git push --mirror "${TARGET_URL}"
                            
                            echo ""
                            echo "Cleaning up temporary directory ${WORKDIR}..."
                            
                            rm -rf "${WORKDIR}"
                            
                            echo "Done."
                        '''
                }
                }
            }
        }
    }

但是在 push 命令之后我最终得到了一个错误。错误:

15:33:47  Cloned to /tmp/tmp.pFdLWmf3rc; pushing to git@ssh.dev.azure.com:v3/XXXX-XX/XX%20XX%20VV%20ZZ/XYXYX
15:34:19  kex_exchange_identification: read: Connection reset by peer
15:34:19  fatal: Could not read from remote repository.
15:34:19 15:34:19  Please make sure you have the correct access rights
15:34:19  and the repository exists.
15:34:19  [Pipeline] }
15:34:19  [Pipeline] // withCredentials
15:34:19  [Pipeline] }
15:34:19  [Pipeline] // stage
15:34:19  [Pipeline] }
15:34:19  [Pipeline] // node
15:34:19  [Pipeline] End of Pipeline
15:34:19  ERROR: script returned exit code 128

【问题讨论】:

标签: azure jenkins azure-devops gitlab azure-repos


【解决方案1】:

您只想从您的描述中使用 SSH 将存储库从 gitlab 镜像到 azure devops。我想推荐一些简单的方法。

1 在脚本中使用 git 命令

cd C:UsersAdministratorcodespace
#  Clone the source repo to a temporary folder on your computer
git clone git@github.com:{account}/{your source repo} 
cd your source repo
# Run the following command to copy the source repo to the target repo.
git push  --mirror git@ssh.dev.azure.com:v3/{organization}/{project}/{repository}

2 使用 REST APT 镜像一个存储库,参考这张票:

Script to copy all the updates on gitlab repository to azure devops repository

【讨论】:

    猜你喜欢
    • 2022-11-03
    • 1970-01-01
    • 1970-01-01
    • 2020-07-21
    • 2020-12-19
    • 2021-04-23
    • 2015-07-16
    • 2013-03-30
    • 2021-04-30
    相关资源
    最近更新 更多