Skip to content

Commit e111f43

Browse files
committed
Refactor Docker image build process to use git for source file hashing and update triggers
1 parent a11828d commit e111f43

3 files changed

Lines changed: 41 additions & 14 deletions

File tree

infra/tf/.terraform.lock.hcl

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

infra/tf/bigquery_export/docker.tf

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# Get current Google Cloud access token
32
data "google_client_config" "default" {}
43

@@ -11,21 +10,26 @@ provider "docker" {
1110
}
1211
}
1312

14-
# Calculate hash of source files to determine if rebuild is needed
15-
locals {
16-
source_files = fileset(path.root, "../${var.function_name}/*")
17-
source_hash = substr(sha1(join("", [for f in local.source_files : filesha1(f)])), 0, 8)
13+
# Calculate hash of source files using git (respects .gitignore)
14+
data "external" "source_hash" {
15+
program = [
16+
"bash",
17+
"-c",
18+
"cd ../${var.function_name}/ && echo '{\"hash\":\"'$(git ls-files -s | sha1sum | cut -c1-8)'\"}'"
19+
]
1820
}
1921

2022
# Build Docker image
2123
resource "docker_image" "function_image" {
22-
name = "${var.region}-docker.pkg.dev/${var.project}/dataform/${var.function_name}:${local.source_hash}"
24+
name = "${var.region}-docker.pkg.dev/${var.project}/dataform/${var.function_name}"
2325

2426
build {
2527
context = "../${var.function_name}/"
26-
dockerfile = "Dockerfile"
2728
platform = "linux/amd64"
2829
}
30+
triggers = {
31+
source_hash = data.external.source_hash.result.hash
32+
}
2933
}
3034

3135
resource "docker_registry_image" "registry_image" {

infra/tf/dataform_service/docker.tf

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# Get current Google Cloud access token
32
data "google_client_config" "default" {}
43

@@ -11,21 +10,26 @@ provider "docker" {
1110
}
1211
}
1312

14-
# Calculate hash of source files to determine if rebuild is needed
15-
locals {
16-
source_files = fileset(path.root, "../${var.function_name}/*")
17-
source_hash = substr(sha1(join("", [for f in local.source_files : filesha1(f)])), 0, 8)
13+
# Calculate hash of source files using git (respects .gitignore)
14+
data "external" "source_hash" {
15+
program = [
16+
"bash",
17+
"-c",
18+
"cd ../${var.function_name}/ && echo '{\"hash\":\"'$(git ls-files -s | sha1sum | cut -c1-8)'\"}'"
19+
]
1820
}
1921

2022
# Build Docker image
2123
resource "docker_image" "function_image" {
22-
name = "${var.region}-docker.pkg.dev/${var.project}/dataform/${var.function_name}:${local.source_hash}"
24+
name = "${var.region}-docker.pkg.dev/${var.project}/dataform/${var.function_name}"
2325

2426
build {
2527
context = "../${var.function_name}/"
26-
dockerfile = "Dockerfile"
2728
platform = "linux/amd64"
2829
}
30+
triggers = {
31+
source_hash = data.external.source_hash.result.hash
32+
}
2933
}
3034

3135
resource "docker_registry_image" "registry_image" {

0 commit comments

Comments
 (0)