CopyFiles@2 - 複製檔案 v2 工作

使用此工作,使用比對模式,將檔案從源資料夾複製到目的檔案夾。 (比對模式只會比對檔案路徑,而不是資料夾路徑) 。

Syntax

# Copy files v2
# Copy files from a source folder to a target folder using patterns matching file paths (not folder paths).
- task: CopyFiles@2
  inputs:
    #SourceFolder: # string. Source Folder. 
    Contents: '**' # string. Required. Contents. Default: **.
    TargetFolder: # string. Required. Target Folder. 
  # Advanced
    #CleanTargetFolder: false # boolean. Clean Target Folder. Default: false.
    #OverWrite: false # boolean. Overwrite. Default: false.
    #flattenFolders: false # boolean. Flatten Folders. Default: false.
    #preserveTimestamp: false # boolean. Preserve Target Timestamp. Default: false.
    #retryCount: '0' # string. Retry count to copy the file. Default: 0.
    #delayBetweenRetries: '1000' # string. Delay between two retries. Default: 1000.
    #ignoreMakeDirErrors: false # boolean. Ignore errors during creation of target folder. Default: false.
# Copy files v2
# Copy files from a source folder to a target folder using patterns matching file paths (not folder paths).
- task: CopyFiles@2
  inputs:
    #SourceFolder: # string. Source Folder. 
    Contents: '**' # string. Required. Contents. Default: **.
    TargetFolder: # string. Required. Target Folder. 
  # Advanced
    #CleanTargetFolder: false # boolean. Clean Target Folder. Default: false.
    #OverWrite: false # boolean. Overwrite. Default: false.
    #flattenFolders: false # boolean. Flatten Folders. Default: false.
    #preserveTimestamp: false # boolean. Preserve Target Timestamp. Default: false.
# Copy Files v2
# 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: # string. Source Folder. 
    Contents: '**' # string. Required. Contents. Default: **.
    TargetFolder: # string. Required. Target Folder. 
  # Advanced
    #CleanTargetFolder: false # boolean. Clean Target Folder. Default: false.
    #OverWrite: false # boolean. Overwrite. Default: false.
    #flattenFolders: false # boolean. Flatten Folders. Default: false.

輸入

SourceFolder - 源資料夾
string.

選擇性。 包含您要複製之檔案的資料夾。 如果資料夾是空的,則工作會從存放庫的根資料夾複製檔案,就像已指定一樣 $(Build.SourcesDirectory)

如果您的組建在來原始目錄之外產生成品,請指定 $(Agent.BuildDirectory) 從為管線建立的目錄複寫檔案。


Contents - 內容
string. 必要。 預設值:**

要包含在複本中的檔案路徑。 此字串支援多行比對模式。

例如:

  • * 會複製指定之源資料夾中的所有檔案。
  • ** 會複製指定之源資料夾中的所有檔案,以及所有子資料夾中的所有檔案。
  • **\bin\** 以遞迴方式從任何 bin 資料夾複製所有檔案。

此模式只用來比對檔案路徑,而不是資料夾路徑。 指定模式,例如 **\bin\** ,而不是 **\bin

使用符合組建代理程式類型的路徑分隔符號。 例如, / 必須用於 Linux 代理程式。 以下顯示更多範例。


TargetFolder - 目的檔案夾
string. 必要。

將包含所複製檔案的目的檔案夾或 UNC 路徑。 您可以使用 變數。 範例: $(build.artifactstagingdirectory).


CleanTargetFolder - 清除目的檔案夾
boolean. 預設值:false

選擇性。 在複製程式之前,刪除目的檔案夾中的所有現有檔案。


OverWrite - 覆蓋
boolean. 預設值:false

選擇性。 取代目的檔案夾中的現有檔案。


flattenFolders - 扁平化資料夾
boolean. 預設值:false

選擇性。 將資料夾結構扁平化,並將所有檔案複製到指定的目的檔案夾。


preserveTimestamp - 保留目標時間戳記
boolean. 預設值:false

使用原始原始程式檔保留目標檔案時間戳記。


retryCount - 重試計數以複製檔案
string. 預設值:0

指定要複製檔案的重試計數。 此字串適用于間歇性問題,例如遠端主機上的 UNC 目標路徑。


delayBetweenRetries - 兩次重試之間的延遲。
string. 預設值:1000

指定兩次重試之間的延遲。 此字串適用于間歇性問題,例如遠端主機上的 UNC 目標路徑。


ignoreMakeDirErrors - 忽略目的檔案夾建立期間的錯誤。
boolean. 預設值:false

忽略在建立目的檔案夾期間發生的錯誤。 此字串適用于避免單一目的檔案夾中數個代理程式平行執行工作的問題。


工作控制選項

除了工作輸入之外,所有工作都有控制選項。 如需詳細資訊,請參閱 控制選項和一般工作屬性

輸出變數

無。

備註

如果沒有相符的檔案,工作仍會回報成功。

  • 如果 Overwritefalse 且目的檔案夾中已有相符的檔案,工作將不會回報失敗,但記錄檔案已經存在並略過。
  • 如果 Overwritetrue 且目的檔案夾中已有相符的檔案,則會覆寫相符的檔案。

範例

將檔案複製到成品臨時目錄併發布

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

複製可執行檔和讀我檔案

目標

您想要只複製讀我檔案,以及執行此 C# 主控台應用程式所需的檔案:

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

注意

ConsoleApplication1.sln 包含具有 .dll 和 .exe 檔案的 bin 資料夾,請參閱下列結果以查看移動的內容!

在 [變數] 索引標籤上, $(BuildConfiguration) 設定為 release

具有多個比對模式的範例:

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

OR 條件的範例:

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

NOT 條件的範例:

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

內容區段中變數的範例

- task: CopyFiles@2
  inputs:
    Contents: '$(Build.Repository.LocalPath)/**' 
    TargetFolder: '$(Build.ArtifactStagingDirectory)'

結果

這些檔案會複製到預備目錄:

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

從來源目錄複寫所有專案,但 .git 資料夾除外

具有多個比對模式的範例:

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

需求

需求 描述
管線類型 YAML,傳統組建
執行于 Agent、DeploymentGroup
要求
Capabilities 此工作不符合作業中後續工作的任何需求。
命令限制 此工作會使用下列 命令限制來執行:受限制
Settable 變數 此工作有權 設定下列變數:已停用設定變數
代理程式版本 2.182.1 或更新版本
工作類別 公用程式
需求 描述
管線類型 YAML,傳統組建
執行于 Agent、DeploymentGroup
要求
Capabilities 此工作不符合作業中後續工作的任何需求。
命令限制 任何
Settable 變數 任何
代理程式版本 1.91.0 或更新版本
工作類別 公用程式

另請參閱