Skip to content

Commit 09dae53

Browse files
committed
docker builds
1 parent f284924 commit 09dae53

File tree

13 files changed

+234
-187
lines changed

13 files changed

+234
-187
lines changed

src/.dockerignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
node_modules
2+
npm-debug.log
3+
.git
4+
.gitignore
5+
.env
6+
.nyc_output
7+
coverage
8+
*.md
9+
.DS_Store
10+
cloudbuild.yaml
11+
__tests__
12+
coverage

src/Dockerfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM node:22-slim
2+
3+
# Set the working directory
4+
WORKDIR /app
5+
6+
# Copy package files first for better layer caching
7+
COPY package*.json ./
8+
9+
# Install dependencies (this layer will be cached unless package files change)
10+
RUN npm ci --only=production --quiet --no-fund --no-audit && npm cache clean --force
11+
12+
ENV DATABASE=tech-report-api-prod
13+
14+
# Copy source code
15+
COPY . .
16+
17+
CMD ["node", "index.js"]

src/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
"scripts": {
1111
"start": "export DATABASE=tech-report-api-prod && node index.js",
1212
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
13-
"test:live": "bash ../test-api.sh"
13+
"test:live": "bash ../test-api.sh",
14+
"build": "docker build -t tech-report-api .",
15+
"run": "docker run -p 8080:8080 tech-report-api"
1416
},
1517
"dependencies": {
1618
"@google-cloud/firestore": "7.11.2",

terraform/dev/local.auto.tfvars.template

Lines changed: 0 additions & 4 deletions
This file was deleted.

terraform/dev/main.tf

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ terraform {
33
bucket = "tfstate-httparchive"
44
prefix = "tech-report-apis/dev"
55
}
6+
7+
required_providers {
8+
docker = {
9+
source = "kreuzwerker/docker"
10+
version = ">= 3.6.2"
11+
}
12+
}
613
}
714

