Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Docker multistage build cache #337

Merged
merged 4 commits into from
Jun 20, 2023

Conversation

igor-alexandrov
Copy link
Contributor

This PR implements support for Docker build cache and closes #329.

Currently I added support for two cache types: gha and registry. Both cache types allow to specified additional options that will be used in cache-to param. Registry cache also allows to specify image to be used for cache, by default application image is used with -build-cache suffix.

I've tested this on Rails 7 application. Both gha and registry cache works and saves about 40% of build time.

docker(:build, *build_options, build_context),
docker(:push, config.absolute_image),
docker(:push, config.latest_image)
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of doing conditionals on all of these, I think maybe we should have Mrsk::Commands::Builder::Native::Cached?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, added Mrsk::Commands::Builder::Native::Cached builder.

[
"type=gha",
@options["cache"]&.fetch("options", nil),
].compact.join(",")
Copy link
Member

@dhh dhh Jun 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract this as cache_config_for_gha or similar.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

"type=registry",
@options["cache"]&.fetch("options", nil),
"ref=#{@server}/#{cache_image}"
].compact.join(",")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract this as cache_config_for_registry or similar.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

remove_context(local["arch"]),
remove_context(remote["arch"])
remove_context(config.builder.local_arch),
remove_context(config.builder.remote_arch)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe setup a delegate_to for config.builder for remote/local_arch|host.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added delegation of all used public methods from Mrsk::Configuration::Builder to builder_config. Lines became shorter, readability improved.

@dhh
Copy link
Member

dhh commented Jun 18, 2023

Nice use of the extracted builder config object 👍

@igor-alexandrov
Copy link
Contributor Author

igor-alexandrov commented Jun 18, 2023

David, thanks for your comments. Will take a look later today.

case @options["cache"]["type"]
when 'gha'
"type=gha"
when 'registry'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consistently use " not '

if cached?
case @options["cache"]["type"]
when 'gha'
"type=gha"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use cache_from_config_for_gha to remain at the same level of abstraction.

[
"type=registry",
"ref=#{@server}/#{cache_image}"
].compact.join(",")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turn into single line.

[
"type=gha",
@options["cache"]&.fetch("options", nil),
].compact.join(",")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turn into single line.

"type=registry",
@options["cache"]&.fetch("options", nil),
"ref=#{@server}/#{cache_image}"
].compact.join(",")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turn into single line.

*build_options,
build_context
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add CR

@dhh
Copy link
Member

dhh commented Jun 19, 2023

Just a few more minor points. Looking good!

@igor-alexandrov
Copy link
Contributor Author

Fixed all code style comments. This is why I proposed Rubocop :)

@dhh dhh merged commit 08d8790 into basecamp:main Jun 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cached multistage docker builds for faster builds
2 participants