Monorepo several binaries and build only some

211 views
Skip to first unread message

Diego Medina

unread,
Sep 14, 2020, 1:15:32 PM9/14/20
to golang-nuts

Hi,

Is there a way to know if a binary has to be rebuilt, due to changes to some of its dependencies?

Let's say I have a repo with this structure:

/cmd/tool1/main.go
/cmd/tool2/main.go
/pkg/users/update.go
/pkg/math/avg.go


tool1 imports the math package
tool2 imports both, math and user packages

As part of our CI/CD, I need to know that if a Pull Request only updates the user pacakge, only tool2 needs to be deployed to production.

This is of course a simplified example, in real life we'll have a few more binaries and a much higher number of packages.

Thanks

Diego





Alex

unread,
Sep 14, 2020, 5:00:23 PM9/14/20
to golang-nuts
You can run the command: go list -f {{.Stale}} 
It will print a "true\n" or "false\n" if the package/program in the current folder needs to be rebuilt.

Alex

unread,
Sep 14, 2020, 5:02:53 PM9/14/20
to golang-nuts
Sorry, return values were reversed. "true\n" if it needs rebuilding

Diego Medina

unread,
Sep 14, 2020, 8:03:34 PM9/14/20
to golang-nuts
Thanks!

Igor Ovsiannikov

unread,
Sep 14, 2020, 10:19:19 PM9/14/20
to golang-nuts
Diego wrote:
> Is there a way to know if a binary has to be rebuilt, due to changes to some of its dependencies?

It maybe a bit an off topic, but if you have a mono repo and want to be able to
specify internal dependencies as well and rebuild only what is necessary you may
want to look into Bazel[1] build tool.  It is definitely an investment, but for
bigger and/or long-living projects it is well worth it.

Reply all
Reply to author
Forward
0 new messages