Skip to content

Commit da6ad7b

Browse files
drselump14maximmiactions-bot
authored
Add option to enable LOCAL_CACHE (#50)
* 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]>
1 parent c98a470 commit da6ad7b

File tree

8 files changed

+62
-42
lines changed

8 files changed

+62
-42
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,16 @@ Available targets:
156156
| 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 |
157157
| 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 |
158158
| buildspec | Optional buildspec declaration to use for building the project | string | `` | no |
159-
| 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 |
160-
| cache_enabled | If cache_enabled is true, create an S3 bucket for storing codebuild cache inside | bool | `true` | no |
161-
| cache_expiration_days | How many days should the build cache be kept | string | `7` | no |
159+
| 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 |
160+
| cache_expiration_days | How many days should the build cache be kept. It only works when cache_type is 'S3' | string | `7` | no |
161+
| 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 |
162162
| delimiter | Delimiter to be used between `name`, `namespace`, `stage`, etc. | string | `-` | no |
163163
| enabled | A boolean to enable/disable resource creation | bool | `true` | no |
164164
| 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 |
165165
| github_token | (Optional) GitHub auth token environment variable (`GITHUB_TOKEN`) | string | `` | no |
166166
| 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 |
167167
| 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 |
168+
| 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 |
168169
| name | Solution name, e.g. 'app' or 'jenkins' | string | - | yes |
169170
| namespace | Namespace, which could be your organization name, e.g. 'eg' or 'cp' | string | `` | no |
170171
| 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 |

docs/terraform.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@
1111
| 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 |
1212
| 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 |
1313
| buildspec | Optional buildspec declaration to use for building the project | string | `` | no |
14-
| 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 |
15-
| cache_enabled | If cache_enabled is true, create an S3 bucket for storing codebuild cache inside | bool | `true` | no |
16-
| cache_expiration_days | How many days should the build cache be kept | string | `7` | no |
14+
| 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 |
15+
| cache_expiration_days | How many days should the build cache be kept. It only works when cache_type is 'S3' | string | `7` | no |
16+
| 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 |
1717
| delimiter | Delimiter to be used between `name`, `namespace`, `stage`, etc. | string | `-` | no |
1818
| enabled | A boolean to enable/disable resource creation | bool | `true` | no |
1919
| 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 |
2020
| github_token | (Optional) GitHub auth token environment variable (`GITHUB_TOKEN`) | string | `` | no |
2121
| 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 |
2222
| 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 |
23+
| 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 |
2324
| name | Solution name, e.g. 'app' or 'jenkins' | string | - | yes |
2425
| namespace | Namespace, which could be your organization name, e.g. 'eg' or 'cp' | string | `` | no |
2526
| 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 |

examples/complete/fixtures.us-west-1.tfvars

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ stage = "test"
66

77
name = "cedebuild-test"
88

9-
cache_enabled = true
10-
119
cache_bucket_suffix_enabled = false
1210

1311
environment_variables = [
@@ -25,3 +23,7 @@ environment_variables = [
2523

2624
}
2725
]
26+
27+
cache_expiration_days = 7
28+
29+
cache_type = "S3"

examples/complete/main.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ module "codebuild" {
77
namespace = var.namespace
88
stage = var.stage
99
name = var.name
10-
cache_enabled = var.cache_enabled
1110
cache_bucket_suffix_enabled = var.cache_bucket_suffix_enabled
1211
environment_variables = var.environment_variables
12+
cache_expiration_days = var.cache_expiration_days
13+
cache_type = var.cache_type
1314
}

examples/complete/variables.tf

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,17 @@ variable "environment_variables" {
3434
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"
3535
}
3636

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

4242
variable "cache_bucket_suffix_enabled" {
4343
type = bool
4444
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"
4545
}
46+
47+
variable "cache_type" {
48+
type = string
49+
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"
50+
}

main.tf

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module "label" {
1515
}
1616

