Skip to content

Commit

Permalink
Added docker image building
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Bogacz committed Oct 16, 2024
1 parent 7905486 commit 287f15d
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 36 deletions.
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ maven.install(
use_repo(maven, "maven")

bazel_dep(name = "rules_pkg", version = "1.0.1")
bazel_dep(name = "rules_oci", version = "2.0.0")
bazel_dep(name = "rules_oci", version = "2.0.1")

oci = use_extension("@rules_oci//oci:extensions.bzl", "oci")
oci.pull(
Expand Down
45 changes: 12 additions & 33 deletions MODULE.bazel.lock

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

21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,24 @@ Formatting is tested in [CI](.github/workflows/pr.yml)
**Comment**: Scalafmt is "goto" tool for formatting Scala code. Yet, included scalafmt in rules_scala is unnecessarily complicated. E.g. execution requires for all targets use `<TARGET>.format` which is hard to execute for all targets.
That's why I decided to use [standalone](https://scalameta.org/scalafmt/docs/installation.html#standalone) version of scalafmt.
Alternative can be [rules_lint](https://github.com/aspect-build/rules_lint)

## Docker

Docker images are build using rules_oci.
Each service build file has extension docker_image, which points to [oci.bzl](tools/oci.bzl)

Local image build:
```bash
bazel run //projects/service-1/src/main:local_image
```

Local image test
```bash
docker run --rm service1:latest
```

Push of image
```bash
bazel run //projects/service-1/src/main:push
```

8 changes: 7 additions & 1 deletion projects/service-1/src/main/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
scala_binary(
scala_library(
name = "service1",
srcs = glob(["scala/**/*.scala"]),
main_class = "com.org.service1.Service1App",
Expand All @@ -8,3 +8,9 @@ scala_binary(
],
deps = ["//projects/commons/init-log/src/main:init-log"],
)

docker_image(
application_name = "service1",
main_class = "com.org.service1.Service1App",
deps = [":service1"],
)
8 changes: 7 additions & 1 deletion projects/service-2/src/main/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
scala_binary(
scala_library(
name = "service2",
srcs = glob(["scala/**/*.scala"]),
main_class = "com.org.service2.Service2App",
Expand All @@ -8,3 +8,9 @@ scala_binary(
],
deps = ["//projects/commons/init-log/src/main:init-log"],
)

docker_image(
application_name = "service2",
main_class = "com.org.service2.Service2App",
deps = [":service2"],
)
Empty file added tools/BUILD.bazel
Empty file.
1 change: 1 addition & 0 deletions tools/build_rules/prelude_bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# this file allows to load by default selected rules

load("@io_bazel_rules_scala//scala:scala.bzl", "scala_test", "scala_binary", "scala_library")
load("//tools:oci.bzl", "docker_image")
31 changes: 31 additions & 0 deletions tools/oci.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
load("@io_bazel_rules_scala//scala:scala.bzl", "scala_binary")
load("@rules_oci//oci:defs.bzl", "oci_image", "oci_load", "oci_push")
load("@rules_pkg//:pkg.bzl", "pkg_tar")

def docker_image(application_name, main_class, deps = [], additional_jvm_opts = []):
return scala_binary(
name = "app",
main_class = main_class,
deps = deps,
), pkg_tar(
name = "app_layer",
srcs = [":app_deploy.jar"],
), oci_image(
name = "image",
base = "@java_temurin",
entrypoint = [
"java",
] + additional_jvm_opts + [
"-jar",
"/app_deploy.jar",
],
tars = [":app_layer"],
), oci_load(
name = "local_image",
image = ":image",
repo_tags = ["{app}:latest".format(app = application_name)],
), oci_push(
name = "push",
image = ":image",
repository = "docker.io/" + application_name,
)

0 comments on commit 287f15d

Please sign in to comment.