From 3d49ef4411db588f29a8a634c0fd634db4a08954 Mon Sep 17 00:00:00 2001 From: Tsiry Sandratraina Date: Sat, 14 Sep 2024 18:16:40 +0000 Subject: [PATCH] [buildx] add build_cloud function --- Cargo.lock | 2 +- buildx/dagger.json | 2 +- buildx/plugin/Cargo.toml | 2 +- buildx/plugin/src/lib.rs | 49 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a846498..153cdf7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -52,7 +52,7 @@ dependencies = [ [[package]] name = "buildx" -version = "0.1.1" +version = "0.1.2" dependencies = [ "anyhow", "extism-pdk", diff --git a/buildx/dagger.json b/buildx/dagger.json index 6d43c0e..00f51d5 100644 --- a/buildx/dagger.json +++ b/buildx/dagger.json @@ -1,7 +1,7 @@ { "name": "buildx", "sdk": "github.com/fluentci-io/daggerverse/deno-sdk@main", - "version": "v0.1.1", + "version": "v0.1.2", "description": "", "author": "Tsiry Sandratraina", "license": "MIT" diff --git a/buildx/plugin/Cargo.toml b/buildx/plugin/Cargo.toml index e364f1e..02b7685 100644 --- a/buildx/plugin/Cargo.toml +++ b/buildx/plugin/Cargo.toml @@ -1,7 +1,7 @@ [package] edition = "2021" name = "buildx" -version = "0.1.1" +version = "0.1.2" [lib] crate-type = ["cdylib"] diff --git a/buildx/plugin/src/lib.rs b/buildx/plugin/src/lib.rs index bcb73c7..9a8fa40 100644 --- a/buildx/plugin/src/lib.rs +++ b/buildx/plugin/src/lib.rs @@ -50,6 +50,55 @@ pub fn build(args: String) -> FnResult { Ok(stdout) } +#[plugin_fn] +pub fn build_cloud(args: String) -> FnResult { + let builder = dag().get_env("BUILDX_BUILDER")?; + if builder.is_empty() { + return Err(Error::msg("BUILDX_BUILDER must be set").into()); + } + + let version = dag() + .get_env("BUILDX_VERSION") + .unwrap_or("v0.17.1-desktop.1".into()); + let version = match version.as_str() { + "" => "v0.17.1-desktop.1".into(), + _ => version, + }; + let (os, arch) = detect_system()?; + + let buildx_download_url = format!( + "https://github.com/docker/buildx-desktop/releases/download/{}/buildx-{}.{}-{}", + version, version, os, arch + ); + + let buildx_plugin = format!("buildx-{}.{}-{}", version, os, arch); + let stdout = dag() + .pipeline("build")? + .pkgx()? + .with_exec(vec!["pkgx", "install", "docker", "wget"])? + .with_exec(vec![&format!( + r#" + if [ ! -f $HOME/.docker/cli-plugins/{} ]; then + wget {}; + chmod +x {}; + mkdir -p $HOME/.docker/cli-plugins; + mv {} $HOME/.docker/cli-plugins; + fi + "#, + buildx_plugin, buildx_download_url, buildx_plugin, buildx_plugin + )])? + .with_exec(vec![&format!( + "docker buildx create --driver cloud {} || true", + &builder + )])? + .with_exec(vec!["docker", "buildx", "inspect", "--bootstrap"])? + .with_exec(vec!["docker", "buildx", "version"])? + .with_exec(vec!["docker", "-v"])? + .with_exec(vec!["docker", "buildx", "build", &args])? + .stdout()?; + Ok(stdout) +} + #[plugin_fn] pub fn publish(args: String) -> FnResult { let registry_password = dag().get_env("REGISTRY_PASSWORD")?;