diff --git a/MODULE.bazel b/MODULE.bazel index 1839120..e44d8d6 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -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( diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock index a34153f..3293061 100644 --- a/MODULE.bazel.lock +++ b/MODULE.bazel.lock @@ -66,8 +66,8 @@ "https://bcr.bazel.build/modules/rules_license/0.0.7/MODULE.bazel": "088fbeb0b6a419005b89cf93fe62d9517c0a2b8bb56af3244af65ecfe37e7d5d", "https://bcr.bazel.build/modules/rules_license/1.0.0/MODULE.bazel": "a7fda60eefdf3d8c827262ba499957e4df06f659330bbe6cdbdb975b768bb65c", "https://bcr.bazel.build/modules/rules_license/1.0.0/source.json": "a52c89e54cc311196e478f8382df91c15f7a2bfdf4c6cd0e2675cc2ff0b56efb", - "https://bcr.bazel.build/modules/rules_oci/2.0.0/MODULE.bazel": "5d5cf1932238b009f874d5a9f214bbedf5d8cec8e5028e083f1147726876572f", - "https://bcr.bazel.build/modules/rules_oci/2.0.0/source.json": "ea89dd54d1f473a6fb1a6c2d0660b741ee832d5141ac0b9c90d5c8fc9b5e3bb3", + "https://bcr.bazel.build/modules/rules_oci/2.0.1/MODULE.bazel": "b1984eceba83906786f99e7e8273754458e1c5f3d39e3b0b28f03d12be9d4099", + "https://bcr.bazel.build/modules/rules_oci/2.0.1/source.json": "70f86dc00a62cde2103e8c50b8fd1f120252f2cb735ecce8aba3842ff1b5875f", "https://bcr.bazel.build/modules/rules_pkg/0.7.0/MODULE.bazel": "df99f03fc7934a4737122518bb87e667e62d780b610910f0447665a7e2be62dc", "https://bcr.bazel.build/modules/rules_pkg/1.0.1/MODULE.bazel": "5b1df97dbc29623bccdf2b0dcd0f5cb08e2f2c9050aab1092fd39a41e82686ff", "https://bcr.bazel.build/modules/rules_pkg/1.0.1/source.json": "bd82e5d7b9ce2d31e380dd9f50c111d678c3bdaca190cb76b0e1c71b05e1ba8a", @@ -123,7 +123,7 @@ "@@aspect_bazel_lib~//lib:extensions.bzl%toolchains": { "general": { "bzlTransitiveDigest": "nzNCCKeGDuuaB6uzXddNTVOHkFu9zZDzQ37VqiZh/Sg=", - "usagesDigest": "0L3O0fnXBwLQd151C6C1A0UuxYFxwyfi2wYr3ZnjAlI=", + "usagesDigest": "RUf1GGO0W8WzbE2neWx+GgcsvtCJDJ0ZWrETcFYbWXU=", "recordedFileInputs": {}, "recordedDirentsInputs": {}, "envVariables": {}, @@ -675,8 +675,8 @@ }, "@@rules_oci~//oci:extensions.bzl%oci": { "general": { - "bzlTransitiveDigest": "0CbR1INkcqET0EwROFLlWop2pQBo0iEg4iIqPZizdJw=", - "usagesDigest": "+xQmZmI0E88hm8MaHPKzvlDF5NV03Cjks9xYouW+U6k=", + "bzlTransitiveDigest": "g/x86AB7byc7Wjh1yTFhPNYXMFWG3qw6Chu9PYzC6ic=", + "usagesDigest": "niFD76nJEwReDhXqkWYnPDzQ4cIhu+ZFVs8iOGo5RcE=", "recordedFileInputs": {}, "recordedDirentsInputs": {}, "envVariables": {}, @@ -754,13 +754,6 @@ "toolchain": "@oci_regctl_{platform}//:regctl_toolchain" } }, - "oci_regctl_windows_armv6": { - "bzlFile": "@@rules_oci~//oci:repositories.bzl", - "ruleClassName": "regctl_repositories", - "attributes": { - "platform": "windows_armv6" - } - }, "oci_crane_linux_amd64": { "bzlFile": "@@rules_oci~//oci:repositories.bzl", "ruleClassName": "crane_repositories", @@ -894,13 +887,6 @@ "crane_version": "v0.18.0" } }, - "oci_regctl_linux_arm64": { - "bzlFile": "@@rules_oci~//oci:repositories.bzl", - "ruleClassName": "regctl_repositories", - "attributes": { - "platform": "linux_arm64" - } - }, "oci_crane_linux_s390x": { "bzlFile": "@@rules_oci~//oci:repositories.bzl", "ruleClassName": "crane_repositories", @@ -909,6 +895,13 @@ "crane_version": "v0.18.0" } }, + "oci_regctl_linux_arm64": { + "bzlFile": "@@rules_oci~//oci:repositories.bzl", + "ruleClassName": "regctl_repositories", + "attributes": { + "platform": "linux_arm64" + } + }, "zstd_linux_amd64": { "bzlFile": "@@aspect_bazel_lib~//lib/private:zstd_toolchain.bzl", "ruleClassName": "zstd_binary_repo", @@ -1064,13 +1057,6 @@ "version": "1.7" } }, - "oci_regctl_linux_i386": { - "bzlFile": "@@rules_oci~//oci:repositories.bzl", - "ruleClassName": "regctl_repositories", - "attributes": { - "platform": "linux_i386" - } - }, "jq_toolchains": { "bzlFile": "@@aspect_bazel_lib~//lib/private:jq_toolchain.bzl", "ruleClassName": "jq_toolchains_repo", @@ -1085,13 +1071,6 @@ "user_repository_name": "copy_to_directory" } }, - "oci_regctl_linux_armv6": { - "bzlFile": "@@rules_oci~//oci:repositories.bzl", - "ruleClassName": "regctl_repositories", - "attributes": { - "platform": "linux_armv6" - } - }, "bazel_features_globals": { "bzlFile": "@@bazel_features~//private:globals_repo.bzl", "ruleClassName": "globals_repo", diff --git a/README.md b/README.md index e62a0d0..242fc15 100644 --- a/README.md +++ b/README.md @@ -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 `.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 +``` + diff --git a/projects/service-1/src/main/BUILD.bazel b/projects/service-1/src/main/BUILD.bazel index de6bbd2..4811b81 100644 --- a/projects/service-1/src/main/BUILD.bazel +++ b/projects/service-1/src/main/BUILD.bazel @@ -1,4 +1,4 @@ -scala_binary( +scala_library( name = "service1", srcs = glob(["scala/**/*.scala"]), main_class = "com.org.service1.Service1App", @@ -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"], +) diff --git a/projects/service-2/src/main/BUILD.bazel b/projects/service-2/src/main/BUILD.bazel index 2c4ef4b..a9aeec1 100644 --- a/projects/service-2/src/main/BUILD.bazel +++ b/projects/service-2/src/main/BUILD.bazel @@ -1,4 +1,4 @@ -scala_binary( +scala_library( name = "service2", srcs = glob(["scala/**/*.scala"]), main_class = "com.org.service2.Service2App", @@ -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"], +) diff --git a/tools/BUILD.bazel b/tools/BUILD.bazel new file mode 100644 index 0000000..e69de29 diff --git a/tools/build_rules/prelude_bazel b/tools/build_rules/prelude_bazel index a0341a7..7139fc6 100644 --- a/tools/build_rules/prelude_bazel +++ b/tools/build_rules/prelude_bazel @@ -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") diff --git a/tools/oci.bzl b/tools/oci.bzl new file mode 100644 index 0000000..b595ed0 --- /dev/null +++ b/tools/oci.bzl @@ -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, + )