Copy Files task

Azure Pipelines | Azure DevOps Server 2019 | TFS 2018 | TFS 2017 | TFS 2015.3

Use this task in a build or release pipeline to copy files from a source folder to a target folder using match patterns.

Note

In Microsoft Team Foundation Server (TFS) 2018 and previous versions, build and release pipelines are called definitions, service connections are called service endpoints, stages are called environments, and jobs are called phases.

Demands

None

YAML snippet

# Copy Files
# Copy files from source folder to target folder using match patterns (The match patterns will only match file paths, not folder paths)
- task: CopyFiles@2
  inputs:
    #sourceFolder: # Optional
    #contents: '**' 
    targetFolder: 
    #cleanTargetFolder: false # Optional
    #overWrite: false # Optional
    #flattenFolders: false # Optional

Arguments

Argument Description
Source Folder

Folder that contains the files you want to copy. If you leave it empty, the copying is done from the root folder of the repo (same as if you had specified $(Build.SourcesDirectory)).

If your build produces artifacts outside of the sources directory, specify $(Agent.BuildDirectory) to copy files from the directory created for the pipeline.

Contents

Specify fnmatch pattern filters (one per line) that you want to apply to the list of files to be copied. For example:

  • * copies all files in the specified source folder.
  • **\* copies all files in the specified source folder and all files in all sub-folders.
  • **\bin\** copies all files recursively from any bin folder.

The pattern is used to match only file paths, not folder paths. So you should specify patterns such as **\bin\** instead of **\bin.

You must use the path separator that matches your build agent type, e.g. / must be used for Linux agents.

More examples are shown below.

Target Folder Folder where the files will be copied. In most cases you specify this folder using a variable. For example, specify $(Build.ArtifactStagingDirectory) if you intend to publish the files as build artifacts.
Advanced
Clean Target Folder Select this check box to delete all existing files in the target folder before beginning to copy.
Overwrite Select this check box to replace existing files in the target folder.
Flatten Folders Flatten the folder structure and copy all files into the specified target folder.
Control options

Notes

If no files are matched, the task will still report success. If a matched file already exists in the target, the task will report failure unless Overwrite is set to true.

Usage

A typical pattern for using this task is:

  • Build something
  • Copy build outputs to a staging directory
  • Publish staged artifacts

For example:

steps:
- powershell: .\build.ps1
- task: CopyFiles@2
  inputs:
    contents: '_buildOutput\**'
    targetFolder: $(Build.ArtifactStagingDirectory)
- task: PublishBuildArtifacts@1
  inputs:
    pathtoPublish: $(Build.ArtifactStagingDirectory)
    artifactName: MyBuildOutputs

Examples

Copy executables and a readme file

Goal

You want to copy just the readme and the files needed to run this C# console app:

`-- ConsoleApplication1
    |-- ConsoleApplication1.sln
    |-- readme.txt
    `-- ClassLibrary1
        |-- ClassLibrary1.csproj
    `-- ClassLibrary2
        |-- ClassLibrary2.csproj
    `-- ConsoleApplication1
        |-- ConsoleApplication1.csproj

Note: ConsoleApplication1.sln contains a bin folder with .dll and .exe files, see the Results below to see what gets moved!

On the Variables tab, $(BuildConfiguration) is set to release.

Example with multiple match patterns:

steps:
- task: CopyFiles@2
  displayName: 'Copy Files to: $(Build.ArtifactStagingDirectory)'
  inputs:
    Contents: |
     ConsoleApplication1\ConsoleApplication1\bin\**\*.exe
     ConsoleApplication1\ConsoleApplication1\bin\**\*.dll
     ConsoleApplication1\readme.txt
    TargetFolder: '$(Build.ArtifactStagingDirectory)'

Example with OR condition:

steps:
- task: CopyFiles@2
  displayName: 'Copy Files to: $(Build.ArtifactStagingDirectory)'
  inputs:
    Contents: |
     ConsoleApplication1\ConsoleApplication1\bin\**\?(*.exe|*.dll)
     ConsoleApplication1\readme.txt
    TargetFolder: '$(Build.ArtifactStagingDirectory)'

Example with NOT condition:

steps:
- task: CopyFiles@2
  displayName: 'Copy Files to: $(Build.ArtifactStagingDirectory)'
  inputs:
    Contents: |
     ConsoleApplication1\**\bin\**\!(*.pdb|*.config)
     !ConsoleApplication1\**\ClassLibrary*\**
     ConsoleApplication1\readme.txt
    TargetFolder: '$(Build.ArtifactStagingDirectory)'

YAML builds are not yet available on TFS.

Results

These files are copied to the staging directory:

`-- ConsoleApplication1
    |-- readme.txt
    `-- ConsoleApplication1
        `-- bin
            `-- Release
                | -- ClassLibrary1.dll
                | -- ClassLibrary2.dll
                | -- ConsoleApplication1.exe

Copy everything from the source directory except the .git folder

Example with multiple match patterns:

steps:
- task: CopyFiles@2
  displayName: 'Copy Files to: $(Build.ArtifactStagingDirectory)'
  inputs:
    SourceFolder: '$(Build.SourcesDirectory)'
    Contents: |
     **/*
     !.git/**/*
    TargetFolder: '$(Build.ArtifactStagingDirectory)'

YAML builds are not yet available on TFS.

Open source

This task is open source on GitHub. Feedback and contributions are welcome.

Q & A

Where can I learn more about file matching patterns?

File matching patterns reference

How do I use this task to publish artifacts?

See Artifacts in Azure Pipelines.

Q: I'm having problems. How can I troubleshoot them?

A: Try this:

  1. On the variables tab, add system.debug and set it to true. Select to allow at queue time.

  2. In the explorer tab, view your completed build and click the build step to view its output.

The control options arguments described above can also be useful when you're trying to isolate a problem.

Q: How do variables work? What variables are available for me to use in the arguments?

A: $(Build.SourcesDirectory) and $(Agent.BuildDirectory) are just a few of the variables you can use. See Variables.

Do I need an agent?

You need at least one agent to run your build or release. Get an agent for Linux, macOS, or Windows.

I can't select a default agent pool and I can't queue my build or release. How do I fix this?

See Agent pools.

I use TFS on-premises and I don't see some of these features. Why not?

Some of these features are available only on Azure Pipelines and not yet available on-premises. Some features are available on-premises if you have upgraded to the latest version of TFS.