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

NIXPACKS_NO_CACHE config variable #280

Merged
merged 1 commit into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub fn create_docker_image(

let logger = Logger::new();
let builder = DockerBuilder::new(logger, build_options.to_owned());
builder.create_image(app.source.to_str().unwrap(), &plan)?;
builder.create_image(app.source.to_str().unwrap(), &plan, &environment)?;

Ok(())
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ fn main() -> Result<()> {
out_dir,
quiet: false,
cache_key,
no_cache,
};

create_docker_image(path, envs, plan_options, build_options)?;
Expand Down
26 changes: 16 additions & 10 deletions src/nixpacks/builder/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use std::{

use super::Builder;
use crate::nixpacks::{
app, cache::sanitize_cache_key, files, logger::Logger, nix, plan::BuildPlan, NIX_PACKS_VERSION,
app, cache::sanitize_cache_key, environment::Environment, files, logger::Logger, nix,
plan::BuildPlan, NIX_PACKS_VERSION,
};
use anyhow::{bail, Context, Ok, Result};
use indoc::formatdoc;
Expand All @@ -22,6 +23,7 @@ pub struct DockerBuilderOptions {
pub labels: Vec<String>,
pub quiet: bool,
pub cache_key: Option<String>,
pub no_cache: bool,
}

pub struct DockerBuilder {
Expand All @@ -30,7 +32,7 @@ pub struct DockerBuilder {
}

impl Builder for DockerBuilder {
fn create_image(&self, app_src: &str, plan: &BuildPlan) -> Result<()> {
fn create_image(&self, app_src: &str, plan: &BuildPlan, env: &Environment) -> Result<()> {
self.logger
.log_section(format!("Building (nixpacks v{})", NIX_PACKS_VERSION).as_str());

Expand All @@ -51,7 +53,7 @@ impl Builder for DockerBuilder {
// Write everything to destination
self.write_app(app_src, dest).context("Writing app")?;
self.write_assets(plan, dest).context("Writing assets")?;
self.write_dockerfile(plan, dest)
self.write_dockerfile(plan, dest, env)
.context("Writing Dockerfile")?;
self.write_nix_expression(plan, dest)
.context("Writing NIx expression")?;
Expand Down Expand Up @@ -123,8 +125,8 @@ impl DockerBuilder {
files::recursive_copy_dir(app_src, &dest)
}

fn write_dockerfile(&self, plan: &BuildPlan, dest: &str) -> Result<()> {
let dockerfile = self.create_dockerfile(plan);
fn write_dockerfile(&self, plan: &BuildPlan, dest: &str, env: &Environment) -> Result<()> {
let dockerfile = self.create_dockerfile(plan, env);

let dockerfile_path = PathBuf::from(dest).join(PathBuf::from("Dockerfile"));
File::create(dockerfile_path.clone()).context("Creating Dockerfile file")?;
Expand Down Expand Up @@ -167,7 +169,7 @@ impl DockerBuilder {
Ok(())
}

fn create_dockerfile(&self, plan: &BuildPlan) -> String {
fn create_dockerfile(&self, plan: &BuildPlan, env: &Environment) -> String {
let app_dir = "/app/";
let assets_dir = app::ASSETS_DIR;

Expand All @@ -178,6 +180,12 @@ impl DockerBuilder {
let variables = plan.variables.clone().unwrap_or_default();
let static_assets = plan.static_assets.clone().unwrap_or_default();

let cache_key = if !self.options.no_cache && !env.is_config_variable_truthy("NO_CACHE") {
self.options.cache_key.clone()
} else {
None
};

// -- Variables
let args_string = if !variables.is_empty() {
format!(
Expand Down Expand Up @@ -232,8 +240,7 @@ impl DockerBuilder {
};

// -- Install
let install_cache_mount =
get_cache_mount(&self.options.cache_key, &install_phase.cache_directories);
let install_cache_mount = get_cache_mount(&cache_key, &install_phase.cache_directories);

let install_cmd = install_phase
.cmds
Expand Down Expand Up @@ -261,8 +268,7 @@ impl DockerBuilder {
.unwrap_or_else(|| vec![".".to_string()]);

// -- Build
let build_cache_mount =
get_cache_mount(&self.options.cache_key, &build_phase.cache_directories);
let build_cache_mount = get_cache_mount(&cache_key, &build_phase.cache_directories);

let build_cmd = build_phase
.cmds
Expand Down
4 changes: 2 additions & 2 deletions src/nixpacks/builder/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::plan::BuildPlan;
use super::{environment::Environment, plan::BuildPlan};
use anyhow::Result;

pub mod docker;

pub trait Builder {
fn create_image(&self, app_source: &str, plan: &BuildPlan) -> Result<()>;
fn create_image(&self, app_source: &str, plan: &BuildPlan, env: &Environment) -> Result<()>;
}