我如何从CDK上的CodePipeline获得源工件?



我使用AWS官方文档使用CDK: https://docs.aws.amazon.com/cdk/latest/guide/cdk_pipeline.html#cdk_pipeline_define创建管道(与文档略有不同,我使用CodeStar连接,正如代码注释所建议的那样)

这会自动创建一个自突变的管道,有三个阶段——Source、Synth和UpdatePipeline。太好了。

我想添加一个带有CodeBuild动作的新阶段。我希望CodeBuild操作是基于构建规范。

在控制台上,我可以通过点击"Add new stage","Add action",并从下拉菜单中选择输入工件来轻松地做到这一点。

但是,在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结构上调用addStage()了。肯定有更好的方法来做我想做的事吧?

任何帮助/建议将不胜感激。谢谢。

codepipelineActions.CodeBuildAction方法接受一个名为outputs的参数,该参数用于此操作的输出工件列表。TypeScript源代码在这里。我认为在python版本的文档中更容易理解(链接在这里)。

相关内容

  • 没有找到相关文章

最新更新