如何创建一个代码管道阶段以在GitHub中使用源代码部署React应用程序? [CDK 2.0]
我在GitHub有一个React存储库,我想构建一个管道,以便每当我将新提交推向存储库时,管道都会构建并部署到生产中。我正在使用CDK 2.0。
我的管道代码:
import * as cdk from "aws-cdk-lib";
import { Stack, StackProps } from "aws-cdk-lib";
import {
CodePipeline,
CodePipelineSource,
ShellStep,
Step,
} from "aws-cdk-lib/pipelines";
import { ManualApprovalStep } from "aws-cdk-lib/pipelines";
import { PipelineStage } from "./pipeline-stage";
export interface CodePipelineStackProps extends cdk.StackProps {
// Built in Stack props
readonly env: cdk.Environment;
readonly description: string;
}
export class CodePipelineStack extends Stack {
constructor(scope: cdk.App, id: string, props: CodePipelineStackProps) {
super(scope, id, props);
const pipeline = new CodePipeline(this, "Pipeline", {
pipelineName: "MyPipeline",
synth: new ShellStep("Synth", {
input: CodePipelineSource.gitHub(
"github-acount-name/my-react-code",
"main"
),
commands: [
'npm ci',
'npm run build',
'npx cdk synth'
],
}),
});
const gammaStage = pipeline.addStage(
new PipelineStage(this, "Gamma", {
env: props.env,
})
);
gammaStage.addPre(
new ShellStep("Run Unit Tests", { commands: ["yarn install", "npm test"] })
);
gammaStage.addPost(
new ManualApprovalStep("Manual approval before production")
);
const prodStage = pipeline.addStage(
new PipelineStage(this, "Prod", {
env: props.env,
})
);
}
}
在这里我应该做什么将我的反应应用程序添加到管道阶段代码?我可以从此示例,它添加了来自本地的 lambdastack
资产。但是,我想从我的github存储库中构建工件。
export class PipelineStage extends cdk.Stage {
constructor(scope: Construct, id: string, props?: cdk.StageProps) {
super(scope, id, props);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有很多选择。首先,CDK具有两种管道风味, codepipeline.pipeline 和 pipelines.codepipeline 。文档具有选择,但简而言之:
pipelines.codepipeline
是有用的,并且针对部署CDK Appscodepipeline.pipeline.pipeline
对通用构建任务更加灵活,aws :: codepipeline :: codepipeline :: pipeline :: pipeline
和aws :: codebuild :: project
资源您应该选择哪个?这是一个偏爱问题,但是对于通用构建任务,我发现
codepipeline.pipeline
api更容易推理。创建一个带有S3目标存储桶和管道结构的堆栈。管道具有单或 - 阶段每个阶段都有一个或and Actions (aws-cdk-lib/aws-codepipeline-actions-actions
)。这是一种方法:actions.codecommitsourceaction
Actions.codebuildaction.codebuildaction
构建代码。您提供 buildspec 并构建命令,设置env vars等。Aactions.S3DeployAction
to output the built code to your destination bucketYou have many options. To start with, the CDK has two pipeline flavours, codepipeline.Pipeline and pipelines.CodePipeline. The docs have guidance on which to choose, but in a nutshell:
pipelines.CodePipeline
is opinionated and optimised for deploying CDK appscodepipeline.Pipeline
is more flexible for generic build tasksAWS::CodePipeline::Pipeline
andAWS::CodeBuild::Project
resourcesWhich should you choose? It's a matter of preference, but for generic build tasks I find the
codepipeline.Pipeline
API easier to reason about. Create a Stack with an S3 destination bucket and a Pipeline construct. A pipeline has one-or-many stages and each stage has one-or-many actions (aws-cdk-lib/aws-codepipeline-actions
). Here is one way to approach it:actions.CodeCommitSourceAction
actions.CodeBuildAction
to build the code. You provide it a buildspec to give install and build commands, set env vars, etc.actions.S3DeployAction
to output the built code to your destination bucket