Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow launcher in nodejs #1854

Merged
merged 3 commits into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
"$@"