[英]How to Specify directory to trigger code pipeline cdk?
I am using a cdk code pipeline and i would like to specify for it to only trigger if a directory within the repo has changed.我正在使用 cdk 代码管道,我想指定它仅在 repo 中的目录发生更改时触发。
const pipeline = new CodePipeline(this, 'Pipeline', {
pipelineName: 'BackEndPipeline',
synth: new CodeBuildStep('SynthStep', {
input: CodePipelineSource.codeCommit(repo, 'master'),
installCommands: [
'npm install -g aws-cdk'
],
commands: [
'cd mydir',
'ls',
'npm install',
'ls',
'npm run build',
'ls',
'npx cdk synth'
],
primaryOutputDirectory: 'mydir/cdk.out'
})
});
It's a DIY job.这是一项 DIY 工作。 A CodePipeline is designed to run from start to finish, unconditionally.
CodePipeline 旨在无条件地从头到尾运行。 The trick is to trigger the pipeline only when there has been a relevant change.
诀窍是仅在发生相关更改时才触发管道。 You must replace the default trigger-on-every-change setup with manual triggering logic.
您必须用手动触发逻辑替换默认的每次更改时触发设置。 For a
CodeCommit
repo:对于
CodeCommit
库:
(1) Turn off the pipeline's automatic trigger. (1) 关闭流水线的自动触发。 Set the
trigger: codepipeline_actions.CodeCommitTrigger.NONE
prop in the CodePipelineSource .在CodePipelineSource中设置
trigger: codepipeline_actions.CodeCommitTrigger.NONE
道具。
(2) Create an EventBridge Rule to listen to your repo's commit events: (2) 创建一个EventBridge 规则来监听你的 repo 的提交事件:
const rule = new events.Rule(this, 'MyRule', {
eventPattern: {
source: ['aws.codecommit'],
resources: ['arn:aws:codecommit:us-east-1:123456789012:my-repo'],
detailType: ['CodeCommit Repository State Change'],
},
});
(3) Add a Lambda as the rule's target . (3) 添加一个 Lambda 作为规则的目标。 The Lambda will receive the referenceUpdated payload on a push to your repo.
Lambda 将在推送到您的存储库时收到referenceUpdated 有效负载。 The event payload contains the
commitId
, but not the changed files.事件负载包含
commitId
,但不包含更改的文件。 To get the file-level changes, your Lambda should call the GetDifferences API. Your Lambda should then determine whether a relevant change has taken place.要获得文件级更改,您的 Lambda 应调用GetDifferences API。然后您的 Lambda 应确定是否发生了相关更改。
(4) Manually trigger a pipeline excecution if required. (4) 如果需要,手动触发流水线执行。 Your Lambda should call the StartPipelineExecution API or set the pipeline as a custom Event target .
您的 Lambda 应该调用StartPipelineExecution API 或将管道设置为自定义事件目标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.