AWS CDK 将分支名称从 Event 传递到 CodeBuild 项目
AWS CDK pass branch name from Event to CodeBuild project
在 AWS CDK 中,我想定义一个 CodeBuild 项目,每次在 CodeCommit 中打开或更新拉取请求时都会 运行。我这样做是为了能够在合并到主分支之前验证我的测试和构建。
如何为与拉取请求关联的分支创建此 CodeBuild 项目 运行?
下面是我的代码:
import { Repository } from 'aws-cdk-lib/aws-codecommit';
import { BuildSpec, Project, Source } from 'aws-cdk-lib/aws-codebuild';
import { CodeBuildProject } from 'aws-cdk-lib/aws-events-targets';
const repo = Repository.fromRepositoryName(this, 'MyRepo', 'my-repo');
const project = new Project(this, 'MyCodeBuildProject', {
source: Source.codeCommit({ repository: repo }),
buildSpec: BuildSpec.fromObject({
version: '0.2',
phases: {
build: {
commands: [ 'npm run build' ],
},
},
}),
});
const myRule = repo.onPullRequestStateChange('MyRule', {
target: new targets.CodeBuildProject(project),
});
我试过以这种方式将它提供给项目源:
import { ReferenceEvent } from 'aws-cdk-lib/aws-codecommit';
...
source: Source.codeCommit({ repository: repo, branchOrRef: ReferenceEvent.name }),
但是我收到这个错误:
reference not found for primary source and source version $.detail.referenceName
CodeCommit -> 打开拉取请求 -> CloudWatch 事件 (EventBridge) -> CodeBuild
AWS CDK v2.5.0
打字稿
我能够通过从事件中提取分支然后将其传递给目标来解决这个问题。
import { EventField, RuleTargetInput } from 'aws-cdk-lib/aws-events';
const myRule = repo.onPullRequestStateChange('MyRule', {
target: new targets.CodeBuildProject(project, {
event: RuleTargetInput.fromObject({
sourceVersion: EventField.fromPath('$.detail.sourceReference'),
}),
}),
});
之所以有效,是因为 targets.CodeBuildProject() will call the CodeBuild StartBuild API. The event
key in CodeBuildProjectProps
specifies the payload that will be sent to the StartBuild API. by default, the entire Event is sent, but this is not in the format expected by CodeBuild. The StartBuild API payload allows a branch, commit, or tag to be specified using sourceVersion
. We can extract data from the event using EventField. Events from CodeCommit 的引用嵌套在 detail.sourceReference
下。这将类似于 'refs/heads/my-branch'
。使用 Eventfield.fromPath()
我们可以使用 $.
语法来访问触发此规则的事件,然后为 JSON 路径提供点符号字符串以访问我们需要的数据。
在 AWS CDK 中,我想定义一个 CodeBuild 项目,每次在 CodeCommit 中打开或更新拉取请求时都会 运行。我这样做是为了能够在合并到主分支之前验证我的测试和构建。
如何为与拉取请求关联的分支创建此 CodeBuild 项目 运行?
下面是我的代码:
import { Repository } from 'aws-cdk-lib/aws-codecommit';
import { BuildSpec, Project, Source } from 'aws-cdk-lib/aws-codebuild';
import { CodeBuildProject } from 'aws-cdk-lib/aws-events-targets';
const repo = Repository.fromRepositoryName(this, 'MyRepo', 'my-repo');
const project = new Project(this, 'MyCodeBuildProject', {
source: Source.codeCommit({ repository: repo }),
buildSpec: BuildSpec.fromObject({
version: '0.2',
phases: {
build: {
commands: [ 'npm run build' ],
},
},
}),
});
const myRule = repo.onPullRequestStateChange('MyRule', {
target: new targets.CodeBuildProject(project),
});
我试过以这种方式将它提供给项目源:
import { ReferenceEvent } from 'aws-cdk-lib/aws-codecommit';
...
source: Source.codeCommit({ repository: repo, branchOrRef: ReferenceEvent.name }),
但是我收到这个错误:
reference not found for primary source and source version $.detail.referenceName
CodeCommit -> 打开拉取请求 -> CloudWatch 事件 (EventBridge) -> CodeBuild
AWS CDK v2.5.0
打字稿
我能够通过从事件中提取分支然后将其传递给目标来解决这个问题。
import { EventField, RuleTargetInput } from 'aws-cdk-lib/aws-events';
const myRule = repo.onPullRequestStateChange('MyRule', {
target: new targets.CodeBuildProject(project, {
event: RuleTargetInput.fromObject({
sourceVersion: EventField.fromPath('$.detail.sourceReference'),
}),
}),
});
之所以有效,是因为 targets.CodeBuildProject() will call the CodeBuild StartBuild API. The event
key in CodeBuildProjectProps
specifies the payload that will be sent to the StartBuild API. by default, the entire Event is sent, but this is not in the format expected by CodeBuild. The StartBuild API payload allows a branch, commit, or tag to be specified using sourceVersion
. We can extract data from the event using EventField. Events from CodeCommit 的引用嵌套在 detail.sourceReference
下。这将类似于 'refs/heads/my-branch'
。使用 Eventfield.fromPath()
我们可以使用 $.
语法来访问触发此规则的事件,然后为 JSON 路径提供点符号字符串以访问我们需要的数据。