Skip to content

Commit

Permalink
Allow launcher in nodejs (#1854)
Browse files Browse the repository at this point in the history
* create nodejs_image test cases for launcher and launcher_args
add launcher and launcher_args attrs to nodejs_image, and pass them to app_layer
update docs with note about launcher and launcher_args for nodejs_image
add run-bazel-in-docker.sh allowing non-linux platforms to execute repo build and tests
add .idea to .gitignore
Add test for using nodejs_image with launcher arg

* add documentation for run-bazel-in-docker.sh
  • Loading branch information
martaver authored Jun 10, 2021
1 parent 359898d commit 1b75b77
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,6 @@ bazel-testlogs

# Npm packages
node_modules

# JetBrains garbage
.idea/
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<a href=#overview-1>Language Rules Overview</a> about how to do this
Expand Down
6 changes: 6 additions & 0 deletions nodejs/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
"""

Expand Down Expand Up @@ -168,4 +172,6 @@ def nodejs_image(
args = kwargs.get("args"),
data = data,
testonly = kwargs.get("testonly"),
launcher = launcher,
launcher_args = launcher_args,
)
15 changes: 15 additions & 0 deletions run-bazel-in-docker.sh
Original file line number Diff line number Diff line change
@@ -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 \
"$@"
46 changes: 46 additions & 0 deletions tests/container/nodejs/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"],
Expand Down
13 changes: 13 additions & 0 deletions tests/container/nodejs/configs/nodejs_image_with_launcher.yaml
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -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"
5 changes: 5 additions & 0 deletions tests/container/nodejs/launcher.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Do something before running CMD
echo "Do something before running CMD"

# Run whatever entrypoint / cmd args are passed next
"$@"

0 comments on commit 1b75b77

Please sign in to comment.