我正在创建一个代码管道如下-
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构造如下-
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来定义阶段,或者这是创建阶段的正确方法。有什么想法吗?
发布于 2022-04-04 12:11:51
问题是您要在一个阶段的范围内创建一个构造。你不能这样做,你只能在舞台的范围内创建堆栈。构造必须在堆栈的作用域中创建。
问题是:
export class ReviewPipelineStage extends Stage {
constructor(scope: Construct, id: string, props: ReviewPipelineStageProps) {
super(scope, id, props);
new GitleaksReviewAction(this, "GitleaksReviewAction", {
sourceFileSet: props.sourceFileSet,
});
}
}
this
是一个舞台,而不是一个堆栈。
要解决这个问题,您必须在您的阶段中创建一个堆栈,并在其中创建您的构造。
https://stackoverflow.com/questions/71734583
复制相似问题