CodeBuild 的构建规范文件示例中的运行时版本
如果您使用的是 Amazon Linux 2 (AL2) 标准映像版本 1.0 或更高版本或者 Ubuntu 标准映像版本 2.0 或更高版本,则可以在构建规范文件的
runtime-versions
部分中指定一个或多个运行时。本示例向您展示了如何更改您的项目运行时、指定多个运行时以及指定依赖于另一个运行时的运行时。有关支持的运行时的信息,请参阅 CodeBuild 提供的 Docker 映像。
如果您在构建容器中使用 Docker,则您的构建必须在特权模式下运行。有关更多信息,请参阅 在 AWS CodeBuild 中运行构建 和 在 AWS CodeBuild 中创建构建项目。
更新您的运行时版本
您可以通过更新构建规范文件的 runtime-versions
部分,将项目使用的运行时修改为新版本。以下示例说明如何指定 Java 版本 8 和 11。
-
runtime-versions
部分:如果您使用的是 Amazon Linux 2 标准映像,则指定版本 8 的 Java:phases: install: runtime-versions: java: corretto8
-
runtime-versions
部分:如果您使用的是 Amazon Linux 2 标准映像,则指定版本 11 的 Java:phases: install: runtime-versions: java: corretto11
-
runtime-versions
部分:如果您使用的是 Ubuntu 标准映像 2.0,则指定版本 8 的 Java:phases: install: runtime-versions: java: openjdk8
-
runtime-versions
部分:如果您使用的是 Ubuntu 标准映像 2.0,则指定版本 11 的 Java:phases: install: runtime-versions: java: openjdk11
以下示例说明如何使用 Ubuntu 标准映像 2.0 或 Amazon Linux 2 标准映像 2.0 指定不同版本的 Node.js:
-
一个
runtime-versions
部分,指定 Node.js 版本 8:phases: install: runtime-versions: nodejs: 8
-
一个
runtime-versions
部分,指定 Node.js 版本 10:phases: install: runtime-versions: nodejs: 10
本示例演示一个项目,该项目从 Java 版本 8 运行时开始,然后更新到 Java 版本 10 运行时。
-
按照 创建源代码 中的步骤 1 和 2 生成源代码。如果成功,系统会创建一个名为
my-web-app
的目录,其中包含您的源文件。 -
使用以下内容创建名为
buildspec.yml
的文件。将此文件存储到(root directory name)
/my-web-appversion: 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 -
-
将
my-web-app
目录的内容上传到 S3 输入存储桶或者上传到 CodeCommit、GitHub 或 Bitbucket 存储库。重要 Do not upload
or(root directory name)
, just the directories and files in(root directory name)
/my-web-app
.(root directory name)
/my-web-appIf 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
or(root directory name)
to the ZIP file, just the directories and files in(root directory name)
/my-web-app
.(root directory name)
/my-web-app -
Open the AWS CodeBuild console at https://console.amazonaws.cn/codesuite/codebuild/home
. -
创建构建项目。有关更多信息,请参阅 创建构建项目 (控制台) 和 运行构建(控制台)。除这些设置以外,将所有设置保留为默认值。
-
对于 Environment (环境):
-
对于 Environment image (环境映像),选择 Managed image (托管映像)。
-
对于 Operating system (操作系统),选择 Amazon Linux 2。
-
对于 Runtime(s) (运行时),选择 Standard (标准)。
-
对于 Image (映像),选择 aws/codebuild/amazonlinux2-x86_64-standard:3.0。
-
-
-
选择 Start build。
-
在 Build configuration (构建配置) 上,接受默认值,然后选择 Start build (开始构建)。
-
当构建完成后,在 Build logs (构建日志) 选项卡上查看构建输出。您应该可以看到类似于如下所示的输出内容:
[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/*;
-
使用 Java 版本 11 更新
runtime-versions
部分:install: runtime-versions: java: corretto11
-
保存更改后,再次运行您的构建并查看构建输出。您应看到已安装的 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/*;
指定运行时依赖关系
本示例说明如何指定运行时和具有依赖关系的运行时。例如,任何支持的 Android 运行时版本都依赖于 Java 运行时版本 8。例如,如果您指定 Android 版本 29 并使用 Amazon Linux 2 或 Ubuntu,则还可以指定 Java 版本 8。如果未指定依赖运行时,则 CodeBuild 会尝试为您选择它。
本示例中的构建项目使用 GitHub AWS 示例
-
Open the AWS CodeBuild console at https://console.amazonaws.cn/codesuite/codebuild/home
. -
创建构建项目。有关更多信息,请参阅 创建构建项目 (控制台) 和 运行构建(控制台)。除这些设置以外,将所有设置保留为默认值。
-
对于 Environment (环境):
-
对于 Source provider (源提供商),选择 GitHub。
-
对于 Repository (存储库),选择 Public repository (公有存储库)。
-
对于 Repository URL (存储库 URL),输入
https://github.com/aws-samples/aws-mobile-android-notes-tutorial
。 -
对于 Environment image (环境映像),选择 Managed image (托管映像)。
-
对于 Operating system (操作系统),选择 Amazon Linux 2。
-
对于 Runtime(s) (运行时),选择 Standard (标准)。
-
对于 Image (映像),选择 aws/codebuild/amazonlinux2-x86_64-standard:3.0。
-
-
-
对于 Build specifications (构建规范),选择 Insert build commands (插入构建命令),然后选择 Switch to editor (切换到编辑器)。
-
在 Build commands (构建命令) 中,将占位符文本替换为以下内容:
version: 0.2 phases: install: runtime-versions: android: 29 java: corretto8 build: commands: - ./gradlew assembleDebug artifacts: files: - app/build/outputs/apk/app-debug.apk
此
runtime-versions
部分同时指定 Android 版本 29 和 Java 版本 8 运行时。 -
选择 Create build project (创建构建项目)。
-
选择 Start build。
-
在 Build configuration (构建配置) 上,接受默认值,然后选择 Start build (开始构建)。
-
当构建完成后,在 Build logs (构建日志) 选项卡上查看构建输出。您应该可以看到类似于如下所示的输出内容。该内容中显示已安装 Android 版本 29 和 Java 版本 8:
[Container] 2019/05/14 23:21:42 Entering phase DOWNLOAD_SOURCES [Container] Date Time Running command echo "Installing Android version 29 ..." Installing Android version 29 ... [Container] Date Time Running command echo "Installing Java version 8 ..." Installing Java version 8 ...
指定两个运行时
您可以在同一个 CodeBuild 构建项目中指定多个运行时。此示例项目使用两个源文件:一个使用 Go 运行时,另一个使用 Node.js 运行时。
-
创建名为
my-source
的目录。 -
在
my-source
目录中,创建一个名为golang-app
的目录。 -
使用以下内容创建名为
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") }
-
在
my-source
目录中,创建一个名为nodejs-app
的目录。 它应与golang-app
目录位于同一级别。 -
使用以下内容创建名为
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");
-
使用以下内容创建名为
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" }
-
使用以下内容创建名为
buildspec.yml
的文件。将文件存储在my-source
目录中,该目录与nodejs-app
以及golang-app
目录同级。runtime-versions
部分指定 Node.js 版本 10 运行时和 Go 版本 1.13 运行时。version: 0.2 phases: install: runtime-versions: golang: 1.13 nodejs: 10 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
-
您的文件结构现在应如下所示。
my-source ├── golang-app │ └── hello.go ├── nodejs.app │ ├── index.js │ └── package.json └── buildspec.yml
-
将
my-source
目录的内容上传到 S3 输入存储桶或者上传到 CodeCommit、GitHub 或 Bitbucket 存储库。重要 如果您使用的是 S3 输入存储桶,请确保创建一个包含目录结构和文件的 ZIP 文件,然后将其上传至输入存储桶。请不要将
my-source
添加到 ZIP 文件中,而只添加my-source
中的目录和文件。 -
Open the AWS CodeBuild console at https://console.amazonaws.cn/codesuite/codebuild/home
. -
创建构建项目。有关更多信息,请参阅 创建构建项目 (控制台) 和 运行构建(控制台)。除这些设置以外,将所有设置保留为默认值。
-
对于 Environment (环境):
-
For Environment image, choose Managed image.
-
For Operating system, choose Amazon Linux 2.
-
For Runtime(s), choose Standard.
-
For Image, choose aws/codebuild/amazonlinux2-x86_64-standard:2.0.
-
-
-
选择 Create build project (创建构建项目)。
-
选择 Start build。
-
在 Build configuration (构建配置) 上,接受默认值,然后选择 Start build (开始构建)。
-
当构建完成后,在 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 '10' based on manual selections... [Container] Date Time Running command echo "Installing Go version 1.13 ..." Installing Go version 1.12 ... [Container] Date Time Running command echo "Installing Node.js version 10 ..." Installing Node.js version 10 ... [Container] Date Time Running command n 10.15.3 [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