Copy Files task

Azure DevOps Services | Azure DevOps Server 2022 - Azure DevOps Server 2019 | TFS 2018

Use this task 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, runs are called builds, service connections are called service endpoints, stages are called environments, and jobs are called phases.

Demands

None

YAML snippet

# Copy files
# Copy files from a source folder to a target folder using patterns matching file paths (not folder paths)
- task: CopyFiles@2
  inputs:
    #sourceFolder: # Optional
    #contents: '**' 
    targetFolder: 
    #cleanTargetFolder: false # Optional
    #overWrite: false # Optional
    #flattenFolders: false # Optional
    #preserveTimestamp: false # Optional
    #retryCount: 0 # Optional
    #ignoreMakeDirErrors: false # Optional

Arguments

Argument Description
SourceFolder
Source Folder
(Optional) 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
Contents
(Required) File paths to include as part of the copy. Supports multiple lines of match patterns.
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. Example, / must be used for Linux agents. More examples are shown below.
    Default value: **
TargetFolder
Target Folder
(Required) Target folder or UNC path files will copy to. You can use variables.
Example: $(build.artifactstagingdirectory)
CleanTargetFolder
Clean Target Folder
(Optional) Delete all existing files in target folder before copy
Default value: false
OverWrite
Overwrite
(Optional) Replace existing files in target folder
Default value: false
flattenFolders
Flatten Folders
(Optional) Flatten the folder structure and copy all files into the specified target folder
Default value: false
preserveTimestamp
Preserve Target Timestamp
(Optional) Using the original source file, preserve the target file timestamp.
Default value: false
retryCount
Retry count to copy the file
(Optional) Specify the retry count to copy the file. It might help to resolve intermittent issues e.g. with UNC target paths on a remote host.
Default value: 0
ignoreMakeDirErrors
Ignore errors during creation of target folder
(Optional) This could be useful to avoid issues with parallel execution of task by several agents with one target folder.
Default value: false

Notes

If no files are matched, the task will still report success. If a matched file already exists in the target folder, the task will report a warning and continue execution unless Overwrite parameter is set to true.

Example

steps:
- 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)'

Example with variables in content section

- task: CopyFiles@2
  inputs:
    Contents: '$(Build.Repository.LocalPath)/**' 
    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.

FAQ

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 issues with publishing my artifacts. How can I view the detailed logs?

To enable detailed logs for your pipeline:

  1. Edit your pipeline and select Variables
  2. Add a new variable with the name System.Debug and value true
  3. Save

Q: Which variables are available to me?

A: $(Build.SourcesDirectory) and $(Agent.BuildDirectory) are just few of the variables you can use in your pipeline. Variables are available as expressions or scripts.

See Define variables, predefined variables, and Classic release and artifacts variables to learn about the different types of variables.

Q: Task allows me to publish artifacts in deployment job in yaml pipeline, but I am not able to use it in downstream pipeline?

A: Deployment jobs do not have the context of source branches and are hence not appropriate for publishing artifacts. They have been primarily designed to consume artifacts. A workaround would be to isolate that logic into a separate job (with dependencies on your deployment jobs).

Do I need an agent?

You need at least one agent to run your build or release.

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

See Troubleshoot Build and Release.

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.

My NuGet push task is failing with the following error: "Error: unable to get local issuer certificate". How can I fix this?

This can be fixed by adding a trusted root certificate. You can either add the NODE_EXTRA_CA_CERTS=file environment variable to your build agent, or you can add the NODE.EXTRA.CA.CERTS=file task variable in your pipeline. See Node.js documentation for more details about this variable. See Set variables in a pipeline for instructions on setting a variable in your pipeline.

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.