AppSpec 「檔案」區段 (僅適用於 EC2 /內部部署) - AWS CodeDeploy

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

AppSpec 「檔案」區段 (僅適用於 EC2 /內部部署)

提供 CodeDeploy 有關在部署的 Install 事件期間,應在執行個體上安裝應該從應用程式修訂版本中的哪些檔案的相關資訊。唯有您在部署期間將修訂中的檔案複製至執行個體上的位置時,會需要本區段。

本區段的結構如下:

files: - source: source-file-location-1 destination: destination-file-location-1 file_exists_behavior: DISALLOW|OVERWRITE|RETAIN

您可以設定多個 sourcedestination 配對。

source 說明識別要從修訂複製至執行個體的檔案或目錄:

  • 如果 source 是指檔案,只會將指定的檔案複製至執行個體。

  • 如果 source 是指目錄,會將該目錄中的所有檔案都複製至執行個體。

  • 如果source是單一斜線 (Amazon Linux、RHEL 和 Ubuntu 伺服器執行個體為「/」,或是 Windows 伺服器執行個體的「\」),則修訂版本中的所有檔案都會複製到執行個體。

中使用的路徑source是相對於appspec.yml檔案的路徑,該路徑應位於修訂的根目錄。如需修訂檔案結構的詳細資訊,請參閱規劃修訂 CodeDeploy

destination 說明識別執行個體上應該複製檔案的位置。這必須是一個完全合格的路徑,例如/root/destination/directory(在 Linux,RHEL 和 Ubuntu 上)或c:\destination\folder(在視窗上)。

sourcedestination 各以字串指定。

此指file_exists_behavior令是選擇性的,並指定如何 CodeDeploy處理已存在於部署目標位置中,但不是先前成功部署的一部分的檔案。此設定可採用下列任一值:

  • 不允許:部署失敗。如果未指定選項,這也是預設行為。

  • 覆寫:目前部署的應用程式修訂版本中的檔案版本會取代執行個體上已有的版本。

  • RETAIN:系統會保留執行個體上已存在的檔案版本,並做為新部署的一部分使用。

使用此file_exists_behavior設定時,請瞭解此設定:

  • 只能指定一次,並套用至下列出的所有檔案和目錄files:

  • 優先於選--file-exists-behavior AWS CLI 項和 fileExistsBehavior API 選項(兩者都是可選的)。

以下是 Amazon Linux、Ubuntu 伺服器或 RHEL 執行個體的範例files區段。

files: - source: Config/config.txt destination: /webapps/Config - source: source destination: /webapps/myApp

在本範例中,會在 Install 事件期間執行下列兩個操作:

  1. 將修訂中的 Config/config.txt 檔案複製至執行個體上的 /webapps/Config/config.txt 路徑。

  2. 將修訂 source 目錄中的所有檔案都遞迴複製至執行個體上的 /webapps/myApp 目錄。

「文件」部分的例子

下列範例顯示如何指定 files 區段。儘管這些示例描述了 Windows 服務器文件和目錄(文件夾)結構,但它們可以很容易地適用於 Amazon Linux,Ubuntu 服務器和 RHEL 實例。

注意

只有 EC2 /內部部署使用此區段。files它不適用於 AWS Lambda 部署。

在下列範例中,假設這些檔案出現在 source 根目錄的套件中:

  • appspec.yml

  • my-file.txt

  • my-file-2.txt

  • my-file-3.txt

# 1) Copy only my-file.txt to the destination folder c:\temp. # files: - source: .\my-file.txt destination: c:\temp # # Result: # c:\temp\my-file.txt # # --------------------- # # 2) Copy only my-file-2.txt and my-file-3.txt to the destination folder c:\temp. # files: - source: my-file-2.txt destination: c:\temp - source: my-file-3.txt destination: c:\temp # # Result: # c:\temp\my-file-2.txt # c:\temp\my-file-3.txt # # --------------------- # # 3) Copy my-file.txt, my-file-2.txt, and my-file-3.txt (along with the appspec.yml file) to the destination folder c:\temp. # files: - source: \ destination: c:\temp # # Result: # c:\temp\appspec.yml # c:\temp\my-file.txt # c:\temp\my-file-2.txt # c:\temp\my-file-3.txt

在下列範例中,假設 appspec.yml 出現在 source 根目錄以及名為 my-folder 資料夾 (包含三個檔案) 的套件中:

  • appspec.yml

  • my-folder\my-file.txt

  • my-folder\my-file-2.txt

  • my-folder\my-file-3.txt

# 4) Copy the 3 files in my-folder (but do not copy my-folder itself) to the destination folder c:\temp. # files: - source: .\my-folder destination: c:\temp # # Result: # c:\temp\my-file.txt # c:\temp\my-file-2.txt # c:\temp\my-file-3.txt # # --------------------- # # 5) Copy my-folder and its 3 files to my-folder within the destination folder c:\temp. # files: - source: .\my-folder destination: c:\temp\my-folder # # Result: # c:\temp\my-folder\my-file.txt # c:\temp\my-folder\my-file-2.txt # c:\temp\my-folder\my-file-3.txt # # --------------------- # # 6) Copy the 3 files in my-folder to other-folder within the destination folder c:\temp. # files: - source: .\my-folder destination: c:\temp\other-folder # # Result: # c:\temp\other-folder\my-file.txt # c:\temp\other-folder\my-file-2.txt # c:\temp\other-folder\my-file-3.txt # # --------------------- # # 7) Copy only my-file-2.txt and my-file-3.txt to my-folder within the destination folder c:\temp. # files: - source: .\my-folder\my-file-2.txt destination: c:\temp\my-folder - source: .\my-folder\my-file-3.txt destination: c:\temp\my-folder # # Result: # c:\temp\my-folder\my-file-2.txt # c:\temp\my-folder\my-file-3.txt # # --------------------- # # 8) Copy only my-file-2.txt and my-file-3.txt to other-folder within the destination folder c:\temp. # files: - source: .\my-folder\my-file-2.txt destination: c:\temp\other-folder - source: .\my-folder\my-file-3.txt destination: c:\temp\other-folder # # Result: # c:\temp\other-folder\my-file-2.txt # c:\temp\other-folder\my-file-3.txt # # --------------------- # # 9) Copy my-folder and its 3 files (along with the appspec.yml file) to the destination folder c:\temp. If any of the files already exist on the instance, overwrite them. # files: - source: \ destination: c:\temp file_exists_behavior: OVERWRITE # # Result: # c:\temp\appspec.yml # c:\temp\my-folder\my-file.txt # c:\temp\my-folder\my-file-2.txt # c:\temp\my-folder\my-file-3.txt