Only push containers to registry when ch anged

935 views
Skip to first unread message

za...@usebutton.com

unread,
Aug 23, 2019, 1:43:23 PM8/23/19
to bazel-discuss
We use Bazel to manage the builds for our Go monorepo and have a question about how to properly manager container pushing.

Our process is to run Bazel build/test and then run a step that pushes the build containers to ECR. However, the way we have it setup pushes every service's container to ECR even if that service did not change in the build. To reduce pushes to ECR we are trying to figure out if there is a way to only run the push step on the changed services.

To push, we invoke a Make script that queries for all of our services and then runs the push
for app in $$(bazel query 'kind(container_push, //...)') ; do \
bazel run \
--workspace_status_command=./build/git_sha.sh \
--platforms=@io_bazel_rules_go//go/toolchain:linux_amd64 \
$$app ; \
done

What we are looking to do is find a way to either integrate the push into the build/test steps in a way that only happens on the containers being rebuilt due to changes or to use a query that lets us only run push on the changed ones. We have a script that can piece this together using git diff and rdeps but we are wondering if there is a way to do this in pure Bazel.

Please let me know if any of this isn't clear or if you need more information. Thank you for the help.

For reference, here is a sample BUILD.bazel for one of our services (with real deps/paths/names removed).


load
("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")

go_library
(
    name
= "go_default_library",
    srcs
= ["main.go"],
    importpath
= "github.com/button/ourpath",
    visibility
= ["//visibility:private"],
    deps
= [
     
],
 
)
 

 go_binary
(
    name
= "serviced",
    embed
= [":go_default_library"],
    visibility
= ["//visibility:public"],
 
)
 

 load
("@io_bazel_rules_docker//container:container.bzl", "container_push")
 load
("@io_bazel_rules_docker//go:image.bzl", "go_image")
 

 go_image
(
    name
= "serviced_image",
    embed
= [":go_default_library"],
    importpath
= "github.com/button/ourpath",
 
)
 

 container_push
(
    name
= "publish",
    format
= "Docker",
    registry
= "our-ecr-registry",
    repository
= "service",
    image
= ":serviced_image",
    tag
= "git-{STABLE_GIT_COMMIT}",
 
)


Preston Van Loon

unread,
Aug 24, 2019, 8:12:21 PM8/24/19
to bazel-discuss
I just filed an issue in rules_docker. I'd like this functionality too, but I don't think it is currently supported.

Reply all
Reply to author
Forward
0 new messages