Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(scheduler-targets): add CodePipeline as target for scheduler #27799

Merged
merged 23 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { ISchedule, IScheduleTarget } from '@aws-cdk/aws-scheduler-alpha';
import { Names } from 'aws-cdk-lib';
import { IPipeline } from 'aws-cdk-lib/aws-codepipeline';
import { IRole, PolicyStatement } from 'aws-cdk-lib/aws-iam';
import { ScheduleTargetBase, ScheduleTargetBaseProps } from './target';
import { sameEnvDimension } from './util';

export class CodePipelineStartPipelineExecution extends ScheduleTargetBase implements IScheduleTarget {
constructor(
private readonly pipeline: IPipeline,
private readonly props: ScheduleTargetBaseProps,
) {
super(props, pipeline.pipelineArn);
}

protected addTargetActionToRole(schedule: ISchedule, role: IRole): void {
const pipelineEnv = this.pipeline.env;
if (!sameEnvDimension(pipelineEnv.region, schedule.env.region)) {
throw new Error(`Cannot assign pipeline in region ${pipelineEnv.region} to the schedule ${Names.nodeUniqueId(schedule.node)} in region ${schedule.env.region}. Both the schedule and the pipeline must be in the same region.`);
}

if (!sameEnvDimension(pipelineEnv.account, schedule.env.account)) {
throw new Error(`Cannot assign pipeline in account ${pipelineEnv.account} to the schedule ${Names.nodeUniqueId(schedule.node)} in account ${schedule.env.region}. Both the schedule and the pipeline must be in the same account.`);
}

if (this.props.role && !sameEnvDimension(this.props.role.env.account, pipelineEnv.account)) {
throw new Error(`Cannot grant permission to execution role in account ${this.props.role.env.account} to invoke target ${Names.nodeUniqueId(this.pipeline.node)} in account ${pipelineEnv.account}. Both the target and the execution role must be in the same account.`);
}

role.addToPrincipalPolicy(new PolicyStatement({
actions: ['codepipeline:StartPipelineExecution'],
resources: [this.pipeline.pipelineArn],
}));
}
}
Loading
Loading