1717
resource "aws_s3_bucket" "cache_bucket" {
18-
count = var.enabled && var.cache_enabled ? 1 : 0
18+
count = var.enabled && local.s3_cache_enabled ? 1 : 0
1919
bucket = local.cache_bucket_name_normalised
2020
acl = "private"
2121
force_destroy = true
@@ -54,21 +54,28 @@ locals {
5454
min(length(local.cache_bucket_name), 63),
5555
)
5656

57+
s3_cache_enabled = var.cache_type == "S3"
58+
5759
## This is the magic where a map of a list of maps is generated
5860
## and used to conditionally add the cache bucket option to the
5961
## aws_codebuild_project
60-
cache_def = {
61-
"true" = [
62-
{
63-
type = "S3"
64-
location = var.enabled && var.cache_enabled ? join("", aws_s3_bucket.cache_bucket.*.bucket) : "none"
65-
}
66-
]
67-
"false" = []
62+
cache_options = {
63+
"S3" = {
64+
type = "S3"
65+
location = var.enabled && local.s3_cache_enabled ? join("", aws_s3_bucket.cache_bucket.*.bucket) : "none"
66+
67+
},
68+
"LOCAL" = {
69+
type = "LOCAL"
70+
modes = var.local_cache_modes
71+
},
72+
"NO_CACHE" = {
73+
type = "NO_CACHE"
74+
}
6875
}
6976

7077
# Final Map Selected from above
71-
cache = local.cache_def[var.cache_enabled ? "true" : "false"]
78+
cache = local.cache_options[var.cache_type]
7279
}
7380

7481
resource "aws_iam_role" "default" {
@@ -102,7 +109,7 @@ resource "aws_iam_policy" "default" {
102109
}
103110

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

138145
data "aws_iam_policy_document" "permissions_cache_bucket" {
139-
count = var.enabled && var.cache_enabled ? 1 : 0
146+
count = var.enabled && local.s3_cache_enabled ? 1 : 0
140147

141148
statement {
142149
sid = ""
@@ -161,7 +168,7 @@ resource "aws_iam_role_policy_attachment" "default" {
161168
}
162169

163170
resource "aws_iam_role_policy_attachment" "default_cache_bucket" {
164-
count = var.enabled && var.cache_enabled ? 1 : 0
171+
count = var.enabled && local.s3_cache_enabled ? 1 : 0
165172
policy_arn = join("", aws_iam_policy.default_cache_bucket.*.arn)
166173
role = join("", aws_iam_role.default.*.id)
167174
}
@@ -177,13 +184,10 @@ resource "aws_codebuild_project" "default" {
177184
type = var.artifact_type
178185
}
179186

180-
dynamic "cache" {
181-
for_each = local.cache
182-
content {
183-
location = lookup(cache.value, "location", null)
184-
modes = lookup(cache.value, "modes", null)
185-
type = lookup(cache.value, "type", null)
186-
}
187+
cache {
188+
type = lookup(local.cache, "type", null)
189+
location = lookup(local.cache, "location", null)
190+
modes = lookup(local.cache, "modes", null)
187191
}
188192

189193
environment {

outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ output "role_arn" {
2020

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

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

3131
output "badge_url" {

variables.tf

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,27 @@ variable "enabled" {
3737
description = "A boolean to enable/disable resource creation"
3838
}
3939

40-
variable "cache_enabled" {
41-
type = bool
42-
default = true
43-
description = "If cache_enabled is true, create an S3 bucket for storing codebuild cache inside"
44-
}
45-
4640
variable "cache_expiration_days" {
4741
default = 7
48-
description = "How many days should the build cache be kept"
42+
description = "How many days should the build cache be kept. It only works when cache_type is 'S3'"
4943
}
5044

5145
variable "cache_bucket_suffix_enabled" {
5246
type = bool
5347
default = true
54-
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"
48+
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"
49+
}
50+
51+
variable "cache_type" {
52+
type = string
53+
default = "NO_CACHE"
54+
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"
55+
}
56+
57+
variable "local_cache_modes" {
58+
type = list(string)
59+
default = []
60+
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"
5561
}
5662

5763
variable "badge_enabled" {

0 commit comments

Comments
 (0)