Refactor BUILD for Temporal server image.

1. Renames the BUILD file to follow this patter: project.BUILD
2. Moves containerized build target into temporal.BUILD so they stay
   closely
3. Now the Temporal image contains binaries and artifacts under
   /etc/temporal, including schemas and configs. Temporal needs to
   run in a certain folder structure to load config files correctly.

Bug: b/311455474
Change-Id: I0de68ba212f909fe130706e005e42bde0595ad54
Reviewed-on: https://skia-review.googlesource.com/c/buildbot/+/789456
Reviewed-by: Leandro Lovisolo <lovisolo@google.com>
Commit-Queue: Hao Wu <haowoo@google.com>
diff --git a/WORKSPACE b/WORKSPACE
index 66294cc..832d3df 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -178,7 +178,7 @@
 
 http_archive(
     name = "com_github_temporal",
-    build_file = "//temporal:BUILD.github",
+    build_file = "//temporal:temporal.BUILD",
     sha256 = "8ab8cbe6092877904df40cbf6640936a9d68421520b877faa39e6e6775a95bcb",
     strip_prefix = "./temporal-1.22.3",
     url = "https://github.com/temporalio/temporal/archive/refs/tags/v1.22.3.tar.gz",
diff --git a/temporal/BUILD.bazel b/temporal/BUILD.bazel
index fef0349..5396963 100644
--- a/temporal/BUILD.bazel
+++ b/temporal/BUILD.bazel
@@ -1,57 +1,46 @@
 load("@io_bazel_rules_docker//container:container.bzl", "container_image")
-load("@io_bazel_rules_docker//docker/util:run.bzl", "container_run_and_extract")
-
-# Build Temporal from the source
-container_run_and_extract(
-    name = "temporal-build",
-    commands = [
-        "export CGO_ENABLED=0 GOOS=linux GOBIN=/tmp/temporal",
-        "mkdir -p /tmp/temporal /temporal/config/dynamicconfig",
-        "go install go.temporal.io/server/cmd/server@v1.22.2",
-        "go install go.temporal.io/server/cmd/tools/sql@v1.22.2",
-        "wget -P /temporal/config/ https://github.com/temporalio/temporal/raw/v1.22.2/docker/config_template.yaml",
-        "wget -O - https://github.com/temporalio/cli/releases/download/v0.10.7/temporal_cli_0.10.7_linux_amd64.tar.gz | tar xzf - -C /temporal",
-        "wget -O - https://github.com/jwilder/dockerize/releases/download/v0.7.0/dockerize-linux-amd64-v0.7.0.tar.gz | tar xzf - -C /temporal",
-        "cp /tmp/temporal/server /temporal/temporal-server",
-        "cp /tmp/temporal/sql /temporal/temporal-sql-tool",
-    ],
-    extract_file = "/temporal",
-    image = "@golang//image",
-    tags = [
-        "manual",  # Exclusion from presubmit and RBE as it requires docker.
-        "no-remote",
-    ],
-)
 
 container_image(
     name = "temporal-admin",
     base = "@basealpine//image",
     entrypoint = ["/bin/sh"],
-    files = [
-        "temporal-build/temporal",
-    ],
     tags = [
         "manual",  # Exclusion from presubmit and RBE as it requires docker.
         "no-remote",
     ],
     tars = [
-        "@com_github_temporal//:schemas",
+        "@com_github_temporal//:temporal",
     ],
-    workdir = "/",
+    workdir = "/etc/temporal",
 )
 
 # Docker image with Temporal server
 container_image(
     name = "temporal-server",
     base = "@basealpine//image",
+    # Temporal will need to generate yml file under config folder using
+    # environment vars. It needs root and access to the folder config.
+    empty_dirs = [
+        "/etc/temporal/config",
+    ],
+    empty_files = [
+        # The default dynamic config placeholder with empty content.
+        "/etc/temporal/config/dynamicconfig/docker.yaml",
+    ],
     entrypoint = ["/entrypoint.sh"],
+    env = {
+        "DYNAMIC_CONFIG_FILE_PATH": "/etc/temporal/config/dynamicconfig/docker.yaml",
+    },
     files = [
-        "temporal-build/temporal",
-        ":docker/entrypoint.sh",
+        "docker/entrypoint.sh",
     ],
     tags = [
         "manual",  # Exclusion from presubmit and RBE as it requires docker.
         "no-remote",
     ],
-    workdir = "/",
+    tars = [
+        "@com_github_temporal//:temporal",
+    ],
+    user = "root",
+    workdir = "/etc/temporal",
 )
diff --git a/temporal/BUILD.github b/temporal/BUILD.github
deleted file mode 100644
index 9516753..0000000
--- a/temporal/BUILD.github
+++ /dev/null
@@ -1,12 +0,0 @@
-load("@rules_pkg//:pkg.bzl", "pkg_tar")
-
-pkg_tar(
-    name = "schemas",
-    srcs = glob([
-        "schema/postgresql/v96/**/*.sql",
-        "schema/postgresql/v96/**/*.json",
-    ]),
-    mode = "0755",
-    strip_prefix = ".",  # Preserve the folder structure.
-    visibility = ["//visibility:public"],
-)
diff --git a/temporal/docker/entrypoint.sh b/temporal/docker/entrypoint.sh
index 300abc7..cf7ef98 100755
--- a/temporal/docker/entrypoint.sh
+++ b/temporal/docker/entrypoint.sh
@@ -21,6 +21,6 @@
     fi
 fi
 
-/temporal/dockerize -template /temporal/config/config_template.yaml:/tmp/docker.yaml
+/etc/temporal/dockerize -template /etc/config_template.yaml:/etc/temporal/config/docker.yaml
 
-exec /temporal/temporal-server --config /tmp -env docker start
+exec /etc/temporal/temporal-server --env docker start
diff --git a/temporal/temporal.BUILD b/temporal/temporal.BUILD
new file mode 100644
index 0000000..81817c8
--- /dev/null
+++ b/temporal/temporal.BUILD
@@ -0,0 +1,63 @@
+# BUILD file for Temporal sources in the github
+# https://github.com/temporalio/temporal
+
+load("@io_bazel_rules_docker//container:container.bzl", "container_image")
+load("@io_bazel_rules_docker//docker/util:run.bzl", "container_run_and_extract")
+load("@rules_pkg//:pkg.bzl", "pkg_tar")
+
+pkg_tar(
+    name = "sources",
+    srcs = glob(["**/*"]),
+    mode = "0755",
+    package_dir = "/temporal-srcs",
+    strip_prefix = ".",  # Preserve the folder structure.
+)
+
+container_image(
+    name = "temporal-srcs",
+    base = "@golang//image",
+    tars = [
+        ":sources",
+    ],
+    workdir = "/temporal-srcs",
+)
+
+container_run_and_extract(
+    name = "temporal-bins",
+    commands = [
+        "export CGO_ENABLED=0 GOOS=linux",
+        "mkdir -p /temporal",
+        "make bins",
+        "mv temporal-server temporal-sql-tool tdbg /temporal/.",
+        "wget -O - https://github.com/temporalio/cli/releases/download/v0.10.7/temporal_cli_0.10.7_linux_amd64.tar.gz | tar xzf - -C /temporal",
+        "wget -O - https://github.com/jwilder/dockerize/releases/download/v0.7.0/dockerize-linux-amd64-v0.7.0.tar.gz | tar xzf - -C /temporal",
+    ],
+    extract_file = "/temporal",
+    image = ":temporal-srcs.tar",
+)
+
+# Pack schema files into proper foler with proper permissions
+pkg_tar(
+    name = "schemas",
+    srcs = glob([
+        "schema/postgresql/v96/**/*.sql",
+        "schema/postgresql/v96/**/*.json",
+    ]),
+    mode = "0755",
+    package_dir = "/temporal",
+    strip_prefix = ".",  # Preserve the folder structure.
+)
+
+# The built temporal binaries and artifacts
+pkg_tar(
+    name = "temporal",
+    srcs = [
+        ":docker/config_template.yaml",
+        ":temporal-bins/temporal",
+    ],
+    package_dir = "/etc",
+    visibility = ["//visibility:public"],
+    deps = [
+        ":schemas",
+    ],
+)