diff --git a/.gitignore b/.gitignore index ecc94fee2..bd2432479 100644 --- a/.gitignore +++ b/.gitignore @@ -53,3 +53,6 @@ bazel-testlogs # Npm packages node_modules + +# JetBrains garbage +.idea/ diff --git a/README.md b/README.md index ccc0ccf69..1714f3ff0 100644 --- a/README.md +++ b/README.md @@ -613,11 +613,13 @@ nodejs_image( name = "nodejs_image", entry_point = "@your_workspace//path/to:file.js", # npm deps will be put into their own layer - data = [":file.js", "@npm//some-npm-dep"], + data = [":file.js", "@npm//some-npm-dep"], ... ) ``` +`nodejs_image` also supports the `launcher` and `launcher_args` attributes which are passed to `container_image` and used to prefix the image's `entry_point`. + If you need to modify somehow the container produced by `nodejs_image` (e.g., `env`, `symlink`), see note above in Language Rules Overview about how to do this diff --git a/nodejs/image.bzl b/nodejs/image.bzl index 84b91aac8..7c7259651 100644 --- a/nodejs/image.bzl +++ b/nodejs/image.bzl @@ -115,6 +115,8 @@ def nodejs_image( data = [], layers = [], binary = None, + launcher = None, + launcher_args = None, **kwargs): """Constructs a container image wrapping a nodejs_binary target. @@ -125,6 +127,8 @@ def nodejs_image( layers: Augments "deps" with dependencies that should be put into their own layers. binary: An alternative binary target to use instead of generating one. + launcher: The container_image launcher to set. + launcher_args: The args for the container_image launcher. **kwargs: See nodejs_binary. """ @@ -168,4 +172,6 @@ def nodejs_image( args = kwargs.get("args"), data = data, testonly = kwargs.get("testonly"), + launcher = launcher, + launcher_args = launcher_args, ) diff --git a/run-bazel-in-docker.sh b/run-bazel-in-docker.sh new file mode 100755 index 000000000..b3e89c891 --- /dev/null +++ b/run-bazel-in-docker.sh @@ -0,0 +1,15 @@ +# Runs bazel in a docker container, with this repository's workspace mounted in its file system. +# This is useful because rules_docker assumes the host environment is linux, and tests will fail on other environments. +# Running bazel in docker is slower, but allows for the build and tests to execute on non-linux environments. + +mkdir -p /tmp/build_output/ +# -e USER="$(id -u)" \ +# -u="$(id -u)" \ +docker run \ + -v "$PWD":/workspace \ + -v /tmp/build_output:/tmp/build_output \ + -v /var/run/docker.sock:/var/run/docker.sock \ + -w /workspace \ + l.gcr.io/google/bazel:latest \ + --output_user_root=/tmp/build_output \ + "$@" \ No newline at end of file diff --git a/tests/container/nodejs/BUILD b/tests/container/nodejs/BUILD index 9d665b6b3..c335571df 100644 --- a/tests/container/nodejs/BUILD +++ b/tests/container/nodejs/BUILD @@ -33,6 +33,40 @@ nodejs_image( entry_point = "@io_bazel_rules_docker//testdata:nodejs_image.js", ) +nodejs_image( + name = "nodejs_image_with_launcher", + args = [ + "arg0", + "arg1", + ], + data = [ + "//testdata:nodejs_image.js", + "//testdata:nodejs_image_lib", + "@npm//jsesc", + ], + entry_point = "@io_bazel_rules_docker//testdata:nodejs_image.js", + launcher = "launcher.sh", +) + +nodejs_image( + name = "nodejs_image_with_launcher_args", + args = [ + "arg0", + "arg1", + ], + data = [ + "//testdata:nodejs_image.js", + "//testdata:nodejs_image_lib", + "@npm//jsesc", + ], + entry_point = "@io_bazel_rules_docker//testdata:nodejs_image.js", + launcher = "launcher.sh", + launcher_args = [ + "foo", + "bar", + ], +) + # Docker Cmd value should be `[""]`. nodejs_image( name = "nodejs_image_list_with_empty_string_args", @@ -114,6 +148,18 @@ container_test( image = ":nodejs_image", ) +container_test( + name = "nodejs_image_with_launcher_test", + configs = ["//tests/container/nodejs/configs:nodejs_image_with_launcher.yaml"], + image = ":nodejs_image_with_launcher", +) + +container_test( + name = "nodejs_image_with_launcher_args_test", + configs = ["//tests/container/nodejs/configs:nodejs_image_with_launcher_args.yaml"], + image = ":nodejs_image_with_launcher_args", +) + container_test( name = "nodejs_image_list_with_empty_string_args_test", configs = ["//tests/container/nodejs/configs:nodejs_image_list_with_empty_string_args.yaml"], diff --git a/tests/container/nodejs/configs/nodejs_image_with_launcher.yaml b/tests/container/nodejs/configs/nodejs_image_with_launcher.yaml new file mode 100644 index 000000000..3955e2d2f --- /dev/null +++ b/tests/container/nodejs/configs/nodejs_image_with_launcher.yaml @@ -0,0 +1,13 @@ +schemaVersion: 2.0.0 + +metadataTest: + cmd: ["arg0", "arg1"] + env: + - key: PORT + value: "8080" + - key: DEBIAN_FRONTEND + value: "noninteractive" + - key: PATH + value: "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" + entrypoint: ['/launcher.sh', '/app/tests/container/nodejs/nodejs_image_with_launcher.binary'] + workdir: "/app/tests/container/nodejs/nodejs_image_with_launcher.binary.runfiles/io_bazel_rules_docker" diff --git a/tests/container/nodejs/configs/nodejs_image_with_launcher_args.yaml b/tests/container/nodejs/configs/nodejs_image_with_launcher_args.yaml new file mode 100644 index 000000000..716c45ad8 --- /dev/null +++ b/tests/container/nodejs/configs/nodejs_image_with_launcher_args.yaml @@ -0,0 +1,13 @@ +schemaVersion: 2.0.0 + +metadataTest: + cmd: ["arg0", "arg1"] + env: + - key: PORT + value: "8080" + - key: DEBIAN_FRONTEND + value: "noninteractive" + - key: PATH + value: "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" + entrypoint: ['/launcher.sh', 'foo', 'bar', '/app/tests/container/nodejs/nodejs_image_with_launcher_args.binary'] + workdir: "/app/tests/container/nodejs/nodejs_image_with_launcher_args.binary.runfiles/io_bazel_rules_docker" diff --git a/tests/container/nodejs/launcher.sh b/tests/container/nodejs/launcher.sh new file mode 100644 index 000000000..13484fe49 --- /dev/null +++ b/tests/container/nodejs/launcher.sh @@ -0,0 +1,5 @@ +# Do something before running CMD +echo "Do something before running CMD" + +# Run whatever entrypoint / cmd args are passed next +"$@" \ No newline at end of file