Skip to content

Commit 9d63316

Browse files
authored
feat(scheduler-targets): CodeBuild scheduler target (#27792)
Closes #27448. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 3c7ca51 commit 9d63316

15 files changed

+35671
-2
lines changed

packages/@aws-cdk/aws-scheduler-targets-alpha/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ The following targets are supported:
2626

2727
1. `targets.LambdaInvoke`: [Invoke an AWS Lambda function](#invoke-a-lambda-function))
2828
2. `targets.StepFunctionsStartExecution`: [Start an AWS Step Function](#start-an-aws-step-function)
29+
3. `targets.CodeBuildStartBuild`: [Start a CodeBuild job](#start-a-codebuild-job)
2930

3031
## Invoke a Lambda function
3132

@@ -102,3 +103,21 @@ new Schedule(this, 'Schedule', {
102103
}),
103104
});
104105
```
106+
107+
## Start a CodeBuild job
108+
109+
Use the `CodeBuildStartBuild` target to start a new build run on a CodeBuild project.
110+
111+
The code snippet below creates an event rule with a CodeBuild project as target which is
112+
called every hour by Event Bridge Scheduler.
113+
114+
```ts
115+
import * as codebuild from 'aws-cdk-lib/aws-codebuild';
116+
117+
declare const project: codebuild.Project;
118+
119+
new Schedule(this, 'Schedule', {
120+
schedule: ScheduleExpression.rate(Duration.minutes(60)),
121+
target: new targets.CodeBuildStartBuild(project),
122+
});
123+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { ISchedule, IScheduleTarget } from '@aws-cdk/aws-scheduler-alpha';
2+
import { Names } from 'aws-cdk-lib';
3+
import { IProject } from 'aws-cdk-lib/aws-codebuild';
4+
import { IRole, PolicyStatement } from 'aws-cdk-lib/aws-iam';
5+
import { ScheduleTargetBase, ScheduleTargetBaseProps } from './target';
6+
import { sameEnvDimension } from './util';
7+
8+
/**
9+
* Use an AWS CodeBuild as a target for AWS EventBridge Scheduler.
10+
*/
11+
export class CodeBuildStartBuild extends ScheduleTargetBase implements IScheduleTarget {
12+
constructor(
13+
private readonly project: IProject,
14+
private readonly props: ScheduleTargetBaseProps = {},
15+
) {
16+
super(props, project.projectArn);
17+
}
18+
19+
protected addTargetActionToRole(schedule: ISchedule, role: IRole): void {
20+
if (!sameEnvDimension(this.project.env.region, schedule.env.region)) {
21+
throw new Error(`Cannot assign project in region ${this.project.env.region} to the schedule ${Names.nodeUniqueId(schedule.node)} in region ${schedule.env.region}. Both the schedule and the project must be in the same region.`);
22+
}
23+
24+
if (!sameEnvDimension(this.project.env.account, schedule.env.account)) {
25+
throw new Error(`Cannot assign project in account ${this.project.env.account} to the schedule ${Names.nodeUniqueId(schedule.node)} in account ${schedule.env.region}. Both the schedule and the project must be in the same account.`);
26+
}
27+
28+
if (this.props.role && !sameEnvDimension(this.props.role.env.account, schedule.env.account)) {
29+
throw new Error(`Cannot grant permission to execution role in account ${this.props.role.env.account} to invoke target ${Names.nodeUniqueId(schedule.node)} in account ${schedule.env.account}. Both the target and the execution role must be in the same account.`);
30+
}
31+
32+
role.addToPrincipalPolicy(new PolicyStatement({
33+
actions: ['codebuild:StartBuild'],
34+
resources: [this.project.projectArn],
35+
}));
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './target';
22
export * from './lambda-invoke';
3-
export * from './stepfunctions-start-execution';
3+
export * from './stepfunctions-start-execution';
4+
export * from './codebuild-start-build';

packages/@aws-cdk/aws-scheduler-targets-alpha/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,4 @@
114114
"assert/assert-dependency"
115115
]
116116
}
117-
}
117+
}

0 commit comments

Comments
 (0)