-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTiltfile
88 lines (73 loc) · 3.79 KB
/
Tiltfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# -*- mode: Python -*-
# For more on Extensions, see: https://docs.tilt.dev/extensions.html
load('ext://restart_process', 'custom_build_with_restart')
load('./bazel.Tiltfile', 'bazel_sourcefile_deps')
# Records the current time, then kicks off a server update.
# Normally, you would let Tilt do deploys automatically, but this
# shows you how to set up a custom workflow that measures it.
local_resource(
'deploy',
'python record-start-time.py',
)
# Use Bazel to generate the Kubernetes YAML
for f in bazel_sourcefile_deps('//deployments:example-go'):
watch_file(f)
k8s_yaml(local('bazel run //deployments:example-go'))
# Use Bazel to build the image
# The go_image BUILD rule
image_target='//:example-go-image'
binary_target='//:example-go'
# Where go_binary puts the binary. We query Bazel's action graph and parse the response.
# The path is not necessarily predictable across bazel rulesets, or even across releases
# of the same ruleset. Even configuration flags passed to Bazel can affect the path.
# See https://github.com/bazelbuild/rules_go/blob/dea7cd1/README.rst#how-do-i-access-go_binary-executables-from-go_test
action_graph = decode_json(local([
'bazel',
'aquery',
'--output',
'jsonproto',
# Needed for compat with Bazel 3 and 4, see https://github.com/bazelbuild/bazel/issues/10358
'--noincompatible_proto_output_v2',
binary_target,
], quiet=True))
# If the image_target is a go_binary, it will have a GoLink action
go_link_actions = [action for action in action_graph['actions'] if action['mnemonic'] == 'GoLink']
# In our example this isn't used, but if your image were a nodejs_image you'd need the whole Bazel "runfiles"
# which is constructed as a symlink tree
symlink_tree_actions = [action for action in action_graph['actions'] if action['mnemonic'] == 'SymlinkTree']
if len(go_link_actions) == 1:
outputIDs = [outputID for action in go_link_actions for outputID in action['outputIds']]
binary_target_local = [a['execPath'] for a in action_graph['artifacts'] if a['id'] in outputIDs][0]
# Where go_image puts the Go binary in the container. You can determine this
# by shelling into the container with `kubectl exec -it [pod name] -- sh`
container_workdir = '/app/example-go-image.binary.runfiles/__main__/'
binary_target_container = container_workdir + 'example-go-image.binary_/example-go-image.binary'
elif len(symlink_tree_actions) > 0:
outputIDs = [outputID for action in symlink_tree_actions for outputID in action['outputIds']]
outputFiles = [a['execPath'] for a in action_graph['artifacts'] if a['id'] in outputIDs]
# translate from the MANIFEST file path to the first-party monorepo outputs
# the tilt-example-bazel here is the name in our WORKSPACE
# bazel-out/k8-fastbuild/bin/path/to.runfiles/MANIFEST
# ->
# bazel-out/k8-fastbuild/bin/path/to.runfiles/tilt-example-bazel
binary_target_local = outputFiles[0][:-len('/MANIFEST')] + '/tilt-example-bazel'
binary_target_container = '/'
# Where go_image puts the image in Docker (bazel/path/to/target:name)
bazel_image='bazel:example-go-image'
local_resource(
name='example-go-compile',
cmd='bazel build --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64 {binary_target}'.format(binary_target=binary_target),
deps=bazel_sourcefile_deps(binary_target))
custom_build_with_restart(
ref='example-go-image',
command=(
'bazel run --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64 {image_target} -- --norun && ' +
'docker tag {bazel_image} $EXPECTED_REF').format(image_target=image_target, bazel_image=bazel_image),
deps=['web', binary_target_local],
entrypoint=binary_target_container,
live_update=[
sync('web', container_workdir + 'web'),
sync(binary_target_local, binary_target_container)
],
)
k8s_resource('example-go', port_forwards=8000, resource_deps=['deploy', 'example-go-compile'])