Azure Pipeline- Copy files from one Repo to another Repo using YAML

Viewed 5196

There is a folder in one of the repositories (Source Repo) that I like to copy to another repository (Destination Repo) using Azure Pipeline (as they needed to be in sync)

so far I can Copy a folder in the same repository using:

- 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

this is how I connect to another repository:

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

but I don't know how to get files from the source repo and place them in the Destination Repo

2 Answers

after a lot of searching, this is the answer:

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)

I was trying to find some solution related to this problem, but instead of using a copy file task, I found a better way and we can use any number of repositories are resources in the build pipeline and we don't need to check out all these.

This is how my build pipeline looks like.

enter image description here

As you can see I have used two variables

  1. $(System.AccessToken), this variable is available in Azure DevOps aka PAT(Personal Access Token)

  2. $(Build.Repository.Uri) URL of the repository (this could be the URL of any repo in resources).

Related