

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

# 指定两个运行时
<a name="sample-runtime-two-major-version-runtimes"></a>

您可以在同一个 CodeBuild 构建项目中指定多个运行时。此示例项目使用两个源文件：一个使用 Go 运行时，另一个使用 Node.js 运行时。

1. 创建一个名为 `my-source` 的目录。

1. 在 `my-source` 目录中，创建一个名为 `golang-app` 的目录。

1. 使用以下内容创建名为 `hello.go` 的文件。将此文件存储到 `golang-app` 目录。

   ```
   package main
   import "fmt"
   
   func main() {
     fmt.Println("hello world from golang")
     fmt.Println("1+1 =", 1+1)
     fmt.Println("7.0/3.0 =", 7.0/3.0)
     fmt.Println(true && false)
     fmt.Println(true || false)
     fmt.Println(!true)
     fmt.Println("good bye from golang")
   }
   ```

1. 在 `my-source` 目录中，创建一个名为 `nodejs-app` 的目录。它应该与 `golang-app` 目录同级。

1. 使用以下内容创建名为 `index.js` 的文件。将此文件存储到 `nodejs-app` 目录。

   ```
   console.log("hello world from nodejs");
   console.log("1+1 =" + (1+1));
   console.log("7.0/3.0 =" + 7.0/3.0);
   console.log(true && false);
   console.log(true || false);
   console.log(!true);
   console.log("good bye from nodejs");
   ```

1. 使用以下内容创建名为 `package.json` 的文件。将此文件存储到 `nodejs-app` 目录。

   ```
   {
     "name": "mycompany-app",
     "version": "1.0.0",
     "description": "",
     "main": "index.js",
     "scripts": {
       "test": "echo \"run some tests here\""
     },
     "author": "",
     "license": "ISC"
   }
   ```

1. 使用以下内容创建名为 `buildspec.yml` 的文件。将文件存储在 `my-source` 目录中，该目录与 `nodejs-app` 以及 `golang-app` 目录同级。`runtime-versions` 部分指定 Node.js 版本 12 运行时和 Go 版本 1.13 运行时。

   ```
   version: 0.2
   
   phases:
     install:
       runtime-versions:
         golang: 1.13
         nodejs: 12
     build:
       commands:
         - echo Building the Go code...
         - cd $CODEBUILD_SRC_DIR/golang-app
         - go build hello.go 
         - echo Building the Node code...
         - cd $CODEBUILD_SRC_DIR/nodejs-app
         - npm run test
   artifacts:
     secondary-artifacts:
       golang_artifacts:
         base-directory: golang-app
         files:
           - hello
       nodejs_artifacts:
         base-directory: nodejs-app
         files:
           - index.js
           - package.json
   ```

1. 您的文件结构现在应如下所示。

   ```
   my-source
   ├── golang-app
   │   └── hello.go
   ├── nodejs.app
   │   ├── index.js
   │   └── package.json
   └── buildspec.yml
   ```

1. 将`my-source`目录的内容上传到 S3 输入存储桶或 CodeCommit GitHub、或 Bitbucket 存储库。
**重要**  
 如果您使用的是 S3 输入存储桶，请确保创建一个包含目录结构和文件的 ZIP 文件，然后将其上传至输入存储桶。请不要将 `my-source` 添加到 ZIP 文件中，而只添加 `my-source` 中的目录和文件。

1. 在 [https://console.aws.amazon.com/codesuite/codebuild](https://console.aws.amazon.com/codesuite/codebuild/home) /home 中打开 AWS CodeBuild 控制台。

1. 创建构建项目。有关更多信息，请参阅[创建构建项目（控制台）](create-project.md#create-project-console)和[运行构建（控制台）](run-build-console.md)。除这些设置以外，将所有设置保留为默认值。
   + 对于**环境**：
     + 对于**环境映像**，选择**托管映像**。
     + 对于**操作系统**，选择 **Amazon Linux 2**。
     + 对于**运行时**，选择**标准**。
     + 对于**图像**，选择 **aws/codebuild/amazonlinux-x** 86\$164-standards: 4.0。

1. 选择 **Create build project（创建构建项目）**。

1. 选择**开始构建**。

1. 在**构建配置**上，接受默认值，然后选择**开始构建**。

1. 当构建完成后，在**构建日志**选项卡上查看构建输出。您应该可以看到类似于如下所示的输出内容。它显示来自 Go 和 Node.js 运行时的输出，还显示来自 Go 和 Node.js 应用程序的输出。

   ```
   [Container] Date Time Processing environment variables
   [Container] Date Time Selecting 'golang' runtime version '1.13' based on manual selections...
   [Container] Date Time Selecting 'nodejs' runtime version '12' based on manual selections...
   [Container] Date Time Running command echo "Installing Go version 1.13 ..."
   Installing Go version 1.13 ... 
    
   [Container] Date Time Running command echo "Installing Node.js version 12 ..." 
   Installing Node.js version 12 ... 
    
   [Container] Date Time Running command n $NODE_12_VERSION
      installed : v12.20.1 (with npm 6.14.10)
   
   [Container] Date Time Moving to directory /codebuild/output/src819694850/src
   [Container] Date Time Registering with agent
   [Container] Date Time Phases found in YAML: 2
   [Container] Date Time  INSTALL: 0 commands
   [Container] Date Time  BUILD: 1 commands
   [Container] Date Time Phase complete: DOWNLOAD_SOURCE State: SUCCEEDED
   [Container] Date Time Phase context status code:  Message:
   [Container] Date Time Entering phase INSTALL
   [Container] Date Time Phase complete: INSTALL State: SUCCEEDED
   [Container] Date Time Phase context status code:  Message:  
   [Container] Date Time Entering phase PRE_BUILD 
   [Container] Date Time Phase complete: PRE_BUILD State: SUCCEEDED 
   [Container] Date Time Phase context status code:  Message:  
   [Container] Date Time Entering phase BUILD 
   [Container] Date Time Running command echo Building the Go code... 
   Building the Go code... 
    
   [Container] Date Time Running command cd $CODEBUILD_SRC_DIR/golang-app 
    
   [Container] Date Time Running command go build hello.go 
    
   [Container] Date Time Running command echo Building the Node code... 
   Building the Node code... 
    
   [Container] Date Time Running command cd $CODEBUILD_SRC_DIR/nodejs-app 
    
   [Container] Date Time Running command npm run test 
    
   > mycompany-app@1.0.0 test /codebuild/output/src924084119/src/nodejs-app 
   > echo "run some tests here" 
    
   run some tests here
   ```