Skip to content

Commit

Permalink
sdk: allow passing docker build arguments in service manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
remcoros committed Oct 22, 2024
1 parent c4e5b7f commit d71e0ab
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 2 deletions.
45 changes: 44 additions & 1 deletion core/startos/src/s9pk/v2/pack.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::path::{Path, PathBuf};
use std::sync::Arc;
Expand Down Expand Up @@ -296,6 +297,7 @@ impl TryFrom<CliImageConfig> for ImageConfig {
ImageSource::DockerBuild {
dockerfile: value.dockerfile,
workdir: value.workdir,
build_args: None
}
} else if let Some(tag) = value.docker_tag {
ImageSource::DockerTag(tag)
Expand Down Expand Up @@ -339,6 +341,22 @@ impl clap::FromArgMatches for ImageConfig {
}
}

#[derive(Debug, Clone, Deserialize, Serialize, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export)]
struct EnvVar {
env: String,
}

#[derive(Debug, Clone, Deserialize, Serialize, TS)]
#[serde(rename_all = "camelCase")]
#[serde(untagged)]
#[ts(export)]
pub enum BuildArg {
String(String),
EnvVar(EnvVar),
}

#[derive(Debug, Clone, Deserialize, Serialize, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export)]
Expand All @@ -348,6 +366,9 @@ pub enum ImageSource {
DockerBuild {
workdir: Option<PathBuf>,
dockerfile: Option<PathBuf>,
#[serde(skip_serializing_if = "Option::is_none")]
#[ts(optional)]
build_args: Option<BTreeMap<String, BuildArg>>
},
DockerTag(String),
}
Expand Down Expand Up @@ -386,6 +407,7 @@ impl ImageSource {
ImageSource::DockerBuild {
workdir,
dockerfile,
build_args
} => {
let workdir = workdir.as_deref().unwrap_or(Path::new("."));
let dockerfile = dockerfile
Expand All @@ -400,14 +422,35 @@ impl ImageSource {
};
// docker buildx build ${path} -o type=image,name=start9/${id}
let tag = format!("start9/{id}/{image_id}:{}", new_guid());
Command::new(CONTAINER_TOOL)
let mut command = Command::new(CONTAINER_TOOL);
command
.arg("build")
.arg(workdir)
.arg("-f")
.arg(dockerfile)
.arg("-t")
.arg(&tag)
.arg(&docker_platform)
.arg("--build-arg").arg(format!("ARCH={}", arch));

// add build arguments
if let Some(build_args) = build_args {
for (key, value) in build_args {
let build_arg_value = match value {
BuildArg::String(val) => val.to_string(),
BuildArg::EnvVar(env_var) => {
match std::env::var(&env_var.env) {
Ok(val) => val,
Err(_) => continue, // skip if env var not set or invalid
}
},
};

command.arg("--build-arg").arg(format!("{}={}", key, build_arg_value));
}
}

command
.arg("-o")
.arg("type=docker,dest=-")
.capture(false)
Expand Down
4 changes: 4 additions & 0 deletions sdk/base/lib/osBindings/BuildArg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { EnvVar } from "./EnvVar"

export type BuildArg = string | EnvVar
3 changes: 3 additions & 0 deletions sdk/base/lib/osBindings/EnvVar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type EnvVar = { env: string }
9 changes: 8 additions & 1 deletion sdk/base/lib/osBindings/ImageSource.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { BuildArg } from "./BuildArg"

export type ImageSource =
| "packed"
| { dockerBuild: { workdir: string | null; dockerfile: string | null } }
| {
dockerBuild: {
workdir: string | null
dockerfile: string | null
buildArgs?: { [key: string]: BuildArg }
}
}
| { dockerTag: string }
2 changes: 2 additions & 0 deletions sdk/base/lib/osBindings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export { BindOptions } from "./BindOptions"
export { BindParams } from "./BindParams"
export { Blake3Commitment } from "./Blake3Commitment"
export { BlockDev } from "./BlockDev"
export { BuildArg } from "./BuildArg"
export { CallbackId } from "./CallbackId"
export { Category } from "./Category"
export { CheckDependenciesParam } from "./CheckDependenciesParam"
Expand All @@ -62,6 +63,7 @@ export { DestroySubcontainerFsParams } from "./DestroySubcontainerFsParams"
export { Duration } from "./Duration"
export { EchoParams } from "./EchoParams"
export { EncryptedWire } from "./EncryptedWire"
export { EnvVar } from "./EnvVar"
export { ExportActionParams } from "./ExportActionParams"
export { ExportServiceInterfaceParams } from "./ExportServiceInterfaceParams"
export { ExposeForDependentsParams } from "./ExposeForDependentsParams"
Expand Down

0 comments on commit d71e0ab

Please sign in to comment.