AWS CodeDeploy
User Guide (API 版本 2014-10-06)
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 Amazon AWS 入门

将应用程序规范文件添加到 AWS CodeDeploy 的修订

本主题介绍如何向部署中添加 AppSpec file。它还包括用来为 AWS Lambda 和 EC2/本地 部署创建 AppSpec file 的模板。

为 AWS Lambda 部署添加 AppSpec 文件

对于到 AWS Lambda 计算平台 的部署:

  • AppSpec file 包含有关要部署并用于部署验证的 Lambda 函数的说明。

  • 修订和 AppSpec file 相同。

  • AppSpec file 可以使用 JSON 格式或 YAML 编写。

  • 在创建部署时,可以将 AppSpec file 另存为文本文件或直接输入控制台 AppSpec 编辑器中。有关更多信息,请参阅 创建 AWS Lambda 计算平台 部署 (控制台)

创建 AppSpec file:

  1. 将 JSON 或 YAML 模板复制到文本编辑器或输入控制台中的 AppSpec 编辑器。

  2. 根据需要修改模板。

  3. 使用 JSON 或 YAML 验证程序验证 AppSpec file。如果您使用 AppSpec 编辑器,则会在您选择 Deploy 时验证该文件。

  4. 如果您使用文本编辑器,请保存该文件。如果您使用 AWS CLI 创建部署,请在 AppSpec file 位于硬盘驱动器上或 Amazon S3 存储桶中时引用它。如果您使用控制台,则必须将 AppSpec file 推送到 Amazon S3。

  5. (可选) 如果使用 AppSpec 编辑器,请选择 Save as text file 以将 AppSpec file 保存到您的硬盘驱动器上。

带有说明的 YAML AppSpec 文件模板

有关在 hooks 部分中使用的生命周期事件的信息,请参阅用于 AWS Lambda 部署的 AppSpec 的“hooks”部分

# This is an appspec.yml template file for use with an AWS Lambda deployment in AWS CodeDeploy. # The lines in this template starting with the hashtag symbol are # instructional comments and can be safely left in the file or # ignored. # For help completing this file, see the "AppSpec File Reference" in the # "AWS CodeDeploy User Guide" at # http://docs.amazonaws.cn/codedeploy/latest/userguide/app-spec-ref.html version: 0.0 # In the Resources section specify the name, alias, # target version, and (optional) the current version of your AWS Lambda function. Resources: - MyFunction: # Replace "MyFunction" with the name of your Lambda function Type: AWS::Lambda::Function Properties: Name: "" # Specify the name of your Lambda function Alias: "" # Specify the alias for your Lambda function CurrentVersion: "" # Specify the current version of your Lambda function TargetVersion: "" # Specify the version of your Lambda function to deploy # (Optional) In the Hooks section, specify a validation Lambda function to run during # a lifecycle event. Replace "LifeCycleEvent" with BeforeAllowTraffic # or AfterAllowTraffic. Hooks: - LifeCycleEvent: "" # Specify a Lambda validation function between double-quotes.

JSON AppSpec 文件模板

在以下模板中,您需要将“MyFunction”替换为 AWS Lambda 函数的名称。在可选的 Hooks 部分,将生命周期事件替换为 BeforeAllowTraffic 或 AfterAllowTraffic。

有关在 Hooks 部分中使用的生命周期事件的信息,请参阅用于 AWS Lambda 部署的 AppSpec 的“hooks”部分

{ "version": 0.0, "Resources": [{ "MyFunction": { "Type": "AWS::Lambda::Function", "Properties": { "Name": "", "Alias": "", "CurrentVersion": "", "TargetVersion": "" } } }], "Hooks": [{ "LifeCycleEvent": "" } ] }

为 EC2/本地 部署添加 AppSpec 文件

如果没有 AppSpec file,AWS CodeDeploy 无法将应用程序修订中的源文件映射到其目标,也无法为您向 EC2/本地 计算平台 中进行的部署运行脚本。

每个修订只能包含一个 AppSpec file。

