buildspec 文件示例中的运行时版本 CodeBuild - Amazon CodeBuild
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

buildspec 文件示例中的运行时版本 CodeBuild

如果您使用的是 Amazon Linux 2 (AL2) 标准映像版本 1.0 或更高版本或者 Ubuntu 标准映像版本 2.0 或更高版本,则可以在 buildspec 文件的 runtime-versions 部分中指定一个或多个运行时。本示例向您展示了如何更改您的项目运行时、指定多个运行时以及指定依赖于另一个运行时的运行时。有关支持的运行时的信息,请参阅 提供的 Docker 镜像 CodeBuild

注意

如果您在构建容器中使用 Docker,则您的构建必须在特权模式下运行。有关更多信息,请参阅 在 Amazon CodeBuild 中运行构建在 Amazon CodeBuild 中创建构建项目

更新您的运行时版本

您可以通过更新 buildspec 文件的 runtime-versions 部分,将项目使用的运行时修改到新版本。以下示例说明如何指定 Java 版本 8 和 11。

  • 一个 runtime-versions 部分,指定 Java 版本 8:

    phases: install: runtime-versions: java: corretto8
  • 一个 runtime-versions 部分,指定 Java 版本 11:

    phases: install: runtime-versions: java: corretto11

以下示例说明如何使用 Ubuntu 标准映像 5.0 或 Amazon Linux 2 标准映像 3.0 指定不同版本的 Python:

  • 一个 runtime-versions 部分,指定 Python 版本 3.7:

    phases: install: runtime-versions: python: 3.7
  • 一个 runtime-versions 部分,指定 Python 版本 3.8:

    phases: install: runtime-versions: python: 3.8

本示例演示一个项目,该项目从 Java 版本 8 运行时开始,然后更新到 Java 版本 10 运行时。

  1. 下载并安装 Maven。有关信息,请参阅 Apache Maven 网站上的下载 Apache Maven安装 Apache Maven

  2. 切换到您的本地计算机或实例上的空目录,然后运行此 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
  3. 使用以下内容创建名为 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 部分指定项目使用 Java 运行时版本 8。

    • - java -version 命令显示您的项目在执行构建时所使用的 Java 版本。

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

    (root directory name) └── my-web-app ├── src │ ├── main │ ├── resources │ └── webapp │ └── WEB-INF │ └── web.xml │ └── index.jsp ├── buildspec.yml └── pom.xml
  4. 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 中的目录和文件。

  5. 打开 Amazon CodeBuild 控制台,网址为 https://console.aws.amazon.com/codesuite/codebuild/home

  6. 创建构建项目。有关更多信息,请参阅 创建构建项目(控制台)运行构建(控制台)。除这些设置以外,将所有设置保留为默认值。

    • 对于环境

      • 对于环境映像,选择托管映像

      • 对于操作系统,选择 Amazon Linux 2

      • 对于运行时,选择标准

      • 对于映像,选择 aws/codebuild/amazonlinux2-x86_64-standard:4.0

  7. 选择开始构建

  8. 构建配置上,接受默认值,然后选择开始构建

  9. 当构建完成后,在构建日志选项卡上查看构建输出。您应该可以看到类似于如下所示的输出内容:

    [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/*;
  10. 使用 Java 版本 11 更新 runtime-versions 部分:

    install: runtime-versions: java: corretto11
  11. 保存更改后,再次运行您的构建并查看构建输出。您应看到已安装的 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/*;

指定两个运行时

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

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

  2. my-source 目录中,创建一个名为 golang-app 的目录。

  3. 使用以下内容创建名为 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") }
  4. my-source 目录中,创建一个名为 nodejs-app 的目录。它应该与 golang-app 目录同级。

  5. 使用以下内容创建名为 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");
  6. 使用以下内容创建名为 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" }
  7. 使用以下内容创建名为 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
  8. 您的文件结构现在应如下所示。

    my-source ├── golang-app │ └── hello.go ├── nodejs.app │ ├── index.js │ └── package.json └── buildspec.yml
  9. my-source目录的内容上传到 S3 输入存储桶或 CodeCommit GitHub、或 Bitbucket 存储库。

    重要

    如果您使用的是 S3 输入存储桶,请确保创建一个包含目录结构和文件的 ZIP 文件,然后将其上传至输入存储桶。请不要将 my-source 添加到 ZIP 文件中,而只添加 my-source 中的目录和文件。

  10. 打开 Amazon CodeBuild 控制台,网址为 https://console.aws.amazon.com/codesuite/codebuild/home

  11. 创建构建项目。有关更多信息,请参阅 创建构建项目(控制台)运行构建(控制台)。除这些设置以外,将所有设置保留为默认值。

    • 对于环境

      • 对于环境映像,选择托管映像

      • 对于操作系统,选择 Amazon Linux 2

      • 对于运行时,选择标准

      • 对于映像,选择 aws/codebuild/amazonlinux2-x86_64-standard:4.0

  12. 选择创建构建项目

  13. 选择开始构建

  14. 构建配置上,接受默认值,然后选择开始构建

  15. 当构建完成后,在构建日志选项卡上查看构建输出。您应该可以看到类似于如下所示的输出内容。它显示来自 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