指定两个运行时 - AWS CodeBuild

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

指定两个运行时

您可以在同一个 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. https://console.aws.amazon.com/codesuite/codebuild /home 中打开 AWS CodeBuild 控制台。

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

    • 对于环境

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

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

      • 对于运行时,选择标准

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

  12. 选择 Create build project(创建构建项目)

  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