将 AppSpec file添加到修订:

  1. 将模板复制到文本编辑器。

  2. 根据需要修改模板。

  3. 使用 YAML 验证程序检查您的 AppSpec file 是否有效。

  4. 在修订的根目录中将文件另存为 appspec.yml

  5. 运行以下命令之一,验证您是否已将 AppSpec file 放置在根目录中:

    • 对于 Linux, OS X, or Unix:

      find /path/to/root/directory -name appspec.yml

      如果 AppSpec file 不在根目录中,则不输出任何内容。

    • 对于 Windows:

      dir path\to\root\directory\appspec.yml

      如果 AppSpec file 未保存在根目录中,则会显示 File Not Found 错误。

  6. 将修订推送到 Amazon S3 或 GitHub。

    有关说明,请参阅将 AWS CodeDeploy 的修订推送到 Amazon S3

带有说明的 AppSpec 文件模板

# This is an appspec.yml template file for use with an EC2/本地 deployment in AWS CodeDeploy. # The lines in this template starting with the hashtag symbol are # instructional comments and can be safely left in the file or # ignored. # For help completing this file, see the "AppSpec File Reference" in the # "AWS CodeDeploy User Guide" at # http://docs.amazonaws.cn/codedeploy/latest/userguide/app-spec-ref.html version: 0.0 # Specify "os: linux" if this revision targets Amazon Linux, # Red Hat Enterprise Linux (RHEL), or Ubuntu Server # instances. # Specify "os: windows" if this revision targets Windows Server instances. # (You cannot specify both "os: linux" and "os: windows".) os: linux # os: windows # During the Install deployment lifecycle event (which occurs between the # BeforeInstall and AfterInstall events), copy the specified files # in "source" starting from the root of the revision's file bundle # to "destination" on the Amazon EC2 instance. # Specify multiple "source" and "destination" pairs if you want to copy # from multiple sources or to multiple destinations. # If you are not copying any files to the Amazon EC2 instance, then remove the # "files" section altogether. A blank or incomplete "files" section # may cause associated deployments to fail. files: - source: destination: - source: destination: # For deployments to Amazon Linux, Ubuntu Server, or RHEL instances, # you can specify a "permissions" # section here that describes special permissions to apply to the files # in the "files" section as they are being copied over to # the Amazon EC2 instance. # For more information, see the documentation. # If you are deploying to Windows Server instances, # then remove the # "permissions" section altogether. A blank or incomplete "permissions" # section may cause associated deployments to fail. permissions: - object: pattern: except: owner: group: mode: acls: - context: user: type: range: type: - # If you are not running any commands on the Amazon EC2 instance, then remove # the "hooks" section altogether. A blank or incomplete "hooks" section # may cause associated deployments to fail. hooks: # For each deployment lifecycle event, specify multiple "location" entries # if you want to run multiple scripts during that event. # You can specify "timeout" as the number of seconds to wait until failing the deployment # if the specified scripts do not run within the specified time limit for the # specified event. For example, 900 seconds is 15 minutes. If not specified, # the default is 1800 seconds (30 minutes). # Note that the maximum amount of time that all scripts must finish executing # for each individual deployment lifecycle event is 3600 seconds (1 hour). # Otherwise, the deployment will stop and AWS CodeDeploy will consider the deployment # to have failed to the Amazon EC2 instance. Make sure that the total number of seconds # that are specified in "timeout" for all scripts in each individual deployment # lifecycle event does not exceed a combined 3600 seconds (1 hour). # For deployments to Amazon Linux, Ubuntu Server, or RHEL instances, # you can specify "runas" in an event to # run as the specified user. For more information, see the documentation. # If you are deploying to Windows Server instances, # remove "runas" altogether. # If you do not want to run any commands during a particular deployment # lifecycle event, remove that event declaration altogether. Blank or # incomplete event declarations may cause associated deployments to fail. # During the ApplicationStop deployment lifecycle event, run the commands # in the script specified in "location" starting from the root of the # revision's file bundle. ApplicationStop: - location: timeout: runas: - location: timeout: runas: # During the BeforeInstall deployment lifecycle event, run the commands # in the script specified in "location". BeforeInstall: - location: timeout: runas: - location: timeout: runas: # During the AfterInstall deployment lifecycle event, run the commands # in the script specified in "location". AfterInstall: - location: timeout: runas: - location: timeout: runas: # During the ApplicationStart deployment lifecycle event, run the commands # in the script specified in "location". ApplicationStart: - location: timeout: runas: - location: timeout: runas: # During the ValidateService deployment lifecycle event, run the commands # in the script specified in "location". ValidateService: - location: timeout: runas: - location: timeout: runas: