Home > OS >  Automated script to mirror a repository from gitlab to azure devops
Automated script to mirror a repository from gitlab to azure devops

Time:11-09

The requirement is that I need to mirror a repository on gitlab to azure, keeping all the history. I have figured out a script to do so, and it works perfectly when executed on my local. However, we want to schedule it, and thus we plan to create a jenkins job.

The credentials for gitlab is setup on the jenkins server, the ssh key authentication is also setup between the jenkins server and the machine on which the pipeline will be triggered (I am running the pipeline on a linux server in our network, the private SSH key is stored on jenkins, and the public SSH key is configured on the azure devops platform)

Image of the structure

The jenkins script looks like below : `

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="[email protected]:v3/XXXX-XX/XX XX VV ZZ/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."
                        '''
                }
                }
            }
        }
    }

But I end up getting an error after the push command. The error:

15:33:47  Cloned to /tmp/tmp.pFdLWmf3rc; pushing to [email protected]:v3/XXXX-XX/XX XX VV ZZ/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

CodePudding user response:

You just want to mirror a repository from gitlab to azure devops with SSH from your description. There are some simple ways that I would like to recommend.

1 use git command in script

cd C:\Users\Administrator\codespace
#  Clone the source repo to a temporary folder on your computer
git clone [email protected]:{account}/{your source repo} 
cd your source repo
# Run the following command to copy the source repo to the target repo.
git push  --mirror [email protected]:v3/{organization}/{project}/{repository}

2 use REST APT to mirror a repository ,refer to this ticket:

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

  • Related