Permalink
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
tilt-example-bazel/3-recommended/Tiltfile
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
88 lines (73 sloc)
3.79 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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']) |