Skip to content

Commit 524844c

Browse files
Add bitbucket support (#34)
* Add support for codestar source in CodePipeline * expose local codebuild cache variables * Expose github_anonymous variable to allow setting it on the webhooks module * bump version of cloudposse/terraform-aws-codebuild * Updated README.md Co-authored-by: actions-bot <[email protected]>
1 parent e9695c1 commit 524844c

File tree

4 files changed

+157
-5
lines changed

4 files changed

+157
-5
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,18 @@ Available targets:
220220
| build\_timeout | How long in minutes, from 5 to 480 (8 hours), for AWS CodeBuild to wait until timing out any related build that does not get marked as completed | `number` | `60` | no |
221221
| buildspec | Declaration to use for building the project. [For more info](http://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html) | `string` | `""` | no |
222222
| cache\_type | The type of storage that will be used for the AWS CodeBuild project cache. Valid values: NO\_CACHE, LOCAL, and S3. Defaults to S3. If cache\_type is S3, it will create an S3 bucket for storing codebuild cache inside | `string` | `"S3"` | no |
223+
| codestar\_connection\_arn | CodeStar connection ARN required for Bitbucket integration with CodePipeline | `string` | `""` | no |
223224
| delimiter | Delimiter between `namespace`, `stage`, `name` and `attributes` | `string` | `"-"` | no |
224225
| ecs\_cluster\_name | ECS Cluster Name | `string` | n/a | yes |
225226
| enabled | Enable `CodePipeline` creation | `bool` | `true` | no |
226227
| environment\_variables | A list of maps, that contain both the key 'name' and the key 'value' to be used as additional environment variables for the build | <pre>list(object(<br> {<br> name = string<br> value = string<br> }))</pre> | `[]` | no |
227-
| github\_oauth\_token | GitHub OAuth Token with permissions to access private repositories | `string` | n/a | yes |
228+
| github\_anonymous | Github Anonymous API (if `true`, token must not be set as GITHUB\_TOKEN or `github_token`) | `bool` | `false` | no |
229+
| github\_oauth\_token | GitHub OAuth Token with permissions to access private repositories | `string` | `""` | no |
228230
| github\_webhook\_events | A list of events which should trigger the webhook. See a list of [available events](https://developer.github.com/v3/activity/events/types/) | `list(string)` | <pre>[<br> "push"<br>]</pre> | no |
229231
| github\_webhooks\_token | GitHub OAuth Token with permissions to create webhooks. If not provided, can be sourced from the `GITHUB_TOKEN` environment variable | `string` | `""` | no |
230232
| image\_repo\_name | ECR repository name to store the Docker image built by this module. Used as CodeBuild ENV variable when building Docker images. [For more info](http://docs.aws.amazon.com/codebuild/latest/userguide/sample-docker.html) | `string` | n/a | yes |
231233
| image\_tag | Docker image tag in the ECR repository, e.g. 'latest'. Used as CodeBuild ENV variable when building Docker images. [For more info](http://docs.aws.amazon.com/codebuild/latest/userguide/sample-docker.html) | `string` | `"latest"` | no |
234+
| local\_cache\_modes | Specifies settings that AWS CodeBuild uses to store and reuse build dependencies. Valid values: LOCAL\_SOURCE\_CACHE, LOCAL\_DOCKER\_LAYER\_CACHE, and LOCAL\_CUSTOM\_CACHE | `list(string)` | `[]` | no |
232235
| name | Name of the application | `string` | n/a | yes |
233236
| namespace | Namespace (e.g. `eg` or `cp`) | `string` | `""` | no |
234237
| poll\_source\_changes | Periodically check the location of your source content and run the pipeline if changes are detected | `bool` | `false` | no |

docs/terraform.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,18 @@
2929
| build\_timeout | How long in minutes, from 5 to 480 (8 hours), for AWS CodeBuild to wait until timing out any related build that does not get marked as completed | `number` | `60` | no |
3030
| buildspec | Declaration to use for building the project. [For more info](http://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html) | `string` | `""` | no |
3131
| cache\_type | The type of storage that will be used for the AWS CodeBuild project cache. Valid values: NO\_CACHE, LOCAL, and S3. Defaults to S3. If cache\_type is S3, it will create an S3 bucket for storing codebuild cache inside | `string` | `"S3"` | no |
32+
| codestar\_connection\_arn | CodeStar connection ARN required for Bitbucket integration with CodePipeline | `string` | `""` | no |
3233
| delimiter | Delimiter between `namespace`, `stage`, `name` and `attributes` | `string` | `"-"` | no |
3334
| ecs\_cluster\_name | ECS Cluster Name | `string` | n/a | yes |
3435
| enabled | Enable `CodePipeline` creation | `bool` | `true` | no |
3536
| environment\_variables | A list of maps, that contain both the key 'name' and the key 'value' to be used as additional environment variables for the build | <pre>list(object(<br> {<br> name = string<br> value = string<br> }))</pre> | `[]` | no |
36-
| github\_oauth\_token | GitHub OAuth Token with permissions to access private repositories | `string` | n/a | yes |
37+
| github\_anonymous | Github Anonymous API (if `true`, token must not be set as GITHUB\_TOKEN or `github_token`) | `bool` | `false` | no |
38+
| github\_oauth\_token | GitHub OAuth Token with permissions to access private repositories | `string` | `""` | no |
3739
| github\_webhook\_events | A list of events which should trigger the webhook. See a list of [available events](https://developer.github.com/v3/activity/events/types/) | `list(string)` | <pre>[<br> "push"<br>]</pre> | no |
3840
| github\_webhooks\_token | GitHub OAuth Token with permissions to create webhooks. If not provided, can be sourced from the `GITHUB_TOKEN` environment variable | `string` | `""` | no |
3941
| image\_repo\_name | ECR repository name to store the Docker image built by this module. Used as CodeBuild ENV variable when building Docker images. [For more info](http://docs.aws.amazon.com/codebuild/latest/userguide/sample-docker.html) | `string` | n/a | yes |
4042
| image\_tag | Docker image tag in the ECR repository, e.g. 'latest'. Used as CodeBuild ENV variable when building Docker images. [For more info](http://docs.aws.amazon.com/codebuild/latest/userguide/sample-docker.html) | `string` | `"latest"` | no |
43+
| local\_cache\_modes | Specifies settings that AWS CodeBuild uses to store and reuse build dependencies. Valid values: LOCAL\_SOURCE\_CACHE, LOCAL\_DOCKER\_LAYER\_CACHE, and LOCAL\_CUSTOM\_CACHE | `list(string)` | `[]` | no |
4144
| name | Name of the application | `string` | n/a | yes |
4245
| namespace | Namespace (e.g. `eg` or `cp`) | `string` | `""` | no |
4346
| poll\_source\_changes | Periodically check the location of your source content and run the pipeline if changes are detected | `bool` | `false` | no |

main.tf

+128-2
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,61 @@ data "aws_iam_policy_document" "codebuild" {
167167
}
168168
}
169169

170+
# https://docs.aws.amazon.com/codepipeline/latest/userguide/connections-permissions.html
171+
resource "aws_iam_role_policy_attachment" "codestar" {
172+
count = var.enabled && var.codestar_connection_arn != "" ? 1 : 0
173+
role = join("", aws_iam_role.default.*.id)
174+
policy_arn = join("", aws_iam_policy.codestar.*.arn)
175+
}
176+
177+
module "codestar_label" {
178+
source = "github.com/cloudposse/terraform-null-label.git?ref=0.16.0"
179+
enabled = var.enabled && var.codestar_connection_arn != ""
180+
attributes = compact(concat(var.attributes, ["codestar"]))
181+
delimiter = var.delimiter
182+
name = var.name
183+
namespace = var.namespace
184+
stage = var.stage
185+
tags = var.tags
186+
}
187+
188+
resource "aws_iam_policy" "codestar" {
189+
count = var.enabled && var.codestar_connection_arn != "" ? 1 : 0
190+
name = module.codestar_label.id
191+
policy = join("", data.aws_iam_policy_document.codestar.*.json)
192+
}
193+
194+
data "aws_iam_policy_document" "codestar" {
195+
count = var.enabled && var.codestar_connection_arn != "" ? 1 : 0
196+
statement {
197+
sid = ""
198+
199+
actions = [
200+
"codestar-connections:UseConnection"
201+
]
202+
203+
condition {
204+
test = "StringLike"
205+
variable = "codestar-connections:FullRepositoryId"
206+
values = [
207+
format("%s/%s", var.repo_owner, var.repo_name)
208+
]
209+
}
210+
211+
resources = [var.codestar_connection_arn]
212+
effect = "Allow"
213+
214+
}
215+
}
216+
170217
data "aws_caller_identity" "default" {
171218
}
172219

173220
data "aws_region" "default" {
174221
}
175222

176223
module "codebuild" {
177-
source = "git::https://github.com/cloudposse/terraform-aws-codebuild.git?ref=tags/0.20.0"
224+
source = "git::https://github.com/cloudposse/terraform-aws-codebuild.git?ref=tags/0.21.0"
178225
enabled = var.enabled
179226
namespace = var.namespace
180227
name = var.name
@@ -195,6 +242,7 @@ module "codebuild" {
195242
environment_variables = var.environment_variables
196243
badge_enabled = var.badge_enabled
197244
cache_type = var.cache_type
245+
local_cache_modes = var.local_cache_modes
198246
}
199247

200248
resource "aws_iam_role_policy_attachment" "codebuild_s3" {
@@ -204,7 +252,7 @@ resource "aws_iam_role_policy_attachment" "codebuild_s3" {
204252
}
205253

206254
resource "aws_codepipeline" "default" {
207-
count = var.enabled ? 1 : 0
255+
count = var.enabled && var.github_oauth_token != "" ? 1 : 0
208256
name = module.codepipeline_label.id
209257
role_arn = join("", aws_iam_role.default.*.arn)
210258

@@ -279,6 +327,83 @@ resource "aws_codepipeline" "default" {
279327
}
280328
}
281329

330+
# https://docs.aws.amazon.com/codepipeline/latest/userguide/action-reference-CodestarConnectionSource.html#action-reference-CodestarConnectionSource-example
331+
resource "aws_codepipeline" "bitbucket" {
332+
count = var.enabled && var.codestar_connection_arn != "" ? 1 : 0
333+
name = module.codepipeline_label.id
334+
role_arn = join("", aws_iam_role.default.*.arn)
335+
336+
artifact_store {
337+
location = join("", aws_s3_bucket.default.*.bucket)
338+
type = "S3"
339+
}
340+
341+
depends_on = [
342+
aws_iam_role_policy_attachment.default,
343+
aws_iam_role_policy_attachment.s3,
344+
aws_iam_role_policy_attachment.codebuild,
345+
aws_iam_role_policy_attachment.codebuild_s3,
346+
aws_iam_role_policy_attachment.codestar
347+
]
348+
349+
stage {
350+
name = "Source"
351+
352+
action {
353+
name = "Source"
354+
category = "Source"
355+
owner = "AWS"
356+
provider = "CodeStarSourceConnection"
357+
version = "1"
358+
output_artifacts = ["code"]
359+
360+
configuration = {
361+
ConnectionArn = var.codestar_connection_arn
362+
FullRepositoryId = format("%s/%s", var.repo_owner, var.repo_name)
363+
BranchName = var.branch
364+
OutputArtifactFormat = "CODE_ZIP"
365+
}
366+
}
367+
}
368+
369+
stage {
370+
name = "Build"
371+
372+
action {
373+
name = "Build"
374+
category = "Build"
375+
owner = "AWS"
376+
provider = "CodeBuild"
377+
version = "1"
378+
379+
input_artifacts = ["code"]
380+
output_artifacts = ["task"]
381+
382+
configuration = {
383+
ProjectName = module.codebuild.project_name
384+
}
385+
}
386+
}
387+
388+
stage {
389+
name = "Deploy"
390+
391+
action {
392+
name = "Deploy"
393+
category = "Deploy"
394+
owner = "AWS"
395+
provider = "ECS"
396+
input_artifacts = ["task"]
397+
version = "1"
398+
399+
configuration = {
400+
ClusterName = var.ecs_cluster_name
401+
ServiceName = var.service_name
402+
}
403+
}
404+
}
405+
}
406+
282407
resource "random_string" "webhook_secret" {
283408
count = var.enabled && var.webhook_enabled ? 1 : 0
284409
length = 32
@@ -312,6 +437,7 @@ resource "aws_codepipeline_webhook" "webhook" {
312437
module "github_webhooks" {
313438
source = "git::https://github.com/cloudposse/terraform-github-repository-webhooks.git?ref=tags/0.7.0"
314439
enabled = var.enabled && var.webhook_enabled ? true : false
440+
github_anonymous = var.github_anonymous
315441
github_organization = var.repo_owner
316442
github_repositories = [var.repo_name]
317443
github_token = var.github_webhooks_token

variables.tf

+21-1
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,16 @@ variable "service_name" {
4949
description = "ECS Service Name"
5050
}
5151

52+
variable "github_anonymous" {
53+
type = bool
54+
description = "Github Anonymous API (if `true`, token must not be set as GITHUB_TOKEN or `github_token`)"
55+
default = false
56+
}
57+
5258
variable "github_oauth_token" {
5359
type = string
5460
description = "GitHub OAuth Token with permissions to access private repositories"
61+
default = ""
5562
}
5663

5764
variable "github_webhooks_token" {
@@ -194,8 +201,21 @@ variable "s3_bucket_force_destroy" {
194201
default = false
195202
}
196203

204+
variable "codestar_connection_arn" {
205+
type = string
206+
description = "CodeStar connection ARN required for Bitbucket integration with CodePipeline"
207+
default = ""
208+
}
209+
197210
variable "cache_type" {
198211
type = string
199212
default = "S3"
200213
description = "The type of storage that will be used for the AWS CodeBuild project cache. Valid values: NO_CACHE, LOCAL, and S3. Defaults to S3. If cache_type is S3, it will create an S3 bucket for storing codebuild cache inside"
201-
}
214+
}
215+
216+
variable "local_cache_modes" {
217+
type = list(string)
218+
default = []
219+
description = "Specifies settings that AWS CodeBuild uses to store and reuse build dependencies. Valid values: LOCAL_SOURCE_CACHE, LOCAL_DOCKER_LAYER_CACHE, and LOCAL_CUSTOM_CACHE"
220+
}
221+

0 commit comments

Comments
 (0)