or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

analytics-targets.mdapi-targets.mdcicd-targets.mdcompute-targets.mdindex.mdmessaging-targets.mdorchestration-targets.mdsystem-targets.md

cicd-targets.mddocs/

0

# CI/CD Targets

1

2

Targets for continuous integration and deployment services that can be triggered by EventBridge events.

3

4

## Capabilities

5

6

### CodeBuild Project Target

7

8

Trigger AWS CodeBuild projects in response to EventBridge events.

9

10

```typescript { .api }

11

/**

12

* Use a CodeBuild project as a target for Amazon EventBridge rules

13

*/

14

class CodeBuildProject implements events.IRuleTarget {

15

constructor(project: codebuild.IProject, props?: CodeBuildProjectProps);

16

17

/**

18

* Returns a RuleTarget that can be used to trigger this CodeBuild project

19

* as a result from an EventBridge event

20

*/

21

bind(rule: events.IRule, id?: string): events.RuleTargetConfig;

22

}

23

24

interface CodeBuildProjectProps extends TargetBaseProps {

25

/**

26

* The role to assume before invoking the target

27

* @default a new role will be created

28

*/

29

readonly eventRole?: iam.IRole;

30

31

/**

32

* The event to send to CodeBuild

33

* This will be available as environment variables in the build

34

* @default the entire EventBridge event

35

*/

36

readonly event?: events.RuleTargetInput;

37

}

38

```

39

40

**Usage Example:**

41

42

```typescript

43

import * as codebuild from "@aws-cdk/aws-codebuild";

44

import * as codecommit from "@aws-cdk/aws-codecommit";

45

import * as events from "@aws-cdk/aws-events";

46

import * as targets from "@aws-cdk/aws-events-targets";

47

import * as sqs from "@aws-cdk/aws-sqs";

48

49

// Create CodeCommit repository

50

const repo = new codecommit.Repository(this, "MyRepo", {

51

repositoryName: "my-application",

52

description: "Application source code repository",

53

});

54

55

// Create CodeBuild project

56

const project = new codebuild.Project(this, "BuildProject", {

57

projectName: "my-app-build",

58

source: codebuild.Source.codeCommit({ repository: repo }),

59

environment: {

60

buildImage: codebuild.LinuxBuildImage.STANDARD_5_0,

61

computeType: codebuild.ComputeType.SMALL,

62

},

63

buildSpec: codebuild.BuildSpec.fromObject({

64

version: "0.2",

65

phases: {

66

pre_build: {

67

commands: [

68

"echo Logging in to Amazon ECR...",

69

"aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com",

70

],

71

},

72

build: {

73

commands: [

74

"echo Build started on `date`",

75

"echo Building the Docker image...",

76

"docker build -t $IMAGE_REPO_NAME:$IMAGE_TAG .",

77

"docker tag $IMAGE_REPO_NAME:$IMAGE_TAG $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG",

78

],

79

},

80

post_build: {

81

commands: [

82

"echo Build completed on `date`",

83

"echo Pushing the Docker image...",

84

"docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG",

85

],

86

},

87

},

88

}),

89

});

90

91

// Create dead letter queue for failed builds

92

const buildDlq = new sqs.Queue(this, "BuildDeadLetterQueue");

93

94

// Trigger build on repository commits to master branch

95

const commitRule = repo.onCommit("OnCommitToMaster", {

96

target: new targets.CodeBuildProject(project, {

97

deadLetterQueue: buildDlq,

98

retryAttempts: 2,

99

maxEventAge: Duration.hours(1),

100

event: events.RuleTargetInput.fromObject({

101

commitId: events.EventField.fromPath("$.detail.commitId"),

102

repositoryName: events.EventField.fromPath("$.detail.repositoryName"),

103

referenceName: events.EventField.fromPath("$.detail.referenceName"),

104

author: events.EventField.fromPath("$.detail.author"),

105

}),

106

}),

107

branches: ["master", "main"],

108

});

109

110

// Trigger build on S3 artifact updates

111

const s3Rule = new events.Rule(this, "S3ArtifactRule", {

112

eventPattern: {

113

source: ["aws.s3"],

114

detailType: ["Object Created"],

115

detail: {

116

bucket: { name: ["my-artifacts-bucket"] },

117

object: { key: [{ prefix: "source-code/" }] },

118

},

119

},

120

});

121

122

s3Rule.addTarget(new targets.CodeBuildProject(project, {

123

event: events.RuleTargetInput.fromObject({

124

buildReason: "S3 artifact updated",

125

s3Bucket: events.EventField.fromPath("$.detail.bucket.name"),

126

s3Key: events.EventField.fromPath("$.detail.object.key"),

127

eventTime: events.EventField.fromPath("$.time"),

128

}),

129

}));

130

```

