首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >向CodePipeline添加一个阶段会引发错误

向CodePipeline添加一个阶段会引发错误
EN

Stack Overflow用户
提问于 2022-04-04 09:04:39
回答 1查看 773关注 0票数 1

我正在创建一个代码管道如下-

代码语言:javascript
运行
复制
import * as cdk from "aws-cdk-lib";
import { CodeBuildStep, CodePipeline, CodePipelineSource } from "aws-cdk-lib/pipelines";
...

export class Pipeline extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    ...

    const pipeline = new CodePipeline(this, "Pipeline", {
      pipelineName: "pipeline",
      synth: new CodeBuildStep("SynthStep", {
        input: CodePipelineSource.codeCommit(repo, "mainline"),
        buildEnvironment: {
          computeType: CodeBuild.ComputeType.MEDIUM,
          buildImage: CodeBuild.LinuxBuildImage.STANDARD_5_0,
        },
        partialBuildSpec: buildSpec,
        commands: [],
        role: codeBuildSynthRole,
        primaryOutputDirectory: "cdk/cdk.out",
      }),
      crossAccountKeys: true,
      selfMutation: true,
      dockerEnabledForSelfMutation: true,
    });

    const review = new ReviewPipelineStage(this, "Review", {
      sourceFileSet: pipeline.cloudAssemblyFileSet,
    });

    const reviewStage = pipeline.addStage(review);

    const gitleaksReviewAction = new GitleaksReviewAction(
      this,
      "GitleaksReviewAction",
      {
        sourceFileSet: pipeline.cloudAssemblyFileSet,
      }
    );

    reviewStage.addPost(gitleaksReviewAction.action);
    gitleaksReviewAction.gitleaksImage.repository.grantPull(
      gitleaksReviewAction.action.project
    );
  }
}

我正试图为Git泄漏添加一个阶段,GitleaksReviewAction构造如下-

代码语言:javascript
运行
复制
export interface GitleaksReviewActionProps {
  sourceFileSet: FileSet;
}

export class GitleaksReviewAction extends Construct {
  public readonly action: CodeBuildStep;
  public readonly gitleaksImage: DockerImageAsset;

  constructor(scope: Construct, id: string, props: GitleaksReviewActionProps) {
    super(scope, id);
    this.gitleaksImage = new DockerImageAsset(this, "gitleaksDockerAsset", {
      directory: path.join(__dirname, "../assets/gitleaks"),
    });
    this.action = new CodeBuildStep("Gitleaks", {
      input: props.sourceFileSet,
      commands: [
        "find . -type d -exec chmod 777 {} \\;",
        "find . -type f -exec chmod 666 {} \\;",
        `aws ecr get-login-password --region $AWS_REGION | docker login -u AWS --password-stdin ${this.gitleaksImage.imageUri}`,
        `docker run -v $(pwd):/repo ${this.gitleaksImage.imageUri} --path=/repo --repo-config-path=config/gitleaks/gitleaks.toml --verbose`,
      ],
      buildEnvironment: {
        buildImage: codebuild.LinuxBuildImage.STANDARD_5_0,
        privileged: true,
      },
    });
  }
}

ReviewPipelineStage的内容如下-

代码语言:javascript
运行
复制
export interface ReviewPipelineStageProps extends StageProps {
  sourceFileSet: FileSet;
}

export class ReviewPipelineStage extends Stage {
  constructor(scope: Construct, id: string, props: ReviewPipelineStageProps) {
    super(scope, id, props);

    new GitleaksReviewAction(this, "GitleaksReviewAction", {
      sourceFileSet: props.sourceFileSet,
    });
  }
}

当我做一个cdk合成器时,我得到了一个错误-

代码语言:javascript
运行
复制
throw new Error(`${construct.constructor?.name ?? 'Construct'} at '${Node.of(construct).path}' should be created in the scope of a Stack, but no Stack found`);

我不确定是否应该使用其他构造aws_codepipeline来定义阶段,或者这是创建阶段的正确方法。有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-04 12:11:51

问题是您要在一个阶段的范围内创建一个构造。你不能这样做,你只能在舞台的范围内创建堆栈。构造必须在堆栈的作用域中创建。

问题是:

代码语言:javascript
运行
复制
export class ReviewPipelineStage extends Stage {
  constructor(scope: Construct, id: string, props: ReviewPipelineStageProps) {
    super(scope, id, props);

    new GitleaksReviewAction(this, "GitleaksReviewAction", {
      sourceFileSet: props.sourceFileSet,
    });
  }
}

this是一个舞台,而不是一个堆栈。

要解决这个问题,您必须在您的阶段中创建一个堆栈,并在其中创建您的构造。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71734583

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档