本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
AWS CodePipeline CodeBuild 的範例
本節說明 CodePipeline 和 CodeBuild 之間的範例整合。
樣本 | 描述 |
---|---|
這些範例示範如何使用 AWS CodePipeline 來建立使用批次建置的建置專案。 |
|
此範例示範如何使用 AWS CodePipeline 建立使用多個輸入來源建立多個輸出成品的建置專案。 |
CodePipeline/CodeBuild 整合和批次建置的範例
AWS CodeBuild 支援批次建置。下列範例示範如何使用 AWS CodePipeline 建立使用批次建置的建置專案。
您可以使用定義管道結構的 JSON 格式檔案,然後將其與 搭配使用 AWS CLI 來建立管道。如需詳細資訊,請參閱AWS CodePipeline 《 使用者指南》中的AWS CodePipeline 管道結構參考。
具有個別成品的批次建置
使用下列 JSON 檔案做為管道結構的範例,該結構會建立具有個別成品的批次建置。若要在 CodePipeline 中啟用批次建置,請將configuration
物件的 BatchEnabled
參數設定為 true
。
{ "pipeline": { "roleArn": "arn:aws:iam::account-id:role/my-AWS-CodePipeline-service-role-name", "stages": [ { "name": "Source", "actions": [ { "inputArtifacts": [], "name": "Source1", "actionTypeId": { "category": "Source", "owner": "AWS", "version": "1", "provider": "S3" }, "outputArtifacts": [ { "name": "source1" } ], "configuration": { "S3Bucket": "
<my-input-bucket-name>
", "S3ObjectKey": "my-source-code-file-name.zip" }, "runOrder": 1 }, { "inputArtifacts": [], "name": "Source2", "actionTypeId": { "category": "Source", "owner": "AWS", "version": "1", "provider": "S3" }, "outputArtifacts": [ { "name": "source2" } ], "configuration": { "S3Bucket": "<my-other-input-bucket-name>
", "S3ObjectKey": "my-other-source-code-file-name.zip" }, "runOrder": 1 } ] }, { "name": "Build", "actions": [ { "inputArtifacts": [ { "name": "source1" }, { "name": "source2" } ], "name": "Build", "actionTypeId": { "category": "Build", "owner": "AWS", "version": "1", "provider": "CodeBuild" }, "outputArtifacts": [ { "name": "build1" }, { "name": "build1_artifact1" }, { "name": "build1_artifact2" }, { "name": "build2_artifact1" }, { "name": "build2_artifact2" } ], "configuration": { "ProjectName": "my-build-project-name", "PrimarySource": "source1", "BatchEnabled": "true" }, "runOrder": 1 } ] } ], "artifactStore": { "type": "S3", "location": "<AWS-CodePipeline-internal-bucket-name>
" }, "name": "my-pipeline-name", "version": 1 } }
以下是將使用此管道組態的 CodeBuild buildspec 檔案範例。
version: 0.2 batch: build-list: - identifier: build1 env: compute-type: BUILD_GENERAL1_SMALL - identifier: build2 env: compute-type: BUILD_GENERAL1_MEDIUM phases: build: commands: - echo 'file' > output_file artifacts: files: - output_file secondary-artifacts: artifact1: files: - output_file artifact2: files: - output_file
管道 JSON 檔案中指定的輸出成品名稱必須符合 buildspec 檔案中定義的組建和成品的識別符。語法是主要成品的 buildIdentifier
,次要成品的 buildIdentifier
_artifactIdentifier
。
例如,對於輸出成品名稱 build1
,CodeBuild 會將 的主要成品上傳build1
到 的位置build1
。對於輸出名稱 build1_artifact1
,CodeBuild 會將 artifact1
的次要成品上傳build1
到 的位置build1_artifact1
,以此類推。如果只指定一個輸出位置,則名稱應該是 buildIdentifier
。
建立 JSON 檔案之後,您可以建立管道。使用 AWS CLI 執行 create-pipeline 命令,並將 檔案傳遞給 --cli-input-json
參數。如需詳細資訊,請參閱AWS CodePipeline 《 使用者指南》中的建立管道 (CLI)。
合併成品的批次建置
使用下列 JSON 檔案做為管道結構的範例,該結構會建立具有合併成品的批次建置。若要在 CodePipeline 中啟用批次建置,請將configuration
物件的 BatchEnabled
參數設定為 true
。若要將建置成品合併到相同的位置,請將configuration
物件的 CombineArtifacts
參數設定為 true
。
{ "pipeline": { "roleArn": "arn:aws:iam::account-id:role/my-AWS-CodePipeline-service-role-name", "stages": [ { "name": "Source", "actions": [ { "inputArtifacts": [], "name": "Source1", "actionTypeId": { "category": "Source", "owner": "AWS", "version": "1", "provider": "S3" }, "outputArtifacts": [ { "name": "source1" } ], "configuration": { "S3Bucket": "
<my-input-bucket-name>
", "S3ObjectKey": "my-source-code-file-name.zip" }, "runOrder": 1 }, { "inputArtifacts": [], "name": "Source2", "actionTypeId": { "category": "Source", "owner": "AWS", "version": "1", "provider": "S3" }, "outputArtifacts": [ { "name": "source2" } ], "configuration": { "S3Bucket": "<my-other-input-bucket-name>
", "S3ObjectKey": "my-other-source-code-file-name.zip" }, "runOrder": 1 } ] }, { "name": "Build", "actions": [ { "inputArtifacts": [ { "name": "source1" }, { "name": "source2" } ], "name": "Build", "actionTypeId": { "category": "Build", "owner": "AWS", "version": "1", "provider": "CodeBuild" }, "outputArtifacts": [ { "name": "output1 " } ], "configuration": { "ProjectName": "my-build-project-name", "PrimarySource": "source1", "BatchEnabled": "true", "CombineArtifacts": "true" }, "runOrder": 1 } ] } ], "artifactStore": { "type": "S3", "location": "<AWS-CodePipeline-internal-bucket-name>
" }, "name": "my-pipeline-name", "version": 1 } }
以下是將使用此管道組態的 CodeBuild buildspec 檔案範例。
version: 0.2 batch: build-list: - identifier: build1 env: compute-type: BUILD_GENERAL1_SMALL - identifier: build2 env: compute-type: BUILD_GENERAL1_MEDIUM phases: build: commands: - echo 'file' > output_file artifacts: files: - output_file
如果批次建置已啟用合併成品,則只允許一個輸出。CodeBuild 會將所有組建的主要成品合併為單一 ZIP 檔案。
建立 JSON 檔案之後,您可以建立管道。使用 AWS CLI 執行 create-pipeline 命令,並將 檔案傳遞給 --cli-input-json
參數。如需詳細資訊,請參閱AWS CodePipeline 《 使用者指南》中的建立管道 (CLI)。
CodePipeline/CodeBuild 與多個輸入來源和輸出成品整合的範例
AWS CodeBuild 專案可以採用多個輸入來源。也可以建立不只一個輸出成品。此範例示範如何使用 AWS CodePipeline 建立使用多個輸入來源建立多個輸出成品的建置專案。如需詳細資訊,請參閱多個輸入來源和輸出成品範例。
您可以使用定義管道結構的 JSON 格式檔案,然後將其與 搭配使用 AWS CLI 來建立管道。使用下列 JSON 檔案作為管道結構的範例,以建立具有多個輸入來源和多個輸出成品的組建。稍後在此範例中,您會看到此檔案如何指定多個輸入和輸出。如需詳細資訊,請參閱AWS CodePipeline 《 使用者指南》中的 CodePipeline 管道結構參考。
{ "pipeline": { "roleArn": "arn:aws:iam::account-id:role/my-AWS-CodePipeline-service-role-name", "stages": [ { "name": "Source", "actions": [ { "inputArtifacts": [], "name": "
Source1
", "actionTypeId": { "category": "Source", "owner": "AWS", "version": "1", "provider": "S3" }, "outputArtifacts": [ { "name": "source1
" } ], "configuration": { "S3Bucket": "my-input-bucket-name
", "S3ObjectKey": "my-source-code-file-name.zip
" }, "runOrder": 1 }, { "inputArtifacts": [], "name": "Source2
", "actionTypeId": { "category": "Source", "owner": "AWS", "version": "1", "provider": "S3" }, "outputArtifacts": [ { "name": "source2
" } ], "configuration": { "S3Bucket": "my-other-input-bucket-name
", "S3ObjectKey": "my-other-source-code-file-name.zip
" }, "runOrder": 1 } ] }, { "name": "Build", "actions": [ { "inputArtifacts": [ { "name": "source1
" }, { "name": "source2
" } ], "name": "Build", "actionTypeId": { "category": "Build", "owner": "AWS", "version": "1", "provider": "AWS CodeBuild" }, "outputArtifacts": [ { "name": "artifact1
" }, { "name": "artifact2
" } ], "configuration": { "ProjectName": "my-build-project-name
", "PrimarySource": "source1
" }, "runOrder": 1 } ] } ], "artifactStore": { "type": "S3", "location": "AWS-CodePipeline-internal-bucket-name
" }, "name": "my-pipeline-name
", "version": 1 } }
在此 JSON 檔案中:
-
您的其中一個輸入來源必須指定為
PrimarySource
。此來源是 CodeBuild 尋找並執行 buildspec 檔案的目錄。在 JSON 檔案的 CodeBuild 階段的configuration
區段中,關鍵字PrimarySource
用來指定主要來源。 -
每個輸入來源安裝在其自己的目錄中。此目錄會存放在內建的環境變數,
$CODEBUILD_SRC_DIR
表示主要來源和$CODEBUILD_SRC_DIR_yourInputArtifactName
表示所有其他來源。就此範例中的管道而言,兩個輸入來源目錄是$CODEBUILD_SRC_DIR
和$CODEBUILD_SRC_DIR_source2
。如需詳細資訊,請參閱建置環境中的環境變數。 -
在管道的 JSON 檔案中指定的輸出成品名稱,必須符合 buildspec 檔案中定義的次要成品名稱。此管道使用下列 buildspec 檔案。如需詳細資訊,請參閱Buildspec 語法。
version: 0.2 phases: build: commands: - touch source1_file - cd $CODEBUILD_SRC_DIR_source2 - touch source2_file artifacts: files: - '**/*' secondary-artifacts: artifact1: base-directory: $CODEBUILD_SRC_DIR files: - source1_file artifact2: base-directory: $CODEBUILD_SRC_DIR_source2 files: - source2_file
建立 JSON 檔案之後,您可以建立管道。使用 AWS CLI 執行 create-pipeline 命令,並將 檔案傳遞給 --cli-input-json
參數。如需詳細資訊,請參閱AWS CodePipeline 《 使用者指南》中的建立管道 (CLI)。