Sync Azure Devops repo with GitHub repo
Let’s say you have a repository in Azure DevOps and you want to keep that in sync with another repository in GitHub, so that you can integrate GitHub with other tools or services. With this you can still manage everything from Azure DevOps like you do today and no additional training is required.
So are you looking for syncing Azure repos with Github enterprise repos? Or are you planning to design a pipeline solution, so that all the file changes pushed to Azure repository (primary) should be reflected on the Github repo (secondary) also?
Prerequisites:
To complete this tutorial, you need following
1) GitHub Personal Access Token
2) Azure DevOps Personal Access Token
3) Existing Azure repo with files
4) New empty GitHub repo
5) Azure build pipeline with a powershell inline task
Steps:
1) To the existing CI pipeline or new pipeline, 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)
2) Add a Powershell task
3) Add this script as inline script
# 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)@<azure-repo-clone-url>'#Please make sure, you remove https from github-repo-clone-url
$destURL = 'https://' + $alias + '@<github-repo-clone-url>'#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 $destinationWrite-Output '*****Git removing remote secondary****'
git remote rm secondaryWrite-Output '*****Git remote add****'
git remote add --mirror=fetch secondary $destURLWrite-Output '*****Git fetch origin****'
git fetch $sourceURLWrite-Output '*****Git push secondary****'
git push secondary --allWrite-Output '**Azure Devops repo synced with Github repo**'
Set-Location $stageDirif((Test-Path -path $githubDir))
{
Remove-Item -Path $githubDir -Recurse -force
}
4) Now save and queue pipeline.