131

132

### CodePipeline Target

133

134

Start AWS CodePipeline pipelines in response to EventBridge events.

135

136

```typescript { .api }

137

/**

138

* Use a CodePipeline pipeline as a target for Amazon EventBridge rules

139

*/

140

class CodePipeline implements events.IRuleTarget {

141

constructor(pipeline: codepipeline.IPipeline, options?: CodePipelineTargetOptions);

142

143

/**

144

* Returns a RuleTarget that can be used to trigger this CodePipeline

145

* as a result from an EventBridge event

146

*/

147

bind(rule: events.IRule, id?: string): events.RuleTargetConfig;

148

}

149

150

interface CodePipelineTargetOptions extends TargetBaseProps {

151

/**

152

* The role to assume before invoking the target

153

* @default a new role will be created

154

*/

155

readonly eventRole?: iam.IRole;

156

}

157

```

158

159

**Usage Example:**

160

161

```typescript

162

import * as codepipeline from "@aws-cdk/aws-codepipeline";

163

import * as codepipelineActions from "@aws-cdk/aws-codepipeline-actions";

164

import * as codecommit from "@aws-cdk/aws-codecommit";

165

import * as codebuild from "@aws-cdk/aws-codebuild";

166

import * as s3 from "@aws-cdk/aws-s3";

167

import * as events from "@aws-cdk/aws-events";

168

import * as targets from "@aws-cdk/aws-events-targets";

169

170

// Create source repository and artifacts bucket

171

const sourceRepo = new codecommit.Repository(this, "SourceRepo", {

172

repositoryName: "my-app-source",

173

});

174

175

const artifactsBucket = new s3.Bucket(this, "ArtifactsBucket");

176

177

// Create CodeBuild project for the pipeline

178

const buildProject = new codebuild.Project(this, "PipelineBuild", {

179

source: codebuild.Source.codeCommit({ repository: sourceRepo }),

180

environment: {

181

buildImage: codebuild.LinuxBuildImage.STANDARD_5_0,

182

},

183

});

184

185

// Create CodePipeline

186

const pipeline = new codepipeline.Pipeline(this, "DeploymentPipeline", {

187

pipelineName: "my-app-pipeline",

188

artifactBucket: artifactsBucket,

189

stages: [

190

{

191

stageName: "Source",

192

actions: [

193

new codepipelineActions.CodeCommitSourceAction({

194

actionName: "Source",

195

repository: sourceRepo,

196

output: new codepipeline.Artifact("SourceOutput"),

197

}),

198

],

199

},

200

{

201

stageName: "Build",

202

actions: [

203

new codepipelineActions.CodeBuildAction({

204

actionName: "Build",

205

project: buildProject,

206

input: new codepipeline.Artifact("SourceOutput"),

207

outputs: [new codepipeline.Artifact("BuildOutput")],

208

}),

209

],

210

},

211

{

212

stageName: "Deploy",

213

actions: [

214

new codepipelineActions.S3DeployAction({

215

actionName: "Deploy",

216

bucket: artifactsBucket,

217

input: new codepipeline.Artifact("BuildOutput"),

218

}),

219

],

220

},

221

],

222

});

223

224

// Schedule pipeline execution

225

const scheduleRule = new events.Rule(this, "ScheduledDeployment", {

226

description: "Trigger deployment pipeline daily at 2 AM",

227

schedule: events.Schedule.cron({

228

hour: "2",

229

minute: "0",

230

}),

231

});

232

233

scheduleRule.addTarget(new targets.CodePipeline(pipeline, {

234

retryAttempts: 1,

235

maxEventAge: Duration.hours(2),

236

}));

237

238

// Trigger pipeline on external events

239

const releaseRule = new events.Rule(this, "ReleaseRule", {

240

eventPattern: {

241

source: ["myapp.releases"],

242

detailType: ["Release Approved"],

243

detail: {

244

environment: ["production"],

245

status: ["approved"],

246

},

247

},

248

});

249

250

releaseRule.addTarget(new targets.CodePipeline(pipeline));

251

252

// Cross-region pipeline trigger

253

const crossRegionRule = new events.Rule(this, "CrossRegionRule", {

254

eventPattern: {

255

source: ["aws.codepipeline"],

256

detailType: ["CodePipeline Pipeline Execution State Change"],

257

detail: {

258

state: ["SUCCEEDED"],

259

pipeline: ["upstream-pipeline"],

260

},

261

},

262

});

263

264

crossRegionRule.addTarget(new targets.CodePipeline(pipeline));

265

```

