Зеркалирование репозиториев с помощью powershell / Git Mirroring

GitLab умеет делать pull mirror в платной версии. Рассмотрим как можно сделать задачу на зеркалирование с помощью powershell

Подробнее о том, как запускать powershell скрипты по расписанию https://windowsnotes.ru/powershell-2/zapusk-powers...

Используем базовый функционал git для создания зеркала. Предположим у нас есть:

1. Репозиторий источник - допустим DevOps Azure

2. Репозиторий приемник - допустим GitLab

Тогда общий сценарий будет такой:

1. Готовим Access Token для репозитория источника и приемника, т.о. чтобы упростить авторизацию. В итоге например для AzureDeops URL репозитория будет таким https://accesstoken@dev.azure.com/some-repo/_git/source-repo . О том как получить AccessToken для gitLab https://docs.gitlab.com/ee/user/profile/personal_a...

2. Делаем pull -mirror на машину, которая запускает powershell скрипт, если репозиторий уже склонирован. В противном случае делаем clone -mirror

3. Делаем push -mirror

Ниже пример такого скрипта, для его работы необходима файл конфига разместить в той же директории. Также проверьте значения переменный $log_file (абсолютный путь к лог файлу) и $base_path (путь где будет выполняться скрипт) repo-clone.ps1

$log_file = 'C:\Users\Administrator\repo-clone\logs.txt'
if (Test-Path $log_file) {
    Remove-Item $log_file
} 
Start-Transcript -Append 'C:\Users\Administrator\repo-clone\logs.txt'
$base_path = 'C:\Users\Administrator\repo-clone'
cd $base_path
. ./repo-config.ps1
foreach ($config in $configs) {
    echo $config['folder']
 if (Test-Path -Path $config['folder'] -PathType Container) {
        cd $config['folder']
        git -c http.sslVerify=true fetch -q --tags
 } else {
        git -c http.sslVerify=true clone --mirror $config['sourceRepo']
        cd $config['folder']
 }
 git.exe -c http.sslVerify=true push --mirror $config['destinatinationRepo']
 cd $base_path
}
Stop-Transcript

Тут хранится конфиг с несколькими репозиториями. Файл repo-config.ps1

$configs = @(
 @{
        # Указываем папку репозитория, в которую будем клонировать
        'folder' = '.\Some-Repo.git\';
        'sourceRepo' = 'https://accesstoken@dev.azure.com/some-repo/_git/source-repo';
        'destinatinationRepo' = 'https://accesstoken@git.somewebsite.ru/dest-repo/dest-repo.git';
 },
 @{
        'folder' = '.\Some-Repo2.git\';
        'sourceRepo' = 'https://accesstoken@dev.azure.com/some-repo2/_git/source-repo2';
        'destinatinationRepo' = 'https://accesstoken@git.somewebsite.ru/dest-repo2/dest-repo2.git';
 }
)


git
comments powered by Disqus