-
Notifications
You must be signed in to change notification settings - Fork 178
feat: support build options - no_cache, skip_if_exists and buildargs
#856
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6d8e248
build: update bollard to version supporting buildkit_providerless fea…
mbodmer c7949a1
Merge branch 'main' into fix-bollard-buildkit-providerless
DDtKey 637b87e
feat: support build options - `no_cache`, `skip_if_exists` and `build…
DDtKey e01ec1d
chore: don't use skip_build for server image
DDtKey 31178ed
temporary fix
DDtKey 2f354c8
patch with http
DDtKey e370de3
docs: add docs for buildable docker images
DDtKey 5f6cd97
docs: sync imports
DDtKey 2345db3
chore: add building_images.md to docs
DDtKey 50969b2
Merge branch 'main' into buildkit-providerless
DDtKey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
File renamed without changes.
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| use std::collections::HashMap; | ||
|
|
||
| /// Options for configuring image build behavior. | ||
| /// | ||
| /// Provides control over various aspects of the Docker image build process, | ||
| /// such as caching, whether to skip building if the image already exists, and build arguments. | ||
| /// | ||
| /// # Example | ||
| /// | ||
| /// ```rust,no_run | ||
| /// use testcontainers::{ | ||
| /// core::BuildImageOptions, | ||
| /// runners::AsyncBuilder, | ||
| /// GenericBuildableImage, | ||
| /// }; | ||
| /// | ||
| /// # async fn example() -> anyhow::Result<()> { | ||
| /// let image = GenericBuildableImage::new("my-app", "latest") | ||
| /// .with_dockerfile_string("FROM alpine:latest\nARG VERSION\nRUN echo $VERSION") | ||
| /// .build_image_with( | ||
| /// BuildImageOptions::new() | ||
| /// .with_skip_if_exists(true) | ||
| /// .with_build_arg("VERSION", "1.0.0") | ||
| /// ) | ||
| /// .await?; | ||
| /// # Ok(()) | ||
| /// # } | ||
| /// ``` | ||
| #[derive(Default, Debug, Clone, PartialEq, Eq)] | ||
| pub struct BuildImageOptions { | ||
| pub(crate) skip_if_exists: bool, | ||
| pub(crate) no_cache: bool, | ||
| pub(crate) build_args: HashMap<String, String>, | ||
| } | ||
|
|
||
| impl BuildImageOptions { | ||
| /// Creates a new `BuildImageOptions` with default values. | ||
| /// | ||
| /// All options default to `false` and `build_args` is empty. | ||
| pub fn new() -> Self { | ||
| Self::default() | ||
| } | ||
|
|
||
| /// Sets whether to skip building if the image already exists. | ||
| /// | ||
| /// When `true`, the build process will first check if an image with the | ||
| /// specified descriptor (name:tag) already exists. If it does, the build | ||
| /// is skipped and the existing image is used. | ||
| /// | ||
| /// Default: `false` | ||
| pub fn with_skip_if_exists(mut self, skip_if_exists: bool) -> Self { | ||
| self.skip_if_exists = skip_if_exists; | ||
| self | ||
| } | ||
|
|
||
| /// Sets whether to disable build cache. | ||
| /// | ||
| /// When `true`, Docker will not use cached layers from previous builds, | ||
| /// ensuring a completely fresh build from scratch. | ||
| /// | ||
| /// Default: `false` | ||
| pub fn with_no_cache(mut self, no_cache: bool) -> Self { | ||
| self.no_cache = no_cache; | ||
| self | ||
| } | ||
|
|
||
| /// Adds a single build argument. | ||
| /// | ||
| /// Build arguments are passed to the Docker build process and can be used | ||
| /// in the Dockerfile with `ARG` instructions. This method appends to existing | ||
| /// build arguments, allowing multiple calls to add different arguments. | ||
| /// | ||
| /// # Arguments | ||
| /// | ||
| /// * `key` - The name of the build argument | ||
| /// * `value` - The value of the build argument | ||
| /// | ||
| /// # Example | ||
| /// | ||
| /// ```rust,no_run | ||
| /// use testcontainers::core::BuildImageOptions; | ||
| /// | ||
| /// let opts = BuildImageOptions::new() | ||
| /// .with_build_arg("VERSION", "1.0.0") | ||
| /// .with_build_arg("BUILD_DATE", "2024-01-01"); | ||
| /// ``` | ||
| pub fn with_build_arg(mut self, key: impl Into<String>, value: impl Into<String>) -> Self { | ||
| self.build_args.insert(key.into(), value.into()); | ||
| self | ||
| } | ||
|
|
||
| /// Replaces all build arguments with the provided HashMap. | ||
| /// | ||
| /// Build arguments are passed to the Docker build process and can be used | ||
| /// in the Dockerfile with `ARG` instructions. This method replaces any | ||
| /// existing build arguments. | ||
| /// | ||
| /// # Arguments | ||
| /// | ||
| /// * `build_args` - A HashMap of build argument names to values | ||
| /// | ||
| /// # Example | ||
| /// | ||
| /// ```rust,no_run | ||
| /// use std::collections::HashMap; | ||
| /// use testcontainers::core::BuildImageOptions; | ||
| /// | ||
| /// let mut args = HashMap::new(); | ||
| /// args.insert("VERSION".to_string(), "1.0.0".to_string()); | ||
| /// args.insert("BUILD_DATE".to_string(), "2024-01-01".to_string()); | ||
| /// | ||
| /// let opts = BuildImageOptions::new().with_build_args(args); | ||
| /// ``` | ||
| pub fn with_build_args(mut self, build_args: HashMap<String, String>) -> Self { | ||
| self.build_args = build_args; | ||
| self | ||
| } | ||
| } |
File renamed without changes.
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| pub mod build_context; | ||
| pub mod build_options; | ||
| pub mod buildable; |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: drop and update version once fussybeaver/bollard#597 merged and released