

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 步骤 2：创建示例应用程序修订
<a name="tutorials-on-premises-instance-2-create-sample-revision"></a>

在这一步中，您将创建要部署到本地实例的示例应用程序修订。

由于很难了解您的本地实例上已经安装了哪些软件和功能 - 或者您的组织策略允许安装哪些软件和功能 - 因此，我们在此处提供的示例应用程序修订仅使用批处理脚本（对于 Windows Server）或 shell 脚本（对于 Ubuntu Server 和 RHEL），将文本文件写入您本地实例上的某个位置。为多个 CodeDeploy 部署生命周期事件（包括 In **stall**、**AfterInstall**ApplicationStart****、和）中的每个事件写入一个文件**ValidateService**。在**BeforeInstall**部署生命周期事件期间，将运行一个脚本来删除此示例的先前部署期间写入的旧文件，并在您的本地实例上创建一个写入新文件的位置。

**注意**  
如果出现以下任何情况，此示例应用程序修订可能无法部署：  
在本地实例上启动 CodeDeploy 代理的用户没有执行脚本的权限。
用户无权在脚本中列出的位置创建或删除文件夹。
用户无权在脚本中列出的位置创建文本文件。

**注意**  
如果您配置了 Windows Server 实例并希望部署其他示例，则可能需要使用[教程：使用 CodeDeploy 部署“Hello, World\$1” 带有 CodeDeploy （Windows 服务器）的应用程序](tutorials-windows.md)教程的[步骤 2：将您的源内容配置为部署到 Windows Server Amazon EC2 实例](tutorials-windows-configure-content.md)中的示例。  
如果您配置了 RHEL 实例并希望部署其他示例，则可能需要使用[教程：部署 WordPress 到亚马逊 EC2 实例（亚马逊 Linux 或红帽企业 Linux 和 Linux、macOS 或 Unix）](tutorials-wordpress.md)教程的[步骤 2：将源内容配置为部署到 Amazon Linux 或 Red Hat Enterprise Linux Amazon EC2 实例](tutorials-wordpress-configure-content.md)中的示例。  
目前，Ubuntu Server 没有替代示例。

1. 在您的开发计算机上，创建名为 `CodeDeployDemo-OnPrem` 的子目录（子文件夹）来存储示例应用程序修订的文件，然后切换到该子文件夹。在此示例中，我们假定您将使用 `c:\temp` 文件夹作为 Windows Server 的根文件夹，或者使用 `/tmp` 文件夹作为 Ubuntu Server 和 RHEL 的根文件夹。如果您使用其他文件夹，请务必在整个教程中使用该文件夹取代我们提供的文件夹：

   对于 Windows：

   ```
   mkdir c:\temp\CodeDeployDemo-OnPrem
   cd c:\temp\CodeDeployDemo-OnPrem
   ```

   对于 Linux、macOS 或 Unix：

   ```
   mkdir /tmp/CodeDeployDemo-OnPrem
   cd /tmp/CodeDeployDemo-OnPrem
   ```

1. 在 `CodeDeployDemo-OnPrem` 子文件夹的根中，使用文本编辑器创建两个分别名为 `appspec.yml` 和 `install.txt` 的文件：

   对于 Windows Server 为 `appspec.yml`：

   ```
   version: 0.0
   os: windows
   files:
     - source: .\install.txt
       destination: c:\temp\CodeDeployExample
   hooks:
     BeforeInstall:
       - location: .\scripts\before-install.bat
         timeout: 900
     AfterInstall:
       - location: .\scripts\after-install.bat     
         timeout: 900
     ApplicationStart:
       - location: .\scripts\application-start.bat  
         timeout: 900
     ValidateService:
       - location: .\scripts\validate-service.bat    
         timeout: 900
   ```

   对于 Ubuntu Server 和 RHEL 为 `appspec.yml`：

   ```
   version: 0.0
   os: linux
   files:
     - source: ./install.txt
       destination: /tmp/CodeDeployExample
   hooks:
     BeforeInstall:
       - location: ./scripts/before-install.sh
         timeout: 900
     AfterInstall:
       - location: ./scripts/after-install.sh
         timeout: 900
     ApplicationStart:
       - location: ./scripts/application-start.sh
         timeout: 900
     ValidateService:
       - location: ./scripts/validate-service.sh
         timeout: 900
   ```

   有关 AppSpec 文件的更多信息，请参阅[将应用程序规范文件添加到修订版中 CodeDeploy](application-revisions-appspec-file.md)和[CodeDeploy AppSpec 文件引用](reference-appspec-file.md)。

   `install.txt`:

   ```
   The Install deployment lifecycle event successfully completed.
   ```

1. 在 `CodeDeployDemo-OnPrem` 子文件夹的根下，创建 `scripts` 子文件夹，然后切换到该子文件夹：

   对于 Windows：

   ```
   mkdir c:\temp\CodeDeployDemo-OnPrem\scripts
   cd c:\temp\CodeDeployDemo-OnPrem\scripts
   ```

   对于 Linux、macOS 或 Unix：

   ```
   mkdir -p /tmp/CodeDeployDemo-OnPrem/scripts
   cd /tmp/CodeDeployDemo-OnPrem/scripts
   ```

1. 在 `scripts` 子文件夹的根中，使用文本编辑器创建 4 个文件（对于 Windows Server，它们分别名为 `before-install.bat`、`after-install.bat`、`application-start.bat` 和 `validate-service.bat`；对于 Ubuntu Server 和 RHEL，它们分别名为 `before-install.sh`、`after-install.sh`、`application-start.sh` 和 `validate-service.sh`）：

   对于 Windows Server：

   `before-install.bat`:

   ```
   set FOLDER=%HOMEDRIVE%\temp\CodeDeployExample
   
   if exist %FOLDER% (
     rd /s /q "%FOLDER%"
   )
   
   mkdir %FOLDER%
   ```

   `after-install.bat`:

   ```
   cd %HOMEDRIVE%\temp\CodeDeployExample
   
   echo The AfterInstall deployment lifecycle event successfully completed. > after-install.txt
   ```

   `application-start.bat`:

   ```
   cd %HOMEDRIVE%\temp\CodeDeployExample
   
   echo The ApplicationStart deployment lifecycle event successfully completed. > application-start.txt
   ```

   `validate-service.bat`:

   ```
   cd %HOMEDRIVE%\temp\CodeDeployExample
   
   echo The ValidateService deployment lifecycle event successfully completed. > validate-service.txt
   ```

   对于 Ubuntu Server 和 RHEL：

   `before-install.sh`:

   ```
   #!/bin/bash
   export FOLDER=/tmp/CodeDeployExample
   
   if [ -d $FOLDER ]
   then
    rm -rf $FOLDER
   fi
   
   mkdir -p $FOLDER
   ```

   `after-install.sh`:

   ```
   #!/bin/bash
   cd /tmp/CodeDeployExample
   
   echo "The AfterInstall deployment lifecycle event successfully completed." > after-install.txt
   ```

   `application-start.sh`:

   ```
   #!/bin/bash
   cd /tmp/CodeDeployExample
   
   echo "The ApplicationStart deployment lifecycle event successfully completed." > application-start.txt
   ```

   `validate-service.sh`:

   ```
   #!/bin/bash
   cd /tmp/CodeDeployExample
   
   echo "The ValidateService deployment lifecycle event successfully completed." > validate-service.txt
   
   unset FOLDER
   ```

1. 仅对于 Ubuntu Server 和 RHEL，确保四个 shell 脚本都具有执行权限：

   ```
   chmod +x ./scripts/*
   ```