815
provider "google" {
@@ -11,23 +18,30 @@ provider "google" {
1118
request_timeout = "60m"
1219
}
1320

14-
resource "google_api_gateway_api" "api" {
15-
provider = google-beta
16-
api_id = "reports-api-dev"
17-
display_name = "Reports API Gateway DEV"
18-
project = var.project
21+
provider "google-beta" {
22+
project = var.project
23+
region = var.region
24+
}
25+
26+
# Get current Google Cloud access token
27+
data "google_client_config" "default" {}
28+
29+
# Configure Docker provider with Artifact Registry authentication
30+
provider "docker" {
31+
registry_auth {
32+
address = "${var.region}-docker.pkg.dev"
33+
username = "oauth2accesstoken"
34+
password = data.google_client_config.default.access_token
35+
}
1936
}
2037

21-
resource "google_api_gateway_api_config" "api_config" {
22-
provider = google-beta
23-
api = google_api_gateway_api.api.api_id
24-
api_config_id_prefix = "reports-api-config-dev"
25-
project = var.project
26-
display_name = "Reports API Config DEV"
27-
openapi_documents {
28-
document {
29-
path = "spec.yaml"
30-
contents = base64encode(<<-EOF
38+
module "gateway" {
39+
source = "./../modules/api-gateway"
40+
project = var.project
41+
environment = var.environment
42+
region = var.region
43+
service_account_email = var.google_service_account_api_gateway
44+
spec_yaml = <<EOF
3145
swagger: "2.0"
3246
info:
3347
title: reports_api_config_dev
@@ -113,48 +127,36 @@ paths:
113127
200:
114128
description: String
115129
EOF
116-
)
117-
}
118-
}
119-
gateway_config {
120-
backend_config {
121-
google_service_account = var.google_service_account_api_gateway
122-
}
123-
}
124-
}
125-
126-
resource "google_api_gateway_gateway" "gateway" {
127-
provider = google-beta
128-
project = var.project
129-
region = var.region
130-
api_config = google_api_gateway_api_config.api_config.id
131-
gateway_id = "reports-dev"
132-
display_name = "Reports API Gateway DEV"
133-
labels = {
134-
owner = "tech_report_api"
135-
environment = var.environment
136-
}
137-
depends_on = [google_api_gateway_api_config.api_config]
138-
lifecycle {
139-
replace_triggered_by = [
140-
google_api_gateway_api_config.api_config
141-
]
142-
}
143130
}
144131

145132
module "endpoints" {
146-
source = "./../modules/run-service"
147-
entry_point = "app"
148-
project = var.project
149-
environment = var.environment
150-
source_directory = "../../src"
151-
function_name = "tech-report-api"
152-
region = var.region
153-
service_account_email = var.google_service_account_cloud_functions
154-
service_account_api_gateway = var.google_service_account_api_gateway
155-
min_instances = var.min_instances
133+
source = "./../modules/run-service"
134+
entry_point = "app"
135+
project = var.project
136+
environment = var.environment
137+
source_directory = "../../src"
138+
function_name = "tech-report-api"
139+
region = var.region
140+
service_account_email = var.google_service_account_cloud_functions
141+
service_account_api_gateway = var.google_service_account_api_gateway
142+
min_instances = var.min_instances
156143
environment_variables = {
157144
"PROJECT" = var.project
158145
"DATABASE" = var.project_database
159146
}
160147
}
148+
149+
moved {
150+
from = google_api_gateway_api.api
151+
to = module.gateway.google_api_gateway_api.api
152+
}
153+
154+
moved {
155+
from = google_api_gateway_api_config.api_config
156+
to = module.gateway.google_api_gateway_api_config.api_config
157+
}
158+
159+
moved {
160+
from = google_api_gateway_gateway.gateway
161+
to = module.gateway.google_api_gateway_gateway.gateway
162+
}

terraform/modules/api-gateway/main.tf

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
1-
######################################
2-
# API Gateway
3-
######################################
41
# Used to expose Internal resources to external sources, such as web applications
52
# See https://cloud.google.com/api-gateway/docs for more information
3+
4+
terraform {
5+
required_providers {
6+
docker = {
7+
source = "hashicorp/google-beta"
8+
version = ">= 6.38.0"
9+
}
10+
}
11+
}
12+
613
# The API used by the Gateway
714
resource "google_api_gateway_api" "api" {
8-
provider = google-beta # API Gateway is still in beta
9-
api_id = "api-gw-${var.environment}"
10-
display_name = "The ${var.environment} API Gateway"
15+
provider = google-beta
16+
api_id = "reports-api-${var.environment}"
17+
display_name = "Reports API Gateway ${var.environment}"
1118
project = var.project
1219
}
20+
1321
# A Configuration, consisting of an OpenAPI specification
1422
resource "google_api_gateway_api_config" "api_config" {
15-
provider = google-beta # API Gateway is still in beta
23+
provider = google-beta
1624
api = google_api_gateway_api.api.api_id
17-
api_config_id_prefix = "api"
25+
api_config_id_prefix = "reports-api-config-${var.environment}"
1826
project = var.project
19-
display_name = "The ${var.environment} Config"
27+
display_name = "Reports API Config ${var.environment}"
2028
openapi_documents {
2129
document {
22-
path = "spec.yaml" # File name is simply sugar to show on GCP
23-
contents = filebase64("spec.yaml") # This is based on *who* is call the module!
30+
path = "spec.yaml"
31+
contents = base64encode(var.spec_yaml)
2432
}
2533
}
2634
gateway_config {
@@ -35,13 +43,13 @@ resource "google_api_gateway_gateway" "gateway" {
3543
project = var.project
3644
region = var.region
3745
api_config = google_api_gateway_api_config.api_config.id
38-
gateway_id = "${var.environment}-gw"
39-
display_name = "${var.environment} Api Gateway"
46+
gateway_id = "reports-${var.environment}"
47+
display_name = "Reports API Gateway ${var.environment}"
4048
labels = {
4149
owner = "tech_report_api"
4250
environment = var.environment
4351
}
44-
depends_on = [google_api_gateway_api_config.api_config]
52+
4553
lifecycle {
4654
replace_triggered_by = [
4755
google_api_gateway_api_config.api_config

terraform/modules/api-gateway/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
variable "environment" {
22
description = "The 'Environment' that is being created/deployed. Applied as a suffix to many resources."
33
type = string
4+
default = "dev"
45
}
56
variable "project" {
67
description = "The ID of the project in which the resource belongs. If it is not provided, the provider project is used."
@@ -14,3 +15,7 @@ variable "service_account_email" {
1415
description = "Email of the service account associated with and to run the API Gateway"
1516
type = string
1617
}
18+
variable "spec_yaml" {
19+
description = "The OpenAPI specification for the API Gateway"
20+
type = string
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Calculate hash of source files to determine if rebuild is needed
2+
locals {
3+
source_files = fileset(path.root, "../${var.function_name}/*")
4+
source_hash = substr(sha1(join("", [for f in local.source_files : filesha1(f)])), 0, 8)
5+
}
6+
7+
# Build Docker image
8+
resource "docker_image" "function_image" {
9+
name = "${var.region}-docker.pkg.dev/${var.project}/dataform/${var.function_name}:${local.source_hash}"
10+
11+
build {
12+
context = "../${var.function_name}/"
13+
dockerfile = "Dockerfile"
14+
platform = "linux/amd64"
15+
}
16+
}
17+
18+
resource "docker_registry_image" "registry_image" {
19+
name = docker_image.function_image.name
20+
}

0 commit comments

Comments
 (0)