我正在使用正式的AWS文档创建一个使用CDK:定义的管道(与docs略有不同,我在其中使用了CodeStar连接,正如代码注释所建议的那样)
这将自动创建一个具有三个阶段-- Source、Synth和UpdatePipeline --的自突变管道。太好了。
我想添加一个带有CodeBuild动作的新阶段。我希望CodeBuild操作基于源目录中的buildspec.yml文件。
在控制台上,通过单击“添加新阶段”、“添加操作”和从下拉菜单中选择输入工件,我可以轻松地做到这一点。
但是,在CDK上,通过这个推荐的设置,没有一种简单的方法可以访问输入工件。
我通过强制buildPipeline()并执行以下操作成功地做到了这一点:
import * as cdk from "@aws-cdk/core";
import {
CodePipeline,
ShellStep,
CodePipelineSource,
} from "@aws-cdk/pipelines";
import * as codebuild from "@aws-cdk/aws-codebuild";
import * as codepipelineActions from "@aws-cdk/aws-codepipeline-actions";
export class PipelineStack extends cdk.Stack {
public readonly source: cdk.CfnOutput
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const source = CodePipelineSource.connection("someuser/somerepo", "master", {
connectionArn: "arn:aws:codestar-connections:us-east-1:REDACTED:connection/REDACTED"
});
const synthShellStep = new ShellStep("Synth", {
input: source,
commands: [
"cd infrastructure",
"npm run ci",
"npm run build",
"npx cdk synth"
],
"primaryOutputDirectory": "infrastructure/cdk.out"
});
const pipeline = new CodePipeline(this, "Pipeline", {
pipelineName: "FancyPipeline",
synth: synthShellStep
});
// Need to build the pipeline to access the
// source artifact
pipeline.buildPipeline();
const sourceStage = pipeline.pipeline.stage("Source");
if (sourceStage) {
const sourceOutputs = sourceStage.actions[0].actionProperties.outputs;
if (sourceOutputs && sourceOutputs.length > 0) {
const sourceArtifact = sourceOutputs[0];
const codeBuildProject = new codebuild.PipelineProject(this, 'DockerBuildProject', {
environment: {
privileged: true
}
});
const buildAction = new codepipelineActions.CodeBuildAction({
actionName: 'DockerBuild',
project: codeBuildProject,
input: sourceArtifact,
environmentVariables: {
AWS_DEFAULT_REGION: {
value: this.region
},
AWS_ACCOUNT_ID: {
value: this.account
},
IMAGE_REPO_NAME: {
value: "somereponame"
},
IMAGE_TAG: {
value: "latest"
}
}
});
pipeline.pipeline.addStage({
stageName: "DockerBuildStage",
actions: [buildAction],
});
}
}
}
}但总体来说,这感觉很尴尬,我不能再在CodePipeline结构上调用CodePipeline了。当然有更好的方法来做我想做的事?
如有任何帮助/建议,将不胜感激。谢谢。
https://stackoverflow.com/questions/69371375
复制相似问题