

# Runtime versions in buildspec file sample for CodeBuild
<a name="sample-runtime-versions"></a>

If you use the Amazon Linux 2 (AL2) standard image version 1.0 or later, or the Ubuntu standard image version 2.0 or later, you can specify one or more runtimes in the `runtime-versions` section of your buildspec file. The following samples show how you can change your project runtime, specify more than one runtime, and specify a runtime that is dependent on another runtime. For information about supported runtimes, see [Docker images provided by CodeBuild](build-env-ref-available.md).

**Note**  
If you use Docker in your build container, your build must run in privileged mode. For more information, see [Run AWS CodeBuild builds manually](run-build.md) and [Create a build project in AWS CodeBuild](create-project.md). 

**Topics**
+ [Update the runtime version in the buildspec file](sample-runtime-update-version.md)
+ [Specify two runtimes](sample-runtime-two-major-version-runtimes.md)

# Update the runtime version in the buildspec file
<a name="sample-runtime-update-version"></a>

You can modify the runtime used by your project to a new version by updating the `runtime-versions` section of your buildspec file. The following examples show how to specify java versions 8 and 11.
+ A `runtime-versions` section that specifies version 8 of Java:

  ```
  phases:
    install:
      runtime-versions:
        java: corretto8
  ```
+ A `runtime-versions` section that specifies version 11 of Java:

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

The following examples show how to specify different versions of Python using the Ubuntu standard image 5.0 or the Amazon Linux 2 standard image 3.0:
+ A `runtime-versions` section that specifies Python version 3.7: 

  ```
  phases:
    install:
      runtime-versions:
        python: 3.7
  ```
+ A `runtime-versions` section that specifies Python version 3.8: 

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

This sample demonstrates a project that starts with the Java version 8 runtime, and then is updated to the Java version 10 runtime. 

1. Download and install Maven. For information, see [Downloading Apache Maven](https://maven.apache.org/download.cgi) and [Installing Apache Maven](https://maven.apache.org/install.html) on the Apache Maven website.

1. Switch to an empty directory on your local computer or instance, and then run this Maven command.

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

   If successful, this directory structure and files are created.

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

1. Create a file named `buildspec.yml` with the following contents. Store the file in the ` (root directory name)/my-web-app` directory. 

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

   In the buildspec file: 
   + The `runtime-versions` section specifies that the project uses version 8 of the Java runtime. 
   + The `- java -version` command displays the version of Java used by your project when it builds. 

   Your file structure should now look like this. 

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

1. Upload the contents of the `my-web-app` directory to an S3 input bucket or a CodeCommit, GitHub, or Bitbucket repository. 
**Important**  
Do not upload `(root directory name)` or `(root directory name)/my-web-app`, just the directories and files in `(root directory name)/my-web-app`.   
If you are using an S3 input bucket, be sure to create a ZIP file that contains the directory structure and files, and then upload it to the input bucket. Do not add `(root directory name)` or `(root directory name)/my-web-app` to the ZIP file, just the directories and files in `(root directory name)/my-web-app`.

1. Open the AWS CodeBuild console at [https://console.aws.amazon.com/codesuite/codebuild/home](https://console.aws.amazon.com/codesuite/codebuild/home).

1. Create a build project. For more information, see [Create a build project (console)](create-project.md#create-project-console) and [Run a build (console)](run-build-console.md). Leave all settings at their default values, except for these settings.
   + For **Environment**:
     + For **Environment image**, choose **Managed image**.
     + For **Operating system**, choose **Amazon Linux 2**. 
     +  For **Runtime(s)**, choose **Standard**. 
     + For **Image**, choose **aws/codebuild/amazonlinux-x86\$164-standard:4.0**.

1. Choose **Start build**. 

1. On **Build configuration**, accept the defaults, and then choose **Start build**. 

1. After the build is complete, view the build output on the **Build logs** tab. You should see output similar to the following: 

   ```
   [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. Update the `runtime-versions` section with Java version 11: 

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

1. After you save the change, run your build again and view the build output. You should see that the installed version of Java is 11. You should see output similar to the following: 

   ```
   [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/*;
   ```

# Specify two runtimes
<a name="sample-runtime-two-major-version-runtimes"></a>

You can specify more than one runtime in the same CodeBuild build project. This sample project uses two source files: one that uses the Go runtime and one that uses the Node.js runtime. 

1. Create a directory named `my-source`. 

1. Inside the `my-source` directory, create a directory named `golang-app`. 

1. Create a file named `hello.go` with the following contents. Store the file in the `golang-app` directory. 

   ```
   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. Inside the `my-source` directory, create a directory named `nodejs-app`. It should be at the same level as the `golang-app` directory. 

1. Create a file named `index.js` with the following contents. Store the file in the `nodejs-app` directory. 

   ```
   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. Create a file named `package.json` with the following contents. Store the file in the `nodejs-app` directory. 

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

1. Create a file named `buildspec.yml` with the following contents. Store the file in the `my-source` directory, at the same level as the `nodejs-app` and `golang-app` directories. The `runtime-versions` section specifies the Node.js version 12 and Go version 1.13 runtimes. 

   ```
   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. Your file structure should now look like this. 

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

1. Upload the contents of the `my-source` directory to an S3 input bucket or a CodeCommit, GitHub, or Bitbucket repository.
**Important**  
 If you are using an S3 input bucket, be sure to create a ZIP file that contains the directory structure and files, and then upload it to the input bucket. Do not add `my-source` to the ZIP file, just the directories and files in `my-source`.

1. Open the AWS CodeBuild console at [https://console.aws.amazon.com/codesuite/codebuild/home](https://console.aws.amazon.com/codesuite/codebuild/home).

1. Create a build project. For more information, see [Create a build project (console)](create-project.md#create-project-console) and [Run a build (console)](run-build-console.md). Leave all settings at their default values, except for these settings.
   + For **Environment**:
     + For **Environment image**, choose **Managed image**.
     + For **Operating system**, choose **Amazon Linux 2**.
     + For **Runtime(s)**, choose **Standard**.
     + For **Image**, choose **aws/codebuild/amazonlinux-x86\$164-standard:4.0**.

1. Choose **Create build project**. 

1. Choose **Start build**. 

1. On **Build configuration**, accept the defaults, and then choose **Start build**. 

1. After the build is complete, view the build output on the **Build logs** tab. You should see output similar to the following. It shows output from the Go and Node.js runtimes. It also shows output from the Go and Node.js applications. 

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