-
Notifications
You must be signed in to change notification settings - Fork 18
/
docker-bake.hcl
133 lines (126 loc) · 4.28 KB
/
docker-bake.hcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# docker-bake.hcl
/*
* Extract platform (arch) from current default host
* Utilizes built-in var BAKE_LOCAL_PLATFORM
* https://docs.docker.com/build/bake/file-definition/#built-in-variables
*
*/
function hostArch {
params = []
result = element(split("/", BAKE_LOCAL_PLATFORM), 1)
}
/*
* Docker Tagging variables
*
* Used to manage container images, and it's layers and cache
* Best effort Tags for local development vs Github Actions build/publish
* Local development: "latest"
* Github Actions: "$(git rev-parse --short HEAD)" -> "abc1234"
*/
variable "NODE_VERSION" {}
variable "AWS_ACCOUNT_ID" {}
variable "PRIVATE_REGISTRY" { default = "${AWS_ACCOUNT_ID}.dkr.ecr.us-west-2.amazonaws.com" }
variable "REFERENCE" { default = "latest" }
variable "GITHUB_ACTIONS" { default = "false" }
variable "PROJECT_NAME" { default = "heartofthevalley" }
variable "DOCKER_TAG" { default = notequal("false",GITHUB_ACTIONS) ? "${REFERENCE}": "latest" }
function "dockerTag" {
params = [image, tag, prefix]
result = concat(
notequal(prefix, "") ?
["${PRIVATE_REGISTRY}/${image}:${prefix}-${tag}"]:
["${PRIVATE_REGISTRY}/${image}:${tag}"],
)
}
/*
* Cache Layers in Github Actions
*
* Utilize S3 cache if AWS cli credentials are present
* More information
* https://docs.docker.com/build/cache/backends/s3/
*/
variable "AWS_ACCESS_KEY_ID" { default = "false" }
variable "AWS_SECRET_ACCESS_KEY" { default = "false" }
variable "DOCKER_S3_CACHE" {
default = "type=s3,mode=max,region=us-west-1,bucket=opensourcesanjose-cache"
}
function "dockerS3Cache" {
params = [cacheid]
result = and(
notequal("false",AWS_ACCESS_KEY_ID),
notequal("false",AWS_SECRET_ACCESS_KEY)
) ? join(",",
[
"${DOCKER_S3_CACHE}",
"name=${cacheid}",
]
): ""
}
variable "OUTPUT" { # output to docker for local development rather than GHCR
default = notequal("false",GITHUB_ACTIONS) ? "type=registry": "type=docker"
}
variable "CACHE_ID" {
default = "docker-${PROJECT_NAME}-${hostArch()}"
}
target "_common" {
output = ["${OUTPUT}"]
}
/*
* Virtual Base Targets
*
* This section defines virtual base targets, which are shared across the
* different dependent targets.
*/
// This builds the frontend bundle and serves using nginx
variable "API_BASE_URL" { default = "/v1/heartofvalley" }
variable "PUBLIC_URL" { default = "http://localhost" }
variable "REACT_APP_API_SERVER" { default = "http://localhost" }
target "build" {
dockerfile = "docker/frontend/Dockerfile"
context = "./"
target = "nginx-build"
args = {
NODE_VERSION = "${NODE_VERSION}"
PUBLIC_URL = "${PUBLIC_URL}"
REACT_APP_API_SERVER = "${REACT_APP_API_SERVER}"
REACT_APP_API_BASE_URL = "${API_BASE_URL}"
}
inherits = ["_common"]
tags = dockerTag("${PROJECT_NAME}", "${DOCKER_TAG}", "frontend")
cache-from = [dockerS3Cache("${CACHE_ID}-frontend")]
cache-to = [notequal("false",GITHUB_ACTIONS) ? dockerS3Cache("${CACHE_ID}-frontend"): ""]
}
target "frontend" {
dockerfile = "docker/frontend/Dockerfile"
context = "./"
target = "dev"
args = {
NODE_VERSION = "${NODE_VERSION}"
REACT_APP_API_SERVER = "${REACT_APP_API_SERVER}"
REACT_APP_API_BASE_URL = "${API_BASE_URL}"
}
inherits = ["_common"]
tags = dockerTag("${PROJECT_NAME}", "${DOCKER_TAG}", "frontend")
cache-from = [dockerS3Cache("${CACHE_ID}-frontend")]
cache-to = [notequal("false",GITHUB_ACTIONS) ? dockerS3Cache("${CACHE_ID}-frontend"): ""]
}
target "backend" {
dockerfile = "docker/backend/Dockerfile"
context = "./"
args = {
NODE_VERSION = "${NODE_VERSION}"
API_BASE_URL = "${API_BASE_URL}"
}
inherits = ["_common"]
tags = dockerTag("${PROJECT_NAME}", "${DOCKER_TAG}", "backend")
cache-from = [dockerS3Cache("${CACHE_ID}-backend")]
cache-to = [notequal("false",GITHUB_ACTIONS) ? dockerS3Cache("${CACHE_ID}-backend"): ""]
}
/*
* Default Target(s) to build
* https://docs.docker.com/build/bake/file-definition/#default-targetgroup
* "docker buildx bake" == "docker buildx bake default"
*/
group "default" {
targets = ["build", "backend"]
}