diff --git a/examples/MODULE.bazel b/examples/MODULE.bazel index ce8ad9f..13158ce 100644 --- a/examples/MODULE.bazel +++ b/examples/MODULE.bazel @@ -4,6 +4,8 @@ bazel_dep(name = "rules_python", version = "0.38.0") bazel_dep(name = "boost.any", version = "1.83.0") bazel_dep(name = "boost.smart_ptr", version = "1.83.0.bcr.1") bazel_dep(name = "boost.thread", version = "1.83.0") +bazel_dep(name = "rules_oci", version = "2.0.1") +bazel_dep(name = "rules_pkg", version = "1.0.1") # This import is relevant for these examples and this (rules_ros) repository. bazel_dep(name = "rules_ros") @@ -44,3 +46,18 @@ use_repo( "urdfdom", "urdfdom_headers", ) + +# in order to build an oci image with our ros nodes in it, one +# needs to define the base image to use, and that must be performed +# at the module level here. +# NOTE: Even though the version of python that the nodes run in is contained +# in the runfiles, a version of python is required at the system level in order +# to run the roslaunch command. +oci = use_extension("@rules_oci//oci:extensions.bzl", "oci") +oci.pull( + name = "rules_ros_base_image", + image = "python:3.10-bookworm", + digest = "sha256:fd0fa50d997eb56ce560c6e5ca6a1f5cf8fdff87572a16ac07fb1f5ca01eb608", + platforms = ["linux/amd64"], +) +use_repo(oci, "rules_ros_base_image", "rules_ros_base_image_linux_amd64") diff --git a/examples/chatter/BUILD.bazel b/examples/chatter/BUILD.bazel index 82b7763..2d6083f 100644 --- a/examples/chatter/BUILD.bazel +++ b/examples/chatter/BUILD.bazel @@ -19,6 +19,12 @@ load("@rules_ros//ros:launch.bzl", "ros_launch") load("@rules_ros//ros:test.bzl", "ros_test") load("@rules_ros//ros:topic.bzl", "ros_topic") load("@rules_ros//third_party:expand_template.bzl", "expand_template") +load( + "@rules_oci//oci:defs.bzl", + "oci_image", + "oci_load" +) +load("@rules_pkg//pkg:pkg.bzl", "pkg_tar") # Handling of ROS messages & services resembles to some extent Bazel's rules for # handling protobuf messages (e.g. proto_library and cc_proto_library). @@ -127,3 +133,37 @@ ros_topic( name = "rostopic", deps = [":chatter"], ) + + +# packages up the nodes, launch file, and all the other required runfiles. +# Note the symlink is required in order to place the files where ros_launch is +# expecting them to be. +pkg_tar( + name = "chatter_tar", + srcs = [ + ":chatter", + ], + include_runfiles = True, + package_dir = "chatter", + symlinks = { + "/chatter/chatter.runfiles/_main/external": "/chatter/chatter.runfiles", + } +) + +# builds the oci image on top of the @rules_ros_base_image defined at the MODULE level. +oci_image( + name = "chatter_oci_image", + base = "@rules_ros_base_image", + tars = [":chatter_tar"], + workdir = "/chatter/chatter.runfiles/_main", + entrypoint = ["/chatter/chatter"], +) + +# loads the oci image into the docker daemon. +# Example usage: `bazel run //chatter:chatter_oci_load` +# Then to run the container: `docker run -it --rm chatter:latest` +oci_load( + name = "chatter_oci_load", + image = ":chatter_oci_image", + repo_tags = ["chatter:latest"], +)