Partilhar via


CopyFiles@2 - Copy files v2 task

Use this task to copy files from a source folder to a target folder using match patterns. (The match patterns will only match file paths, not folder paths).

Inputs

SourceFolder - Source Folder
string.

Optional. The folder that contains the files you want to copy. If the folder is empty, then the task copies files from the root folder of the repo as though $(Build.SourcesDirectory) was specified.

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
string. Required. Default value: **.

The file paths to include as part of the copy. This string 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. Specify patterns, such as **\bin\** instead of **\bin.

Use the path separator that matches your build agent type. For example, / must be used for Linux agents. More examples are shown below.


TargetFolder - Target Folder
string. Required.

The target folder or UNC path that will contain the copied files. You can use variables. Example: $(build.artifactstagingdirectory).


CleanTargetFolder - Clean Target Folder
boolean. Default value: false.

Optional. Deletes all existing files in the target folder before the copy process.


OverWrite - Overwrite
boolean. Default value: false.

Optional. Replaces the existing files in the target folder.


flattenFolders - Flatten Folders
boolean. Default value: false.

Optional. Flattens the folder structure and copies all files into the specified target folder.


Task control options

All tasks have control options in addition to their task inputs. For more information, see Control options and common task properties.

Output variables

None.

Remarks

If no files match, the task will still report success.

  • If Overwrite is false and a matched file already exists in the target folder, the task will not report failure but log that the file already exists and skip it.
  • If Overwrite is true and a matched file already exists in the target folder, the matched file will be overwritten.

Examples

Copy file to artifacts staging directory and publish

steps:
- task: CopyFiles@2
  inputs:
    contents: '_buildOutput/**'
    targetFolder: $(Build.ArtifactStagingDirectory)
- task: PublishBuildArtifacts@1
  inputs:
    pathToPublish: $(Build.ArtifactStagingDirectory)
    artifactName: MyBuildOutputs

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

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

Requirements

Requirement Description
Pipeline types YAML, Classic build
Runs on Agent, DeploymentGroup
Demands None
Capabilities This task does not satisfy any demands for subsequent tasks in the job.
Command restrictions Any
Settable variables Any
Agent version 1.91.0 or greater
Task category Utility

See also