

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

# 指定兩個執行時間
<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：//[https://console.aws.amazon.com/codesuite/codebuild/home](https://console.aws.amazon.com/codesuite/codebuild/home) 開啟 AWS CodeBuild 主控台。

1. 建立建置專案。如需詳細資訊，請參閱[建立組建專案 (主控台)](create-project.md#create-project-console)及[執行建置 (主控台)](run-build-console.md)。除了下列設定外，保留所有設定的預設值。
   + 針對 **Environment (環境)**：
     + 針對 **Environment image (環境映像)**，選擇 **Managed image (受管映像)**。
     + 針對 **Operating system** (作業系統)，請選擇 **Amazon Linux 2**。
     + 針對 **Runtime(s) (執行時間)**，選擇 **Standard (標準)**。
     + 針對**映像**，選擇 **aws/codebuild/amazonlinux-x86\$164-standard：4.0。**

1. 選擇 **Create build project (建立建置專案)**。

1. 選擇 **Start build (開始組建)**。

1. 在 **Build configuration (組建組態)** 上，接受預設值，然後選擇 **Start build (開始組建)**。

1. 建置完成後，在 **Build logs (建置日誌)** 索引標籤上檢視建置輸出。您應該會看到類似下列的輸出。其中顯示 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
   ```