Azure Pipeline-使用YAML将文件从一个存储库复制到另一个存储库

goucqfw6  于 2023-03-13  发布在  其他
关注(0)|答案(3)|浏览(168)

其中一个存储库(源存储库)中有一个文件夹,我希望使用Azure Pipeline将其复制到另一个存储库(目标存储库)(因为它们需要同步)
到目前为止,我可以使用以下命令复制同一存储库中的文件夹:

- task: CopyFiles@2
  inputs:
    SourceFolder: '$(Build.Repository.LocalPath)\MyFolder\'
    Contents: |
      **
      !**\obj\**
      !**\bin\**
    TargetFolder: '$(Build.Repository.LocalPath)\DestFolder'
    flattenFolders: false
    CleanTargetFolder: true
    OverWrite: true
    preserveTimestamp: true

这是我连接到另一个存储库的方式:

resources:
   repositories:
   - repository: SourceRepo
     type: git
     name: MyCollection/SourceRepo

但我不知道如何从源存储库获取文件并将其放入目标存储库

lrl1mhuk

lrl1mhuk1#

经过大量的搜索,这是答案:

resources:
  repositories:
  - repository: SourceRepo
    type: git
    name: MyCollection/SourceRepo

steps:

- checkout: SourceRepo
  clean: true
- checkout: self
  persistCredentials: true
  clean: true

- task: DotNetCoreCLI@2
  displayName: "restore DestRepo"
  inputs:
    command: 'restore'
    projects: '$(Build.Repository.LocalPath)/DestRepo/**/*.csproj'
    feedsToUse: 'select'

- task: DotNetCoreCLI@2
  displayName: "build DestRepo"
  inputs:
    command: 'build'
    projects: '$(Build.Repository.LocalPath)/DestRepo/DestRepo/**/*.csproj'
    configuration: Release

# configurations for using git command
- task: CmdLine@2
  inputs:
    script: |
      cd $(Agent.HomeDirectory)\externals\git\cmd
      git config --global user.email ""
      git config --global user.name "$(Build.RequestedFor)"

- task: CmdLine@2
  displayName: checkout
  inputs:
    script: |
      git -C RootRep checkout $(Build.SourceBranchName)

- task: CmdLine@2
  displayName: pull
  inputs:
    script: |
      git -C DestRepo pull

- task: CopyFiles@2
  inputs:
    SourceFolder: '$(Build.Repository.LocalPath)\SourceRepo\SourceFolder'
    Contents: |
      **
      !**\obj\**
      !**\bin\**
    TargetFolder: '$(Build.Repository.LocalPath)\DestRepo\DestFolder'
    flattenFolders: false
    CleanTargetFolder: true
    OverWrite: true
    # preserveTimestamp: true

- task: CmdLine@2
  displayName: add
  inputs:
    script: |
      git -C DestRepo add --all

- task: CmdLine@2
  displayName: commit
  continueOnError: true
  inputs:
    script: |
      git -C DestRepo commit -m "Azure Pipeline Repository Integration"

- task: CmdLine@2
  displayName: push
  inputs:
    script: |
      git -C DestRepo push -u origin $(Build.SourceBranchName)
6ju8rftf

6ju8rftf2#

我试图找到一些与此问题相关的解决方案,但我发现了一个更好的方法,而不是使用复制文件任务,我们可以使用任何数量的资源库是构建管道中的资源,我们不需要检查所有这些。
这是我的构建管道的样子。

如您所见,我使用了两个变量
1.$(System.AccessToken),此变量在Azure DevOps中可用,也称为PAT(个人访问令牌)
1.$(Build.Repository.Uri)存储库的URL(这可以是资源中任何存储库的URL)。

2wnc66cl

2wnc66cl3#

我遇到了几乎相同的问题,两个存储库都可以在我的Azure DevOps租户上找到,而且我使用的是ubuntu代理,下面是我的解决方案:

resources:
  repositories:
  - repository: main-repository
    type: git
    name: main
  - repository: secondary-repository
    type: git
    name: secondary

steps:
- checkout: self
#first checkout the main repository
#Build related tasks
# configurations for using git command and use the another repository
- checkout: secondary-repository
  persistCredentials: true
- task: CmdLine@2
  displayName: Change directory
  inputs:
      script: |
        cd secondary-repository

- task: CmdLine@2
  inputs:
    script: |
      git config --global user.email "user@email.com"
      git config --global user.name "$(Build.RequestedFor)"

- task: CmdLine@2
  displayName: pull
  inputs:
    script: |
      git -C secondary-repository pull origin main
- task: CopyFiles@2
  inputs:
    SourceFolder: '$(Build.Repository.LocalPath)/main-repository/folder/'
    Contents: '*.jar'
    TargetFolder: '$(Build.Repository.LocalPath)/secondary-repository/folder/'
    flattenFolders: false
    CleanTargetFolder: true
    OverWrite: true
    # preserveTimestamp: true

- task: CmdLine@2
  displayName: pull
  inputs:
    script: |
      git -C secondary-repository checkout main

- task: CmdLine@2
  displayName: add
  inputs:
    script: |
      git -C secondary-repository add --all

- task: CmdLine@2
  displayName: commit
  continueOnError: true
  inputs:
    script: |
      git -C secondary-repository commit -m "Azure Pipeline Repository Integration"

- task: CmdLine@2
  displayName: status
  inputs:
    script: |
      git -C secondary-repository status

- task: CmdLine@2
  displayName: push
  inputs:
    script: |
      git -C secondary-repository push -u origin main

对于此解决方案,如果没有“项目集合生成服务”,则必须添加对该服务的权限。您可以通过转到存储库设置-〉存储库-〉选择所需的存储库-〉允许每个存储库中的“贡献”来执行此操作。x1c 0d1x
完成这些步骤后,您将能够在Azure中处理两个不同的存储库。

相关问题