Skip to content

Commit

Permalink
Add option to enable LOCAL_CACHE (#50)
Browse files Browse the repository at this point in the history
* add option to set local cache

* remove cache enabled option

* Updated README.md

* Fix docs about Cache  type

* Updated README.md

* example fixed

* example fixed

Co-authored-by: Maxim Mironenko <[email protected]>
Co-authored-by: actions-bot <[email protected]>
  • Loading branch information
3 people authored Mar 3, 2020
1 parent c98a470 commit da6ad7b
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 42 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,16 @@ Available targets:
| build_image | Docker image for build environment, e.g. 'aws/codebuild/standard:2.0' or 'aws/codebuild/eb-nodejs-6.10.0-amazonlinux-64:4.0.0'. For more info: http://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref.html | string | `aws/codebuild/standard:2.0` | no |
| 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 | string | `60` | no |
| buildspec | Optional buildspec declaration to use for building the project | string | `` | no |
| cache_bucket_suffix_enabled | The cache bucket generates a random 13 character string to generate a unique bucket name. If set to false it uses terraform-null-label's id value | bool | `true` | no |
| cache_enabled | If cache_enabled is true, create an S3 bucket for storing codebuild cache inside | bool | `true` | no |
| cache_expiration_days | How many days should the build cache be kept | string | `7` | no |
| cache_bucket_suffix_enabled | The cache bucket generates a random 13 character string to generate a unique bucket name. If set to false it uses terraform-null-label's id value. It only works when cache_type is 'S3 | bool | `true` | no |
| cache_expiration_days | How many days should the build cache be kept. It only works when cache_type is 'S3' | string | `7` | no |
| 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 NO_CACHE. If cache_type is S3, it will create an S3 bucket for storing codebuild cache inside | string | `NO_CACHE` | no |
| delimiter | Delimiter to be used between `name`, `namespace`, `stage`, etc. | string | `-` | no |
| enabled | A boolean to enable/disable resource creation | bool | `true` | no |
| 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 | object | `<list>` | no |
| github_token | (Optional) GitHub auth token environment variable (`GITHUB_TOKEN`) | string | `` | no |
| image_repo_name | (Optional) 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 | `UNSET` | no |
| image_tag | (Optional) 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 |
| 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) | `<list>` | no |
| name | Solution name, e.g. 'app' or 'jenkins' | string | - | yes |
| namespace | Namespace, which could be your organization name, e.g. 'eg' or 'cp' | string | `` | no |
| privileged_mode | (Optional) If set to true, enables running the Docker daemon inside a Docker container on the CodeBuild instance. Used when building Docker images | bool | `false` | no |
Expand Down
7 changes: 4 additions & 3 deletions docs/terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@
| build_image | Docker image for build environment, e.g. 'aws/codebuild/standard:2.0' or 'aws/codebuild/eb-nodejs-6.10.0-amazonlinux-64:4.0.0'. For more info: http://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref.html | string | `aws/codebuild/standard:2.0` | no |
| 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 | string | `60` | no |
| buildspec | Optional buildspec declaration to use for building the project | string | `` | no |
| cache_bucket_suffix_enabled | The cache bucket generates a random 13 character string to generate a unique bucket name. If set to false it uses terraform-null-label's id value | bool | `true` | no |
| cache_enabled | If cache_enabled is true, create an S3 bucket for storing codebuild cache inside | bool | `true` | no |
| cache_expiration_days | How many days should the build cache be kept | string | `7` | no |
| cache_bucket_suffix_enabled | The cache bucket generates a random 13 character string to generate a unique bucket name. If set to false it uses terraform-null-label's id value. It only works when cache_type is 'S3 | bool | `true` | no |
| cache_expiration_days | How many days should the build cache be kept. It only works when cache_type is 'S3' | string | `7` | no |
| 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 NO_CACHE. If cache_type is S3, it will create an S3 bucket for storing codebuild cache inside | string | `NO_CACHE` | no |
| delimiter | Delimiter to be used between `name`, `namespace`, `stage`, etc. | string | `-` | no |
| enabled | A boolean to enable/disable resource creation | bool | `true` | no |
| 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 | object | `<list>` | no |
| github_token | (Optional) GitHub auth token environment variable (`GITHUB_TOKEN`) | string | `` | no |
| image_repo_name | (Optional) 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 | `UNSET` | no |
| image_tag | (Optional) 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 |
| 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) | `<list>` | no |
| name | Solution name, e.g. 'app' or 'jenkins' | string | - | yes |
| namespace | Namespace, which could be your organization name, e.g. 'eg' or 'cp' | string | `` | no |
| privileged_mode | (Optional) If set to true, enables running the Docker daemon inside a Docker container on the CodeBuild instance. Used when building Docker images | bool | `false` | no |
Expand Down
6 changes: 4 additions & 2 deletions examples/complete/fixtures.us-west-1.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ stage = "test"

