向 CodePipeline 添加阶段会引发错误

Adding a stage to CodePipeline throws errors

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

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

问题是您正在舞台范围内创建构造。你不能那样做,你只能在 Stage 的范围内创建 Stacks。必须在堆栈范围内创建构造。

问题在这里:

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

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

this 是阶段,不是堆栈。

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