如何通过YAML在Azure DevOps中同步完整Git仓库
修改Azure Pipeline实现repo1全量同步到repo2
要实现将repo1的所有文件夹同步至repo2,只需调整CopyFiles@2任务的配置,并确保复制时不破坏repo2的Git仓库信息,以下是修改后的完整流水线代码:
trigger: - none pool: vmImage: windows-latest resources: repositories: - repository: repo1 type: git name: projectname/repo1 - repository: repo2 type: git name: projectname/repo2 steps: - checkout: self # 若流水线定义在repo1中,此步骤会拉取repo1代码;若不在,需替换为 checkout: repo1 - checkout: repo2 persistCredentials: true - task: CmdLine@2 displayName: "change directory" inputs: script: | cd repo2 - task: CmdLine@2 inputs: script: | git config --global user.email "xxxx" git config --global user.name "xxxx" - task: CmdLine@2 displayName: "pull" inputs: script: | git -C repo2 pull origin development # 核心修改:调整复制规则实现全量同步 - task: CopyFiles@2 inputs: SourceFolder: '$(Build.Repository.LocalPath)/repo1/' Contents: | ** !.git/** # 排除repo1的.git目录,避免覆盖repo2的仓库配置 TargetFolder: '$(Build.Repository.LocalPath)/repo2/' CleanTargetFolder: true # 复制前清空repo2中现有内容(根据需求可选,若需保留repo2原有非冲突文件可设为false) OverWrite: true - task: CmdLine@2 inputs: script: | git -C repo2 checkout development - task: CmdLine@2 inputs: script: | git -C repo2 add --all - task: CmdLine@2 inputs: script: | git -C repo2 commit -m "Azure Pipeline Repository Integration" - task: CmdLine@2 inputs: script: | git -C repo2 status - task: CmdLine@2 inputs: script: | git -C repo2 push -u origin development
关键修改说明:
- 源文件夹调整:将
SourceFolder从repo1/Pipelines/改为repo1/,指向repo1的根目录 - 内容匹配规则:使用
**匹配所有文件和文件夹,同时通过!.git/**排除repo1的Git仓库目录,防止覆盖repo2自身的.git文件夹导致仓库信息丢失 - 目标文件夹调整:将
TargetFolder改为repo2/,将内容同步到repo2的根目录 - CleanTargetFolder:若设置为
true,会在复制前清空repo2的现有内容,实现严格的全量覆盖;若需保留repo2中未在repo1出现的文件,可将此值改为false
另外注意:如果你的流水线定义不在repo1中,需要将checkout: self替换为checkout: repo1,确保repo1的代码被拉取到流水线环境中。
内容的提问来源于stack exchange,提问作者Vinay.M vinu.M
相关产品推荐
相关产品推荐

