简体   繁体   English

如何将 Azure DevOps 存储库与 GitHub 存储库同步

[英]How to synchronize Azure DevOps repo with GitHub repo

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.我正在开发一个个人项目,并将其托管在 Azure DevOps 存储库中,因为我还想了解更多有关 Azure 服务如何工作的信息。 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.但是,我真的很想在 Github 上获得这个 repo 的精确副本,因为我以前的大部分项目都是在这里创建的。 Ideally, I'd like to set up a system so that every time I pushed to any branch in Azure, the entire repository (ie all branches, even if they're new) are copied to GitHub automatically.理想情况下,我想建立一个系统,以便每次我推送到 Azure 中的任何分支时,整个存储库(即所有分支,即使它们是新的)都会自动复制到 GitHub。

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.我试过四处查看几篇不同的文章,但我无法得到任何工作,可能是因为我无法完全理解它。

You could use this azure Pipelines Build and Release extension helps you synchronise one Git Repository with another : Git Tools for Azure DevOps您可以使用这个 azure Pipelines Build and Release 扩展帮助您将一个 Git 存储库与另一个同步: 用于 Azure DevOps 的 Git 工具

Create a CI pipeline and set variable $(Github.PAT)=GitHub personal access token.创建 CI 管道并设置变量 $(Github.PAT)=GitHub 个人访问令牌。 Add this task with the following configuration : https://<GitHub user name>:$(Github.PAT)@github.com/<OrganizationName>/<RepoName>.git .使用以下配置添加此任务: https://<GitHub user name>:$(Github.PAT)@github.com/<OrganizationName>/<RepoName>.git

在此处输入图像描述

You could also use PowerShell task with script for it as below and add pipeline variables as secret variable.您还可以使用带有脚本的 PowerShell 任务,如下所示,并将管道变量添加为秘密变量。 Name 'AzureDevOps.PAT' (with value as your Azure DevOps PAT) and name 'Github.PAT' (with value as your GitHub PAT).命名为“AzureDevOps.PAT”(值为您的 Azure DevOps PAT)并命名为“Github.PAT”(值为您的 GitHub PAT)。

在此处输入图像描述

# 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
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM