avatarRenjith Ravindranathan

Summary

The article outlines a process for synchronizing an external Git repository with Azure Repos using Azure Pipelines, due to the absence of a native synchronization feature in Azure Repos.

Abstract

The author describes a workaround to synchronize external Git repositories (from platforms like GitHub, Bitbucket, or GitLab) with Azure Repos, as Azure currently only offers a one-time import function. The solution involves generating Personal Access Tokens (PAT) for both Azure DevOps and the external Git service, setting up a service connection in Azure DevOps, creating a pipeline with a PowerShell script to handle the git commands, and executing the script to mirror the repositories. The script includes cloning the external repo, removing and reconfiguring remotes, and force-pushing to Azure Repos. The author notes that adjustments may be needed for Linux systems, either by converting the script to Bash or installing PowerShell Core.

Opinions

  • The author indicates that the lack of a built-in synchronization feature in Azure Repos is a limitation.
  • The use of Personal Access Tokens is emphasized as a necessary step for authentication.
  • The author provides a subjective opinion on the effectiveness of their solution by inviting feedback, suggesting confidence in the method described.
  • The article concludes with a call to action, encouraging readers to engage with the FAUN community and to show support for the author by using the clap button.

How to synchronize Azure Repos with External Git Repos

Earlier this month, I had a requirement to synchronize an external git repo(GitHub, Bitbucket, GitLab etc) to Azure Repos using Azure Pipelines. Unfortunately, there is no feature in Azure Repos now to synchronize with external repositories except a one time import function. To solve this, I had to write some tweak script containing git commands and execute it using the Azure Pipelines. Below are the steps I took in Azure DevOps to achieve the repo synchronization with GitLab Repo.

  1. First, we need to generate PAT from Azure Devops and GitLab.
  2. Create a service connection in Azure Devops Project.

3. Select the ‘Other Git’ option from the menu and click Next. Enter the external repo URL, PAT, and other details

4. Create a new pipeline from Azure Pipelines and select Other Git as source code

5. Select the Service connection and repo branch that you wanted to sync

6. Create a PowerShell task and add the variables. Note on the repo urls as it is concatenated with PAT tokens GITLAB -> https://TOKENNAME:TOKEN@gitlab.com/yourid/repo/path.git AZUREPOS -> https://AZUREDEVOPSPAT@dev.azure.com/yourid/../..

Write-Host Starting the synchronization process
mkdir copyrepo
$sourceURL = "https://$(GITLABPAT)@$(GITLABREPO)"
$destURL = "https://$(AZUREREPOPAT)@$(AZUREPO)"
Set-Location "$(Build.SourcesDirectory)/copyrepo"
git clone --mirror $sourceURL
Set-Location "$(Build.SourcesDirectory)/copyrepo/$(GITLABREPONAME)/"
Write-Host "*****Git removing remote origin****"
git remote rm origin
Write-Output "*****Git remote add****"
git remote add --mirror=fetch origin $destURL
Write-Output "*****Git fetch origin****"
git fetch $sourceURL
Write-Output "*****Git push to Azure Repos****"
git push origin --all -f

Please note that if you are using a Linux system, the script needs to be converted into Bash or needs to install PowerShell Core in the machine. There is no change in the git commands.

Check it out and let me know your feedback on this.

Join FAUN: Website 💻|Podcast 🎙️|Twitter 🐦|Facebook 👥|Instagram 📷|Facebook Group 🗣️|Linkedin Group 💬| Slack 📱|Cloud Native News 📰|More.

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇

Azure Devops
Gitlab
Git
Azure Pipelines
DevOps
Recommended from ReadMedium