Skip to content

Commit

Permalink
Build docker images with terraform.
Browse files Browse the repository at this point in the history
  • Loading branch information
anybodys committed Jun 22, 2024
1 parent b64b265 commit 2487c66
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 29 deletions.
44 changes: 22 additions & 22 deletions infra/app/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions infra/app/docker.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
locals {
docker_hostname = "${var.region}-docker.pkg.dev"
image_root = "${local.docker_hostname}/${var.project}/${var.project}"
}

provider "docker" {
registry_auth {
address = local.docker_hostname
}
}


resource "docker_image" "app" {
for_each = var.app_versions
name = "${local.image_root}/${each.key}"

build {
# The application's directory.
context = "${path.cwd}/../../${each.key}"
tag = ["${local.image_root}/${each.key}:${each.value}"]
}

triggers = {
# Trigger a build if this value has changed.
version_tag = each.value
}
}

resource "docker_registry_image" "app" {
for_each = docker_image.app
name = tolist(docker_image.app[each.key].build)[0].tag[0]
keep_remotely = true
}
12 changes: 6 additions & 6 deletions infra/app/main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "3.0.2"
}
google = {
source = "hashicorp/google"
version = "5.32.0"
Expand All @@ -15,7 +19,6 @@ terraform {
}
}


provider "google" {
project = var.project
region = var.region
Expand All @@ -29,10 +32,7 @@ data "google_client_config" "current" {
}

locals {
image_base = "${var.region}-docker.pkg.dev/${var.project}/${var.project}/"
client_tag = var.app_versions["client"]
voting_tag = var.app_versions["votingapi"]
storageapi_tag = var.app_versions["storageapi"]
storageapi_image = tolist(docker_image.app["storageapi"].build)[0].tag[0]
}

################################################################
Expand All @@ -59,7 +59,7 @@ resource "google_cloud_run_v2_service" "storageapi" {
}

containers {
image = "${local.image_base}storageapi:${local.storageapi_tag}"
image = local.storageapi_image

ports {
container_port = 8000
Expand Down
2 changes: 1 addition & 1 deletion infra/app/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ variable "app_versions" {
client : "0.1.0",
votingapi : "0.1.1",
painterapi : "0.1.0",
storageapi : "0.0.0kmddbtestmigrate3",
storageapi : "0.0.1",
}

}
Expand Down

0 comments on commit 2487c66

Please sign in to comment.