将阶段添加到Codepipeline会引发错误

发布于 2025-01-18 21:41:41 字数 3446 浏览 2 评论 0原文

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

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

我正在尝试为 Gitleaks 添加一个阶段,GitleaksReviewAction 构造如下 -

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 如下 -

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 合成器时,我收到错误 -

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 来定义阶段,或者这是创建阶段的正确方法。有什么想法吗?

I am creating a code pipeline as follows -

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

I am trying to add a stage for Gitleaks and the GitleaksReviewAction construct is as follows -

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

The ReviewPipelineStage is as follows -

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

As i do a cdk synth i get an error -

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`);

I am not sure if i should be using the other construct aws_codepipeline to define the stages or is this the right way to create a stage. Any ideas?

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

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

发布评论

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

评论(1

七月上 2025-01-25 21:41:41

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

问题在这里:

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

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

是一个阶段,而不是堆栈。

为了解决这个问题,您必须在舞台上创建一个堆栈,并在其中创建构造。

The issue is that you're creating a Construct in the scope of a Stage. You can't do that, you can only create Stacks in the scope of a Stage. Constructs have to be created in the scope of a Stack.

The issue is here:

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

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

this is a Stage, not a Stack.

To fix, this, you have to create a Stack in your stage, and create your Constructs in there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文