266

267

## Common CI/CD Patterns

268

269

### Automated Build Triggers

270

271

```typescript

272

// Multi-branch build triggers

273

const multiBranchRule = new events.Rule(this, "MultiBranchRule", {

274

eventPattern: {

275

source: ["aws.codecommit"],

276

detailType: ["CodeCommit Repository State Change"],

277

detail: {

278

event: ["referenceCreated", "referenceUpdated"],

279

referenceName: [

280

{ prefix: "refs/heads/feature/" },

281

{ prefix: "refs/heads/release/" },

282

"refs/heads/develop",

283

],

284

},

285

},

286

});

287

288

multiBranchRule.addTarget(new targets.CodeBuildProject(buildProject, {

289

event: events.RuleTargetInput.fromObject({

290

branchName: events.EventField.fromPath("$.detail.referenceName"),

291

commitId: events.EventField.fromPath("$.detail.commitId"),

292

buildType: "feature-build",

293

}),

294

}));

295

```

296

297

### Integration with External Systems

298

299

```typescript

300

// Trigger builds from external webhook events via custom EventBridge events

301

const webhookRule = new events.Rule(this, "WebhookRule", {

302

eventPattern: {

303

source: ["myapp.webhooks"],

304

detailType: ["GitHub Push", "GitLab Push"],

305

detail: {

306

repository: ["my/repository"],

307

ref: [{ prefix: "refs/heads/" }],

308

},

309

},

310

});

311

312

webhookRule.addTarget(new targets.CodeBuildProject(project, {

313

event: events.RuleTargetInput.fromObject({

314

repository: events.EventField.fromPath("$.detail.repository"),

315

branch: events.EventField.fromPath("$.detail.ref"),

316

commitSha: events.EventField.fromPath("$.detail.after"),

317

pusher: events.EventField.fromPath("$.detail.pusher.name"),

318

buildReason: "External webhook trigger",

319

}),

320

}));

321

```

322

323

### Build Status Notifications

324

325

```typescript

326

// React to build completion events

327

const buildCompleteRule = new events.Rule(this, "BuildCompleteRule", {

328

eventPattern: {

329

source: ["aws.codebuild"],

330

detailType: ["CodeBuild Build State Change"],

331

detail: {

332

"build-status": ["SUCCEEDED", "FAILED"],

333

"project-name": [project.projectName],

334

},

335

},

336

});

337

338

// Trigger downstream pipeline on successful build

339

buildCompleteRule.addTarget(new targets.CodePipeline(pipeline));

340

```