Skip to content

Commit 1b75b77

Browse files
authored
Allow launcher in nodejs (#1854)
* 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
1 parent 359898d commit 1b75b77

File tree

8 files changed

+104
-1
lines changed

8 files changed

+104
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,6 @@ bazel-testlogs
5353

5454
# Npm packages
5555
node_modules
56+
57+
# JetBrains garbage
58+
.idea/

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,11 +613,13 @@ nodejs_image(
613613
name = "nodejs_image",
614614
entry_point = "@your_workspace//path/to:file.js",
615615
# npm deps will be put into their own layer
616-
data = [":file.js", "@npm//some-npm-dep"],
616+
data = [":file.js", "@npm//some-npm-dep"],
617617
...
618618
)
619619
```
620620

621+
`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`.
622+
621623
If you need to modify somehow the container produced by
622624
`nodejs_image` (e.g., `env`, `symlink`), see note above in
623625
<a href=#overview-1>Language Rules Overview</a> about how to do this

nodejs/image.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ def nodejs_image(
115115
data = [],
116116
layers = [],
117117
binary = None,
118+
launcher = None,
119+
launcher_args = None,
118120
**kwargs):
119121
"""Constructs a container image wrapping a nodejs_binary target.
120122
@@ -125,6 +127,8 @@ def nodejs_image(
125127
layers: Augments "deps" with dependencies that should be put into
126128
their own layers.
127129
binary: An alternative binary target to use instead of generating one.
130+
launcher: The container_image launcher to set.
131+
launcher_args: The args for the container_image launcher.
128132
**kwargs: See nodejs_binary.
129133
"""
130134

@@ -168,4 +172,6 @@ def nodejs_image(
168172
args = kwargs.get("args"),
169173
data = data,
170174
testonly = kwargs.get("testonly"),
175+
launcher = launcher,
176+
launcher_args = launcher_args,
171177
)

run-bazel-in-docker.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Runs bazel in a docker container, with this repository's workspace mounted in its file system.
2+
# This is useful because rules_docker assumes the host environment is linux, and tests will fail on other environments.
3+
# Running bazel in docker is slower, but allows for the build and tests to execute on non-linux environments.
4+
5+
mkdir -p /tmp/build_output/
6+
# -e USER="$(id -u)" \
7+
# -u="$(id -u)" \
8+
docker run \
9+
-v "$PWD":/workspace \
10+
-v /tmp/build_output:/tmp/build_output \
11+
-v /var/run/docker.sock:/var/run/docker.sock \
12+
-w /workspace \
13+
l.gcr.io/google/bazel:latest \
14+
--output_user_root=/tmp/build_output \
15+
"$@"

tests/container/nodejs/BUILD

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,40 @@ nodejs_image(
3333
entry_point = "@io_bazel_rules_docker//testdata:nodejs_image.js",
3434
)
3535

36+
nodejs_image(
37+
name = "nodejs_image_with_launcher",
38+
args = [
39+
"arg0",
40+
"arg1",
41+
],
42+
data = [
43+
"//testdata:nodejs_image.js",
44+
"//testdata:nodejs_image_lib",
45+
"@npm//jsesc",
46+
],
47+
entry_point = "@io_bazel_rules_docker//testdata:nodejs_image.js",
48+
launcher = "launcher.sh",
49+
)
50+
51+
nodejs_image(
52+
name = "nodejs_image_with_launcher_args",
53+
args = [
54+
"arg0",
55+
"arg1",
56+
],
57+
data = [
58+
"//testdata:nodejs_image.js",
59+
"//testdata:nodejs_image_lib",
60+
"@npm//jsesc",
61+
],
62+
entry_point = "@io_bazel_rules_docker//testdata:nodejs_image.js",
63+
launcher = "launcher.sh",
64+
launcher_args = [
65+
"foo",
66+
"bar",
67+
],
68+
)
69+
3670
# Docker Cmd value should be `[""]`.
3771
nodejs_image(
3872
name = "nodejs_image_list_with_empty_string_args",
@@ -114,6 +148,18 @@ container_test(
114148
image = ":nodejs_image",
115149
)
116150

151+
container_test(
152+
name = "nodejs_image_with_launcher_test",
153+
configs = ["//tests/container/nodejs/configs:nodejs_image_with_launcher.yaml"],
154+
image = ":nodejs_image_with_launcher",
155+
)
156+
157+
container_test(
158+
name = "nodejs_image_with_launcher_args_test",
159+
configs = ["//tests/container/nodejs/configs:nodejs_image_with_launcher_args.yaml"],
160+
image = ":nodejs_image_with_launcher_args",
161+
)
162+
117163
container_test(
118164
name = "nodejs_image_list_with_empty_string_args_test",
119165
configs = ["//tests/container/nodejs/configs:nodejs_image_list_with_empty_string_args.yaml"],
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
schemaVersion: 2.0.0
2+
3+
metadataTest:
4+
cmd: ["arg0", "arg1"]
5+
env:
6+
- key: PORT
7+
value: "8080"
8+
- key: DEBIAN_FRONTEND
9+
value: "noninteractive"
10+
- key: PATH
11+
value: "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
12+
entrypoint: ['/launcher.sh', '/app/tests/container/nodejs/nodejs_image_with_launcher.binary']
13+
workdir: "/app/tests/container/nodejs/nodejs_image_with_launcher.binary.runfiles/io_bazel_rules_docker"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
schemaVersion: 2.0.0
2+
3+
metadataTest:
4+
cmd: ["arg0", "arg1"]
5+
env:
6+
- key: PORT
7+
value: "8080"
8+
- key: DEBIAN_FRONTEND
9+
value: "noninteractive"
10+
- key: PATH
11+
value: "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
12+
entrypoint: ['/launcher.sh', 'foo', 'bar', '/app/tests/container/nodejs/nodejs_image_with_launcher_args.binary']
13+
workdir: "/app/tests/container/nodejs/nodejs_image_with_launcher_args.binary.runfiles/io_bazel_rules_docker"

tests/container/nodejs/launcher.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Do something before running CMD
2+
echo "Do something before running CMD"
3+
4+
# Run whatever entrypoint / cmd args are passed next
5+
"$@"

0 commit comments

Comments
 (0)