如何创建一个代码管道阶段以在GitHub中使用源代码部署React应用程序? [CDK 2.0]

发布于 2025-01-20 09:03:58 字数 2134 浏览 4 评论 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);
        
    }
}

I have a React repository in GitHub, and I want to build a pipeline so that whenever I push new commit into the repository, the pipeline will build and deploy to production. I'm using CDK 2.0.

My pipeline code:

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,
      })
    );
    
  }
}

What should I do here to add my react applications to the pipeline stage code? I can see from this example that it added a LambdaStack from local asset. However, I want to build the artifact from my GitHub repository.

export class PipelineStage extends cdk.Stage {
    constructor(scope: Construct, id: string, props?: cdk.StageProps) {
        super(scope, id, props);
        
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耳钉梦 2025-01-27 09:03:58

您有很多选择。首先,CDK具有两种管道风味, codepipeline.pipeline pipelines.codepipeline 。文档具有选择,但简而言之:

  1. pipelines.codepipeline 是有用的,并且针对部署CDK Apps
  2. codepipeline.pipeline.pipeline 对通用构建任务更加灵活,
  3. 这两个很大程度上 是更灵活的。重叠 - 您可以解决许多问题使用一个
  4. 在引擎盖下,两者都exploy aws :: codepipeline :: codepipeline :: pipeline :: pipeline aws :: codebuild :: project 资源

您应该选择哪个?这是一个偏爱问题,但是对于通用构建任务,我发现 codepipeline.pipeline api更容易推理。创建一个带有S3目标存储桶和管道结构的堆栈。管道具有单或 - 阶段每个阶段都有一个或and Actions aws-cdk-lib/aws-codepipeline-actions-actions )。这是一种方法:

  1. 使用源操作从远程仓库中提取代码,例如 actions.codecommitsourceaction
  2. a 构建 带有 Actions.codebuildaction.codebuildaction 构建代码。您提供 buildspec 并构建命令,设置env vars等。A
  3. deploy stage with an actions.S3DeployAction to output the built code to your destination bucket

You 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:

  1. pipelines.CodePipeline is opinionated and optimised for deploying CDK apps
  2. codepipeline.Pipeline is more flexible for generic build tasks
  3. The two largely overlap - you can solve many problems using either one
  4. Under the hood, both deploy AWS::CodePipeline::Pipeline and AWS::CodeBuild::Project resources

Which 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:

  1. A source stage with a source action to pull your code from a remote repo, like actions.CodeCommitSourceAction
  2. A build stage with an actions.CodeBuildAction to build the code. You provide it a buildspec to give install and build commands, set env vars, etc.
  3. A deploy stage with an actions.S3DeployAction to output the built code to your destination bucket
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文