【问题标题】:Git keep file in sync task between 2 repos in Azure DevOpsGit 在 Azure DevOps 中的 2 个存储库之间保持文件同步任务
【发布时间】:2019-11-04 21:36:30
【问题描述】:

我有需要在 2 个存储库(2 个不同的组织)之间保持同步的文件

  1. https://org1@dev.azure.com/org1/XProject/_git/MyRepoX
  2. https://org2@dev.azure.com/org2/YProject/_git/MyRepoY

RepoX 和 RepoY 都有一个共同的文件“FileA”(以保持同步)

当 FileA 发生更改时 - 触发管道并将 MyRepoX/FileA“合并”到 MyRepoY/FileA

我做了以下事情:

  • 从 Org1 创建了 PAT 令牌
  • 从 Org2 创建了 PAT 令牌
  • 使用命令行脚本创建管道
  • 在我做的脚本中:

    • git clone https://@dev.azure.com/org1/XProject/_git/MyRepoX
    • git 合并 https://@dev.azure.com/org1/YProject/_git/MyRepoY

错误“没有要合并的东西”。

我是一个“git 新手”,我需要执行什么命令才能在 repos 之间保持文件同步?

【问题讨论】:

  • 为什么不将文件放在一个包中并让您的 CI/CD 进程使用该包?
  • @DanielMann 你能再扩展一点吗?我没想到。所有的事情都必须在提交文件更改时发生

标签: git azure-devops azure-pipelines


【解决方案1】:

您不能像以前那样只进行合并,因为它是 2 个不同的存储库,而且,您真的要合并整个存储库吗?您只需同步一个文件。

你可以用这个逻辑来实现它:

  • 克隆第二个仓库
  • FileA从repo 1复制到repo 2
  • 提交和推送

我编写了一个可以运行的小型 PowerShell 脚本:

cd $(Agent.BuildDirectory)
git clone https://PAT-HERE@dev.azure.com/{organzition}/{project}/_git/{repo-name}
cd {repo-name}
git checkout branch-name # if the synced file not in master
# Assuming the synced file name is "test.txt" and he exists in the folder "common"
Copy-Item $(Build.SourcesDirectory)\common\test.txt $(Agnet.BuildDirectory)\{repo-name}\common -Force
# If you use Hosted agent you need to configure the email & address
git config --globa user.email "Build@AzureDevOps.com" 
git config --global user.name "Azure DevOps Build"
git add common/test.txt
git commit -m "Sync test.txt"
git push

现在创建 2 个管道,在每个管道中只触发您要同步的公共文件:

使用上面的脚本:

结果:

【讨论】:

  • 太棒了。让我明白你做了什么。我只需要同步一个文件而不是所有 repo。
  • @developer9969 是的,在我的示例中,我只同步一个文件 :)
  • @Abramczyk。刚试过 .grr "Get error "The term 'Agent.BuildDirectory' is not recognized as the name" (我不应该得到这个错误) 我正在使用发布管道代理 "Hosted Windows 2019 with VS2019".mmm
  • @developer9969 这个变量是用来构建管道的,不是用来发布的,为什么要使用发布管道?
  • @Abramczyk 。抱歉切换到构建并进一步发展......我省略了 git checkout 分支名称作为它的主人是电子邮件和用户的 git config(必需)我猜是“目标 repo.correct ?. 得到错误不知道为什么。我错误 + git commit -m "Sync test.txt" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~ + CategoryInfo : NotSpecified: (:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError
猜你喜欢
  • 2011-07-30
  • 2013-07-09
  • 2019-01-17
  • 1970-01-01
  • 2022-12-04
  • 2021-03-05
  • 2021-12-14
  • 2021-09-29
  • 1970-01-01
相关资源
最近更新 更多