name = "cedebuild-test"

cache_enabled = true

cache_bucket_suffix_enabled = false

environment_variables = [
Expand All @@ -25,3 +23,7 @@ environment_variables = [

}
]

cache_expiration_days = 7

cache_type = "S3"
3 changes: 2 additions & 1 deletion examples/complete/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ module "codebuild" {
namespace = var.namespace
stage = var.stage
name = var.name
cache_enabled = var.cache_enabled
cache_bucket_suffix_enabled = var.cache_bucket_suffix_enabled
environment_variables = var.environment_variables
cache_expiration_days = var.cache_expiration_days
cache_type = var.cache_type
}
11 changes: 8 additions & 3 deletions examples/complete/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@ variable "environment_variables" {
description = "A list of maps, that contain both the key 'name' and the key 'value' to be used as additional environment variables for the build"
}

variable "cache_enabled" {
type = bool
description = "If cache_enabled is true, create an S3 bucket for storing codebuild cache inside"
variable "cache_expiration_days" {
type = number
description = "How many days should the build cache be kept. It only works when cache_type is 'S3'"
}

variable "cache_bucket_suffix_enabled" {
type = bool
description = "The cache bucket generates a random 13 character string to generate a unique bucket name. If set to false it uses terraform-null-label's id value"
}

variable "cache_type" {
type = string
description = "The type of storage that will be used for the AWS CodeBuild project cache. Valid values: NO_CACHE, LOCAL, and S3. Defaults to NO_CACHE. If cache_type is S3, it will create an S3 bucket for storing codebuild cache inside"
}
44 changes: 24 additions & 20 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module "label" {
}

resource "aws_s3_bucket" "cache_bucket" {
count = var.enabled && var.cache_enabled ? 1 : 0
count = var.enabled && local.s3_cache_enabled ? 1 : 0
bucket = local.cache_bucket_name_normalised
acl = "private"
force_destroy = true
Expand Down Expand Up @@ -54,21 +54,28 @@ locals {
min(length(local.cache_bucket_name), 63),
)

s3_cache_enabled = var.cache_type == "S3"

## This is the magic where a map of a list of maps is generated
## and used to conditionally add the cache bucket option to the
## aws_codebuild_project
cache_def = {
"true" = [
{
type = "S3"
location = var.enabled && var.cache_enabled ? join("", aws_s3_bucket.cache_bucket.*.bucket) : "none"
}
]
"false" = []
cache_options = {
"S3" = {
type = "S3"
location = var.enabled && local.s3_cache_enabled ? join("", aws_s3_bucket.cache_bucket.*.bucket) : "none"

},
"LOCAL" = {
type = "LOCAL"
modes = var.local_cache_modes
},
"NO_CACHE" = {
type = "NO_CACHE"
}
}

# Final Map Selected from above
cache = local.cache_def[var.cache_enabled ? "true" : "false"]
cache = local.cache_options[var.cache_type]
}

resource "aws_iam_role" "default" {
Expand Down Expand Up @@ -102,7 +109,7 @@ resource "aws_iam_policy" "default" {
}

resource "aws_iam_policy" "default_cache_bucket" {
count = var.enabled && var.cache_enabled ? 1 : 0
count = var.enabled && local.s3_cache_enabled ? 1 : 0
name = "${module.label.id}-cache-bucket"
path = "/service-role/"
policy = join("", data.aws_iam_policy_document.permissions_cache_bucket.*.json)
Expand Down Expand Up @@ -136,7 +143,7 @@ data "aws_iam_policy_document" "permissions" {
}

data "aws_iam_policy_document" "permissions_cache_bucket" {
count = var.enabled && var.cache_enabled ? 1 : 0
count = var.enabled && local.s3_cache_enabled ? 1 : 0

statement {
sid = ""
Expand All @@ -161,7 +168,7 @@ resource "aws_iam_role_policy_attachment" "default" {
}

resource "aws_iam_role_policy_attachment" "default_cache_bucket" {
count = var.enabled && var.cache_enabled ? 1 : 0
count = var.enabled && local.s3_cache_enabled ? 1 : 0
policy_arn = join("", aws_iam_policy.default_cache_bucket.*.arn)
role = join("", aws_iam_role.default.*.id)
}
Expand All @@ -177,13 +184,10 @@ resource "aws_codebuild_project" "default" {
type = var.artifact_type
}

dynamic "cache" {
for_each = local.cache
content {
location = lookup(cache.value, "location", null)
modes = lookup(cache.value, "modes", null)
type = lookup(cache.value, "type", null)
}
cache {
type = lookup(local.cache, "type", null)
location = lookup(local.cache, "location", null)
modes = lookup(local.cache, "modes", null)
}

environment {
Expand Down
4 changes: 2 additions & 2 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ output "role_arn" {

output "cache_bucket_name" {
description = "Cache S3 bucket name"
value = var.enabled && var.cache_enabled ? join("", aws_s3_bucket.cache_bucket.*.bucket) : "UNSET"
value = var.enabled && local.s3_cache_enabled ? join("", aws_s3_bucket.cache_bucket.*.bucket) : "UNSET"
}

output "cache_bucket_arn" {
description = "Cache S3 bucket ARN"
value = var.enabled && var.cache_enabled ? join("", aws_s3_bucket.cache_bucket.*.arn) : "UNSET"
value = var.enabled && local.s3_cache_enabled ? join("", aws_s3_bucket.cache_bucket.*.arn) : "UNSET"
}

output "badge_url" {
Expand Down
22 changes: 14 additions & 8 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,27 @@ variable "enabled" {
description = "A boolean to enable/disable resource creation"
}

variable "cache_enabled" {
type = bool
default = true
description = "If cache_enabled is true, create an S3 bucket for storing codebuild cache inside"
}

variable "cache_expiration_days" {
default = 7
description = "How many days should the build cache be kept"
description = "How many days should the build cache be kept. It only works when cache_type is 'S3'"
}

variable "cache_bucket_suffix_enabled" {
type = bool
default = true
description = "The cache bucket generates a random 13 character string to generate a unique bucket name. If set to false it uses terraform-null-label's id value"
description = "The cache bucket generates a random 13 character string to generate a unique bucket name. If set to false it uses terraform-null-label's id value. It only works when cache_type is 'S3"
}

variable "cache_type" {
type = string
default = "NO_CACHE"
description = "The type of storage that will be used for the AWS CodeBuild project cache. Valid values: NO_CACHE, LOCAL, and S3. Defaults to NO_CACHE. If cache_type is S3, it will create an S3 bucket for storing codebuild cache inside"
}

variable "local_cache_modes" {
type = list(string)
default = []
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"
}

variable "badge_enabled" {
Expand Down

0 comments on commit da6ad7b

Please sign in to comment.