Home > Blockchain >  How to synchronize Azure DevOps repo with GitHub repo
How to synchronize Azure DevOps repo with GitHub repo

Time:07-09

I'm working on a personal project and I have it hosted in an Azure DevOps repo because I'd also like to learn a little bit more about how Azure services work. However, I'd really like to have an exact copy of this repo available on Github because that's where most of my previous projects have been created. Ideally, I'd like to set up a system so that every time I pushed to any branch in Azure, the entire repository (i.e. all branches, even if they're new) are copied to GitHub automatically.

I've tried looking around at a few different articles, but I haven't been able to get anything to work, likely because I haven't been able to fully understand it.

CodePudding user response:

You could use this azure Pipelines Build and Release extension helps you synchronise one Git Repository with another : enter image description here

You could also use PowerShell task with script for it as below and add pipeline variables as secret variable. Name ‘AzureDevOps.PAT’ (with value as your Azure DevOps PAT) and name ‘Github.PAT’ (with value as your GitHub PAT).

enter image description here

# Write your PowerShell commands here.
Write-Host ' - - - - - - - - - - - - - - - - - - - - - - - - -'
Write-Host ' reflect Azure Devops repo changes to GitHub repo'
Write-Host ' - - - - - - - - - - - - - - - - - - - - - - - - - '
$stageDir = '$(Build.SourcesDirectory)' | Split-Path
$githubDir = $stageDir  "\" "gitHub"
$destination = $githubDir  "\" "<azure-repo-name>.git"
#please provide your username
$alias = '<userName>:'  "$(Github.PAT)"
#Please make sure, you remove https from azure-repo-clone-url
$sourceURL = 'https://$(AzureDevOps.PAT)@dev.azure.com/<devops organization name>/<project name>/_git/<azure repo name>'
#Please make sure, you remove https from github-repo-clone-url
$destURL = 'https://'   $alias   '@github.com/<GitHub organization name>/<repo name>.git'
#Check if the parent directory exists and delete
if((Test-Path -path $githubDir))
{
  Remove-Item -Path $githubDir -Recurse -force
}
if(!(Test-Path -path $githubDir))
{
  New-Item -ItemType directory -Path $githubDir
  Set-Location $githubDir
  git clone --mirror $sourceURL
}
else
{
  Write-Host "The given folder path $githubDir already exists";
}
Set-Location $destination
Write-Output '*****Git removing remote secondary****'
git remote rm secondary
Write-Output '*****Git remote add****'
git remote add --mirror=fetch secondary $destURL
Write-Output '*****Git fetch origin****'
git fetch $sourceURL
Write-Output '*****Git push secondary****'
git push secondary --all
Write-Output '**Azure Devops repo synced with Github repo**'
Set-Location $stageDir
if((Test-Path -path $githubDir))
{
 Remove-Item -Path $githubDir -Recurse -force
} 
  • Related