如何指定目录来触发代码管道cdk?

How to Specify directory to trigger code pipeline cdk?

我正在使用 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'
    })
});

这是一项 DIY 工作。 CodePipeline 被设计为 运行 从开始到结束,无条件。诀窍是仅在发生相关更改时才触发管道。您必须用手动触发逻辑替换默认的 trigger-on-every-change 设置。对于 CodeCommit 回购:

(1)关闭流水线的自动触发。在 CodePipelineSource.

中设置 trigger: codepipeline_actions.CodeCommitTrigger.NONE 属性

(2) 创建一个 EventBridge Rule 来监听你的 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) 添加一个 Lambda 作为 rule's target. The Lambda will receive the referenceUpdated payload on a push to your repo. The event payload contains the commitId, but not the changed files. To get the file-level changes, your Lambda should call the GetDifferences API。然后,您的 Lambda 应确定是否发生了相关更改。

(4) 如果需要,手动触发管道执行。您的 Lambda 应该调用 StartPipelineExecution API or set the pipeline as a custom Event target.