如何使用 yaml 同步 Azure DevOps 中的整个存储库

问题描述 投票:0回答:1
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
- 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/Pipelines/'
    Contents: '**'
    TargetFolder: '$(Build.Repository.LocalPath)/repo2/Pipelines/'
    CleanTargetFolder: true
    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    

在上面的代码中,我只是尝试仅将管道文件夹从 repo1 复制到 repo2。但我想将 repo1 中的所有文件夹同步到 repo2。如何做到这一点

azure-devops azure-pipelines azure-pipelines-yaml
1个回答
0
投票

在上面的代码中,我只是尝试仅将管道文件夹从 repo1 复制到 repo2。但我想将 repo1 中的所有文件夹同步到 repo2。如何做到这一点

要将 repo1 中的所有文件夹同步到 repo2,您需要在代理上保留 repo2 中的

.git
文件,但替换所有其他文件。删除您的
CopyFiles@2
任务,添加以下两个任务:

- task: PowerShell@2
  displayName: clean repo except git
  inputs:
    targetType: 'inline'
    script: |
      Get-ChildItem -Path '$(Build.Repository.LocalPath)/repo2/' -Recurse |
      Where-Object { !$_.FullName.Contains(".git") } |
      Remove-Item -Recurse -Force

- task: CopyFiles@2
  inputs:
    SourceFolder: '$(Build.Repository.LocalPath)/repo1/'
    Contents: |
      **/*
      !.git/**/*
    TargetFolder: '$(Build.Repository.LocalPath)/repo2/'
    #CleanTargetFolder: true
    OverWrite: true

上面的 powershell 任务删除除 .git 之外的文件,CopyFiles@2 将同步除 .git 之外的所有文件。

另外,前面要加上

- checkout: repo1

pipeline repo
上,使用
Collection scoped build service account
:

enter image description here

在有repo1、repo2的

projectname
上,确保构建服务账号有
contribut
force push
权限,这样才能同步代码。 enter image description here

整个yaml如下:

trigger: none

pool:
  vmImage: windows-latest

resources:
  repositories:
    - repository: repo1
      type: git
      name: wadetest14/repo1
    - repository: repo2
      type: git
      name: wadetest14/repo2

steps:
- checkout: self
- 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 "wadetest"
      git config --global user.name "[email protected]"

- task: CmdLine@2
  displayName: "pull"
  inputs:
    script: |
      git -C repo2 pull origin development

# - task: CopyFiles@2
#   inputs:
#     SourceFolder: '$(Build.Repository.LocalPath)/repo1/Pipelines/'
#     Contents: '**'
#     TargetFolder: '$(Build.Repository.LocalPath)/repo2/Pipelines/'
#     CleanTargetFolder: true
#     OverWrite: true

- task: PowerShell@2
  displayName: clean repo except git
  inputs:
    targetType: 'inline'
    script: |
      Get-ChildItem -Path '$(Build.Repository.LocalPath)/repo2/' -Recurse |
      Where-Object { !$_.FullName.Contains(".git") } |
      Remove-Item -Recurse -Force

- task: CopyFiles@2
  inputs:
    SourceFolder: '$(Build.Repository.LocalPath)/repo1/'
    Contents: |
      **/*
      !.git/**/*
    TargetFolder: '$(Build.Repository.LocalPath)/repo2/'
    #CleanTargetFolder: true
    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    

enter image description here

编辑:

我需要忽略同步文件,我怎样才能实现这一点。例如,一个名为 a.txt 的文件,我如何在同步中忽略该文件

和.git文件一样,你可以在powershell和copyfile任务中再添加一个排除项来忽略a.txt。

- task: PowerShell@2
  displayName: clean repo except git
  inputs:
    targetType: 'inline'
    script: |
      Get-ChildItem -Path '$(Build.Repository.LocalPath)/repo2/' -Recurse |
      Where-Object { (!$_.FullName.Contains(".git")) -and (!$_.FullName.Contains("a.txt")) } |
      Remove-Item -Recurse -Force

- task: CopyFiles@2
  inputs:
    SourceFolder: '$(Build.Repository.LocalPath)/repo1/'
    Contents: |
      **/*
      !.git/**/*
      !**/a.txt
    TargetFolder: '$(Build.Repository.LocalPath)/repo2/'
    #CleanTargetFolder: true
    OverWrite: true

添加更多:

我可以忽略该文件中的前 250 行吗?我可以从 repo1 同步其他 250 行吗

如上所述,您可以在 powershell 中再添加一个排除项并复制文件任务来忽略 a.txt 文件。然后再添加一个任务来替换内容,忽略前 250 行。

我测试了字符串较少,请将

3
替换为
250
,我的a.txt在repo1,repo2下。

- powershell: |
    $sourcecontent = Get-Content -Path "repo1/a.txt"
    $targetcontent = Get-Content -Path "repo2/a.txt"

    # Modify the desired lines (starting from line 4)
    if ($sourcecontent.Length -lt $targetcontent.Length) {
      for ($i = 3; $i -lt $sourcecontent.Length; $i++) {
        $targetcontent[$i] = $sourcecontent[$i]
      }
    }
    else {
      for ($i = 3; $i -lt $targetcontent.Length; $i++) {
        $targetcontent[$i] = $sourcecontent[$i]
      }
      for ($i = $targetcontent.Length; $i -lt $sourcecontent.Length; $i++ ) {
        $targetcontent += $sourcecontent[$i]
      }
    }

    # Write the updated content back to the text file
    ($targetcontent)[0..($sourcecontent.Length-1)] | Set-Content -Path "repo2/a.txt"

替换内容:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.