

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# 2 つのランタイムの指定
<a name="sample-runtime-two-major-version-runtimes"></a>

同じ CodeBuild ビルドプロジェクトで、複数のランタイムを指定できます。このサンプルプロジェクトでは、2 つのソースファイルを使用します。1 つは Go ランタイムを使用し、もう 1 つは 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/home](https://console.aws.amazon.com/codesuite/codebuild/home) で AWS CodeBuild コンソールを開きます。

1. ビルドプロジェクトを作成します。詳細については、「[ビルドプロジェクトの作成 (コンソール)](create-project.md#create-project-console)」および「[ビルドの実行 (コンソール)](run-build-console.md)」を参照してください。これらの設定を除いて、すべての設定をデフォルト値のままにします。
   + [**環境**] の場合:
     + [**環境イメージ**] で、[**Managed image (マネージド型イメージ)**] を選択します。
     + [**オペレーティングシステム**] で、[**Amazon Linux 2**] を選択します。
     + [**ランタイム**] で、[**Standard (標準)**] を選択します。
     + [**イメージ**] で、[**aws/codebuild/amazonlinux-x86\$164-standard:4.0**] を選択します。

1. [**Create build project (ビルドプロジェクトの作成)**] を選択します。

1. [**Start build**] を選択します。

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
   ```