使用 SDK forWord(v3) 的 CodeBuild JavaScript 範例 - AWS SDK 程式碼範例

文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的 GitHub 範例。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 SDK forWord(v3) 的 CodeBuild JavaScript 範例

下列程式碼範例示範如何使用 AWS SDK for JavaScript (v3) with CodeBuild 來執行動作和實作常見案例。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會示範如何呼叫個別服務函數,但您可以在相關案例中查看內容中的動作。

每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。

主題

動作

下列程式碼範例示範如何使用 CreateProject

SDK for JavaScript (v3)
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

建立專案。

import { ArtifactsType, CodeBuildClient, ComputeType, CreateProjectCommand, EnvironmentType, SourceType, } from "@aws-sdk/client-codebuild"; // Create the AWS CodeBuild project. export const createProject = async ( projectName = "MyCodeBuilder", roleArn = "arn:aws:iam::xxxxxxxxxxxx:role/CodeBuildAdmin", buildOutputBucket = "xxxx", githubUrl = "https://...", ) => { const codeBuildClient = new CodeBuildClient({}); const response = await codeBuildClient.send( new CreateProjectCommand({ artifacts: { // The destination of the build artifacts. type: ArtifactsType.S3, location: buildOutputBucket, }, // Information about the build environment. The combination of "computeType" and "type" determines the // requirements for the environment such as CPU, memory, and disk space. environment: { // Build environment compute types. // https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-compute-types.html computeType: ComputeType.BUILD_GENERAL1_SMALL, // Docker image identifier. // See https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-available.html image: "aws/codebuild/standard:7.0", // Build environment type. type: EnvironmentType.LINUX_CONTAINER, }, name: projectName, // A role ARN with permission to create a CodeBuild project, write to the artifact location, and write CloudWatch logs. serviceRole: roleArn, source: { // The type of repository that contains the source code to be built. type: SourceType.GITHUB, // The location of the repository that contains the source code to be built. location: githubUrl, }, }), ); console.log(response); // { // '$metadata': { // httpStatusCode: 200, // requestId: 'b428b244-777b-49a6-a48d-5dffedced8e7', // extendedRequestId: undefined, // cfId: undefined, // attempts: 1, // totalRetryDelay: 0 // }, // project: { // arn: 'arn:aws:codebuild:us-east-1:xxxxxxxxxxxx:project/MyCodeBuilder', // artifacts: { // encryptionDisabled: false, // location: 'xxxxxx-xxxxxxx-xxxxxx', // name: 'MyCodeBuilder', // namespaceType: 'NONE', // packaging: 'NONE', // type: 'S3' // }, // badge: { badgeEnabled: false }, // cache: { type: 'NO_CACHE' }, // created: 2023-08-18T14:46:48.979Z, // encryptionKey: 'arn:aws:kms:us-east-1:xxxxxxxxxxxx:alias/aws/s3', // environment: { // computeType: 'BUILD_GENERAL1_SMALL', // environmentVariables: [], // image: 'aws/codebuild/standard:7.0', // imagePullCredentialsType: 'CODEBUILD', // privilegedMode: false, // type: 'LINUX_CONTAINER' // }, // lastModified: 2023-08-18T14:46:48.979Z, // name: 'MyCodeBuilder', // projectVisibility: 'PRIVATE', // queuedTimeoutInMinutes: 480, // serviceRole: 'arn:aws:iam::xxxxxxxxxxxx:role/CodeBuildAdmin', // source: { // insecureSsl: false, // location: 'https://...', // reportBuildStatus: false, // type: 'GITHUB' // }, // timeoutInMinutes: 60 // } // } return response; };