Practical Bazel: Use multirun for parallel execution
Practical Bazel bazel
Published: 2023-03-09
Practical Bazel: Use multirun for parallel execution

A quick tip on how to execute multiple run commands in parallel.

Sometimes it is useful to execute multiple Bazel run commands in parallel. For example, imagine you are deploying software to multiple systems at once – it might be significantly more efficient to run these commands in parallel.

The multirun() command, which I originally found in Atlassian’s bazel-tools repository, makes this easy:

WORKSPACE file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

# multirun is written in Go and hence needs rules_go to be built.
# See https://github.com/bazelbuild/rules_go for the up to date setup instructions.
http_archive(
    name = "io_bazel_rules_go",
)

git_repository(
    name = "com_github_atlassian_bazel_tools",
    commit = "<commit>",
    remote = "https://github.com/atlassian/bazel-tools.git",
    shallow_since = "<bla>",
)

load("@com_github_atlassian_bazel_tools//multirun:deps.bzl", "multirun_dependencies")

multirun_dependencies()

BUILD.bazel file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
load("@com_github_atlassian_bazel_tools//multirun:def.bzl", "multirun", "command")

multirun(
    name = "run_all_parallel",
    commands = [
        ":command1",
        "//some/other:label",
    ],
    parallel = True,
)