

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

# CodeBuild の buildspec ファイルサンプルのランタイムバージョン
<a name="sample-runtime-versions"></a>

Amazon Linux 2 (AL2) 標準イメージバージョン 1.0 以降、または Ubuntu 標準イメージバージョン 2.0 以降を使用している場合は、buildspec ファイルの `runtime-versions` セクションで 1 つ以上のランタイムを指定できます。次のサンプルでは、プロジェクトランタイムを変更する方法、複数のランタイムを指定する方法、および別のランタイムに依存するランタイムを指定する方法を示します。サポートされているランタイムについては、「[CodeBuild に用意されている Docker イメージ](build-env-ref-available.md)」を参照してください。

**注記**  
ビルドコンテナで Docker を使用している場合、ビルドは特権モードで実行する必要があります。詳細については、「[AWS CodeBuild ビルドを手動で実行する](run-build.md)」および「[でのビルドプロジェクトの作成AWS CodeBuild](create-project.md)」を参照してください。

**Topics**
+ [buildspec ファイル内のランタイムバージョンを更新](sample-runtime-update-version.md)
+ [2 つのランタイムの指定](sample-runtime-two-major-version-runtimes.md)

# buildspec ファイル内のランタイムバージョンを更新
<a name="sample-runtime-update-version"></a>

プロジェクトで使用されるランタイムを新しいバージョンに変更するには、buildpec ファイルの `runtime-versions` セクションを更新します。以下の例では、Java バージョン 8 および 11 を指定する方法を示します。
+ Java バージョン 8 を指定する `runtime-versions` セクション:

  ```
  phases:
    install:
      runtime-versions:
        java: corretto8
  ```
+ Java バージョン 11 を指定する `runtime-versions` セクション:

  ```
  phases:
    install:
      runtime-versions:
        java: corretto11
  ```

次の例では、Ubuntu 標準イメージ 5.0 または Amazon Linux 2 標準イメージ 3.0 を使用して、Python の異なるバージョンを指定する方法を示しています。
+ Python バージョン 3.7 を指定する `runtime-versions` セクション: 

  ```
  phases:
    install:
      runtime-versions:
        python: 3.7
  ```
+ Python バージョン 3.8 を指定する `runtime-versions` セクション: 

  ```
  phases:
    install:
      runtime-versions:
        python: 3.8
  ```

このサンプルでは、Java バージョン 8 ランタイムで始まり、その後で Java バージョン 10 ランタイムに更新されるプロジェクトを示します。

1. Maven をダウンロードし、インストールします。詳細については、Apache Maven ウェブサイトの「[Apache Maven のダウンロード](https://maven.apache.org/download.cgi)」および「[Apache Maven のインストール](https://maven.apache.org/install.html)」を参照してください。

1. ローカルコンピュータまたはインスタンスの空のディレクトリに切り替えて、この Maven コマンドを実行します。

   ```
   mvn archetype:generate "-DgroupId=com.mycompany.app" "-DartifactId=ROOT" "-DarchetypeArtifactId=maven-archetype-webapp" "-DinteractiveMode=false"
   ```

   成功すると、このディレクトリ構造とファイルが作成されます。

   ```
   .
   └── ROOT
       ├── pom.xml
       └── src
           └── main
               ├── resources
               └── webapp
                   ├── WEB-INF
                   │   └── web.xml
                   └── index.jsp
   ```

1. 次の内容で、`buildspec.yml` というファイルを作成します。ファイルを ` (root directory name)/my-web-app` ディレクトリ内に保存します。

   ```
   version: 0.2
   
   phases:
     install:
       runtime-versions:
         java: corretto8
     build:
       commands:
         - java -version
         - mvn package
   artifacts:
     files:
       - '**/*'
     base-directory: 'target/my-web-app'
   ```

   buildspec ファイル: 
   + この `runtime-versions` セクションでは、プロジェクトでバージョン 8 のJava ランタイムを使用することを指定します。
   + この `- java -version` コマンドは、ビルド時にプロジェクトで使用されている Java のバージョンを表示します。

   ファイル構造は次のようになります。

   ```
   (root directory name)
   └── my-web-app
       ├── src
       │   ├── main
       │   ├── resources
       │   └── webapp
       │       └── WEB-INF
       │           └── web.xml
       │               └── index.jsp
       ├── buildspec.yml
       └── pom.xml
   ```

1. 「`my-web-app`」ディレクトリの内容を、S3 入力バケットにアップロードするか、CodeCommit、GitHub、または Bitbucket リポジトリにアップロードします。
**重要**  
`(root directory name)` または `(root directory name)/my-web-app` をアップロードしないでください。アップロードするのは、`(root directory name)/my-web-app` のディレクトリとファイルだけです。  
S3 入力バケットを使用している場合は、ディレクトリ構造とファイルを必ず ZIP ファイルに圧縮してから入力バケットにアップロードしてください。`(root directory name)` または `(root directory name)/my-web-app` を ZIP ファイルに追加しないでください。追加するのは、`(root directory name)/my-web-app` のディレクトリとファイルだけです。

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. [**Start build**] を選択します。

1. [**ビルド設定**] でデフォルト値をそのまま使用して、[**ビルドの開始**] を選択します。

1. ビルドが完了したら、[**ビルドログ**] タブでビルド出力を表示します。次のような出力が表示されます。

   ```
   [Container] Date Time Phase is DOWNLOAD_SOURCE
   [Container] Date Time CODEBUILD_SRC_DIR=/codebuild/output/src460614277/src
   [Container] Date Time YAML location is /codebuild/output/src460614277/src/buildspec.yml
   [Container] Date Time Processing environment variables
   [Container] Date Time Selecting 'java' runtime version 'corretto8' based on manual selections...
   [Container] Date Time Running command echo "Installing Java version 8 ..."
   Installing Java version 8 ... 
    
   [Container] Date Time Running command export JAVA_HOME="$JAVA_8_HOME" 
    
   [Container] Date Time Running command export JRE_HOME="$JRE_8_HOME" 
    
   [Container] Date Time Running command export JDK_HOME="$JDK_8_HOME" 
    
   [Container] Date Time Running command for tool_path in "$JAVA_8_HOME"/bin/* "$JRE_8_HOME"/bin/*;
   ```

1. `runtime-versions` セクションを Java バージョン 11 で更新します。

   ```
   install:
       runtime-versions:
         java: corretto11
   ```

1. 変更を保存したら、ビルドを再実行し、ビルド出力を表示します。現在インストールされている Java のバージョンが 11 であることが表示されます。次のような出力が表示されます: 

   ```
   [Container] Date Time Phase is DOWNLOAD_SOURCE
   [Container] Date Time CODEBUILD_SRC_DIR=/codebuild/output/src460614277/src
   [Container] Date Time YAML location is /codebuild/output/src460614277/src/buildspec.yml
   [Container] Date Time Processing environment variables
   [Container] Date Time Selecting 'java' runtime version 'corretto11' based on manual selections... 
   Installing Java version 11 ... 
    
   [Container] Date Time Running command export JAVA_HOME="$JAVA_11_HOME" 
    
   [Container] Date Time Running command export JRE_HOME="$JRE_11_HOME" 
    
   [Container] Date Time Running command export JDK_HOME="$JDK_11_HOME" 
    
   [Container] Date Time Running command for tool_path in "$JAVA_11_HOME"/bin/* "$JRE_11_HOME"/bin/*;
   ```

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