feat: Stages (#173)
## Stages A new property (`stages`) is being added to the recipe file schema. This property will allow users to define a list of Containerfile stages each with their own modules. Stages can be used to compile programs, perform parallel operations, and copy the results into the final image without contaminating the final image. ### Module Support Currently the only modules that work out-of-the-box are `copy`, `script`, `files`, and `containerfile`. Other modules are dependent on the programs installed on the image. In order to better support some of our essential modules, a setup script is ran at the start of each stage that is not `scratch`. This script will install `curl`, `wget`, `bash`, and `grep` and use the package manager for the detected distributions. At this time, the following distributions are supported: - Debian - Ubuntu - Fedora - Alpine Contributions to increase the size of this list is [welcome](https://github.com/blue-build/cli)! ### Syntax - **Required** - `from` - The full image ref (image name + tag). This will be set in the `FROM` statement of the stage. - `name` - The name of the stage. This is used when referencing the stage when using the `from:` property in the `copy` module. - `modules` - The list of modules to execute. The exact same syntax used by the main recipe `modules:` property. - **Optional** - `shell` - Allows a user to pass in an array of strings that are passed directly into the [`SHELL` instruction](https://docs.docker.com/reference/dockerfile/#shell). #### Example ```yaml stages: - name: ubuntu-test from: ubuntu modules: - type: files files: - usr: /usr - type: script scripts: - example.sh snippets: - echo "test" > /test.txt - type: test-module - type: containerfile containerfiles: - labels snippets: - RUN echo "This is a snippet" ``` ### Tasks - [x] `from-file:` - Allows the user to store their stages in a separate file so it can be included in multiple recipes - [x] `no-cache:` - This will be useful for stages that want to pull the latest changes from a git repo and not have to rely on the base image getting an update for the build to be triggered again. - [x] Add setup script to be able to install necessary programs to run `bluebuild` modules in stages - [x] Check for circular dependencies and error out ## `copy` module This is a 1-1 for the [`COPY` instruction](https://docs.docker.com/reference/dockerfile/#copy). It has the ability to copy files between stages, making this a very important addition to complete functionality for the stages feature. Each use of this "module" will become its own layer. ### Decision to use `--link` We use the `--link` [option](https://docs.docker.com/reference/dockerfile/#benefits-of-using---link) which allows that layer to have the same hash if the files haven't changed regardless of if the previous instructions have changed. This allows these layers to not have to be re-downloaded on the user's computer if the copied files haven't changed. ### Syntax - **Required** - `src` - The source directory/file from the repo OR when `from:` is set the image/stage that is specified. - `dest` - The destination directory/file inside the working image. - **Optional** - `from` - The stage/image to copy from. #### Example ```yaml modules: - type: copy from: ubuntu-test src: /test.txt dest: / ``` ### Tasks - [x] make `from:` optional - [x] Add README.md and module.yml ## Feature gating Gating this feature until we release for `v0.9.0`. The plan will be to build all features (including this one) for main branch builds. This means that these features will be available when using the `main` image and consequently the `use_unstable_cli:` option on the GitHub Action. All future `v0.9.0` features will be gated as well to allow for patches to `v0.8`. ### Tasks - [x] Build `--all-features` on non-tagged builds - [x] Add stages and copy features
This commit is contained in:
parent
8308e5b285
commit
8069006c03
31 changed files with 742 additions and 119 deletions
|
|
@ -35,9 +35,8 @@ impl std::fmt::Display for DefaultThemes {
|
|||
/// # Errors
|
||||
/// Will error if the theme doesn't exist, the syntax doesn't exist, or the file
|
||||
/// failed to serialize.
|
||||
pub fn print(file: &str, file_type: &str, theme: Option<DefaultThemes>) -> Result<()> {
|
||||
trace!("syntax_highlighting::print({file}, {file_type}, {theme:?})");
|
||||
|
||||
pub fn highlight(file: &str, file_type: &str, theme: Option<DefaultThemes>) -> Result<String> {
|
||||
trace!("syntax_highlighting::highlight(file, {file_type}, {theme:?})");
|
||||
if atty::is(atty::Stream::Stdout) {
|
||||
let ss: SyntaxSet = if file_type == "dockerfile" || file_type == "Dockerfile" {
|
||||
dumps::from_uncompressed_data(include_bytes!(concat!(
|
||||
|
|
@ -58,15 +57,43 @@ pub fn print(file: &str, file_type: &str, theme: Option<DefaultThemes>) -> Resul
|
|||
.get(theme.unwrap_or_default().to_string().as_str())
|
||||
.ok_or_else(|| anyhow!("Failed to get highlight theme"))?,
|
||||
);
|
||||
|
||||
let mut highlighted_lines: Vec<String> = vec![];
|
||||
for line in file.lines() {
|
||||
let ranges = h.highlight_line(line, &ss)?;
|
||||
let escaped = syntect::util::as_24_bit_terminal_escaped(&ranges, false);
|
||||
println!("{escaped}");
|
||||
highlighted_lines.push(syntect::util::as_24_bit_terminal_escaped(
|
||||
&h.highlight_line(line, &ss)?,
|
||||
false,
|
||||
));
|
||||
}
|
||||
println!("\x1b[0m");
|
||||
highlighted_lines.push("\x1b[0m".to_string());
|
||||
Ok(highlighted_lines.join("\n"))
|
||||
} else {
|
||||
println!("{file}");
|
||||
Ok(file.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
/// Takes a serializable struct and serializes it with syntax highlighting.
|
||||
///
|
||||
/// # Errors
|
||||
/// Will error if the theme doesn't exist, the syntax doesn't exist, or the file
|
||||
/// failed to serialize.
|
||||
pub fn highlight_ser<T: Serialize + std::fmt::Debug>(
|
||||
file: &T,
|
||||
file_type: &str,
|
||||
theme: Option<DefaultThemes>,
|
||||
) -> Result<String> {
|
||||
trace!("syntax_highlighting::highlight_ser(file, {file_type}, {theme:?})");
|
||||
highlight(serde_yaml::to_string(file)?.as_str(), file_type, theme)
|
||||
}
|
||||
|
||||
/// Prints the file with syntax highlighting.
|
||||
///
|
||||
/// # Errors
|
||||
/// Will error if the theme doesn't exist, the syntax doesn't exist, or the file
|
||||
/// failed to serialize.
|
||||
pub fn print(file: &str, file_type: &str, theme: Option<DefaultThemes>) -> Result<()> {
|
||||
trace!("syntax_highlighting::print(file, {file_type}, {theme:?})");
|
||||
println!("{}", highlight(file, file_type, theme)?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
@ -75,11 +102,12 @@ pub fn print(file: &str, file_type: &str, theme: Option<DefaultThemes>) -> Resul
|
|||
/// # Errors
|
||||
/// Will error if the theme doesn't exist, the syntax doesn't exist, or the file
|
||||
/// failed to serialize.
|
||||
pub fn print_ser<T: Serialize>(
|
||||
pub fn print_ser<T: Serialize + std::fmt::Debug>(
|
||||
file: &T,
|
||||
file_type: &str,
|
||||
theme: Option<DefaultThemes>,
|
||||
) -> Result<()> {
|
||||
trace!("syntax_highlighting::print_ser(file, {file_type}, {theme:?})");
|
||||
print(serde_yaml::to_string(file)?.as_str(), file_type, theme)?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue