本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
構建規格文件示例中的運行時版本 CodeBuild
如果您使用 Amazon Linux 2(AL2)標準映像版本 1.0 或更新版本,或 Ubuntu 標準映像版本 2.0 或更新版本,則可以在構建規格文件的runtime-versions
部分中指定一個或多個運行時。這個範例說明如何變更您的專案執行時間、指定一個以上的執行時間,以及指定一個取決於另一個執行時間的執行時間。如需支援的執行時間資訊,請參閱碼頭圖片提供 CodeBuild。
注意
如果您在建置容器中使用 Docker,您的建置必須以特殊權限模式執行。如需詳細資訊,請參閱 在 AWS CodeBuild 中執行建置 及 在 AWS CodeBuild 中建立建置專案。
更新您的執行時間版本
您可以通過更新 buildspec 文件的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:
-
指
runtime-versions
定 3.7 版本 Python 部分:phases: install: runtime-versions: python: 3.7
-
指
runtime-versions
定 3.8 版本 Python 部分:phases: install: runtime-versions: python: 3.8
此範例示範的專案最初使用 Java 8 執行時間,然後更新為 Java 版本 10 執行時間。
-
下載並安裝 Maven。如需詳細資訊,請參閱 Apache Maven 網站上的下載 Apache Maven
和安裝 Apache Maven 。 -
切換至您本機電腦或執行個體上的空白目錄,然後執行此 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
-
使用下列內容建立名為
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 儲存庫。重要
請勿上傳
或(root directory name)
,僅上傳(root directory name)
/my-web-app
中的目錄和檔案即可。(root directory name)
/my-web-app如果您使用的是 S3 輸入儲存貯體,請務必建立包含目錄結構和檔案的 ZIP 檔案,然後將其上傳至輸入儲存貯體。請勿將
或(root directory name)
新增至 ZIP 檔案,僅新增(root directory name)
/my-web-app
中的目錄和檔案即可。(root directory name)
/my-web-app -
請在以下位置開啟 AWS CodeBuild 主控台。
https://console.aws.amazon.com/codesuite/codebuild/home -
建立建置專案。如需詳細資訊,請參閱 建立組建專案 (主控台) 及 執行建置 (主控台)。除了下列設定外,保留所有設定的預設值。
-
針對 Environment (環境):
-
針對 Environment image (環境映像),選擇 Managed image (受管映像)。
-
針對 Operating system (作業系統),請選擇 Amazon Linux 2。
-
針對 Runtime(s) (執行時間),選擇 Standard (標準)。
-
對於圖像,請選擇 AWS /代碼構建/ 亞馬遜鏈 2-x86_64 標準:4.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/*;
-
將
runtime-versions
區段更新為 Java 版本 11: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/*;
指定兩個執行時間
您可以在同一個 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 版本 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
-
您的檔案結構現在看起來應如下。
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
中的目錄和檔案即可。 -
請在以下位置開啟 AWS CodeBuild 主控台。
https://console.aws.amazon.com/codesuite/codebuild/home -
建立建置專案。如需詳細資訊,請參閱 建立組建專案 (主控台) 及 執行建置 (主控台)。除了下列設定外,保留所有設定的預設值。
-
針對 Environment (環境):
-
針對 Environment image (環境映像),選擇 Managed image (受管映像)。
-
針對 Operating system (作業系統),請選擇 Amazon Linux 2。
-
針對 Runtime(s) (執行時間),選擇 Standard (標準)。
-
對於圖像,請選擇 AWS /代碼構建/ 亞馬遜鏈 2-x86_64 標準:4.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 '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