chore(ci): update ci to use dagger-sdk v0.2.6

This commit is contained in:
Kasper Juul Hermansen 2023-02-19 18:28:47 +01:00
parent c312bc57ad
commit c35c104b49
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
3 changed files with 116 additions and 169 deletions

37
Cargo.lock generated
View File

@ -114,7 +114,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"clap", "clap",
"color-eyre", "color-eyre",
"dagger-sdk 0.2.2", "dagger-sdk 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
"eyre", "eyre",
] ]
@ -328,24 +328,6 @@ dependencies = [
"tempfile", "tempfile",
] ]
[[package]]
name = "dagger-sdk"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d1562df6d7d47fbb891331896ec9c04da1db129c1a9f073c40c33ab918f06e99"
dependencies = [
"base64",
"dagger-core 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"eyre",
"futures",
"genco",
"gql_client",
"pretty_assertions",
"serde",
"serde_json",
"tokio",
]
[[package]] [[package]]
name = "dagger-sdk" name = "dagger-sdk"
version = "0.2.6" version = "0.2.6"
@ -364,6 +346,23 @@ dependencies = [
"tokio", "tokio",
] ]
[[package]]
name = "dagger-sdk"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "654625e954f59d70eb897bb681936c3e8b69c5f6e528f85c0b307554883db63f"
dependencies = [
"base64",
"dagger-core 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"derive_builder",
"eyre",
"futures",
"gql_client",
"serde",
"serde_json",
"tokio",
]
[[package]] [[package]]
name = "darling" name = "darling"
version = "0.14.3" version = "0.14.3"

View File

@ -8,5 +8,5 @@ edition = "2021"
[dependencies] [dependencies]
clap = "4.1.6" clap = "4.1.6"
color-eyre = "0.6.2" color-eyre = "0.6.2"
dagger-sdk = "0.2.2" dagger-sdk = "0.2.6"
eyre = "0.6.8" eyre = "0.6.8"

View File

@ -1,6 +1,6 @@
use std::sync::Arc; use std::sync::Arc;
use dagger_sdk::gen::{Container, HostDirectoryOpts, Query}; use dagger_sdk::{Container, HostDirectoryOpts, Query};
fn main() -> eyre::Result<()> { fn main() -> eyre::Result<()> {
color_eyre::install().unwrap(); color_eyre::install().unwrap();
@ -11,11 +11,11 @@ fn main() -> eyre::Result<()> {
.subcommand(clap::Command::new("release")) .subcommand(clap::Command::new("release"))
.get_matches(); .get_matches();
let client = dagger_sdk::client::connect()?; let client = dagger_sdk::connect()?;
match matches.subcommand() { match matches.subcommand() {
Some(("pr", _)) => { Some(("pr", _)) => {
let base = select_base_image(client.clone()); let base = select_base_image(client.clone())?;
return validate_pr(client, base); return validate_pr(client, base);
} }
Some(("release", subm)) => return release(client, subm), Some(("release", subm)) => return release(client, subm),
@ -29,41 +29,31 @@ fn main() -> eyre::Result<()> {
} }
fn release(client: Arc<Query>, _subm: &clap::ArgMatches) -> Result<(), color_eyre::Report> { fn release(client: Arc<Query>, _subm: &clap::ArgMatches) -> Result<(), color_eyre::Report> {
let src_dir = client.host().directory( let src_dir = client.host().directory_opts(
".".into(), ".",
Some(HostDirectoryOpts { HostDirectoryOpts {
exclude: Some(vec!["target/".into()]), exclude: Some(vec!["target/"]),
include: None, include: None,
}), },
); );
let base_image = client let base_image = client
.container(None) .container()
.from("rust:latest".into()) .from("rust:latest")
.with_workdir("app".into()) .with_workdir("app")
.with_mounted_directory("/app/".into(), src_dir.id()); .with_mounted_directory("/app/", src_dir.id()?);
let container = base_image let container = base_image
.with_exec( .with_exec(vec!["cargo", "install", "cargo-smart-release"])
vec![ .with_exec(vec![
"cargo".into(), "cargo",
"install".into(), "smart-release",
"cargo-smart-release".into(), "--execute",
], "--allow-fully-generated-changelogs",
None, "--no-changelog-preview",
) "dagger-rs",
.with_exec( "dagger-sdk",
vec![ ]);
"cargo".into(), let exit = container.exit_code()?;
"smart-release".into(),
"--execute".into(),
"--allow-fully-generated-changelogs".into(),
"--no-changelog-preview".into(),
"dagger-rs".into(),
"dagger-sdk".into(),
],
None,
);
let exit = container.exit_code();
if exit != 0 { if exit != 0 {
eyre::bail!("container failed with non-zero exit code"); eyre::bail!("container failed with non-zero exit code");
} }
@ -73,153 +63,111 @@ fn release(client: Arc<Query>, _subm: &clap::ArgMatches) -> Result<(), color_eyr
Ok(()) Ok(())
} }
fn get_dependencies(client: Arc<Query>) -> Container { fn get_dependencies(client: Arc<Query>) -> eyre::Result<Container> {
let cargo_dir = client.host().directory( let cargo_dir = client.host().directory_opts(
".".into(), ".",
Some(HostDirectoryOpts { HostDirectoryOpts {
exclude: None, exclude: None,
include: Some(vec![ include: Some(vec![
"**/Cargo.lock".into(), "**/Cargo.lock",
"**/Cargo.toml".into(), "**/Cargo.toml",
"**/main.rs".into(), "**/main.rs",
"**/lib.rs".into(), "**/lib.rs",
]), ]),
}), },
); );
let src_dir = client.host().directory( let src_dir = client.host().directory_opts(
".".into(), ".",
Some(HostDirectoryOpts { HostDirectoryOpts {
exclude: Some(vec!["target/".into()]), exclude: Some(vec!["target/"]),
include: None, include: None,
}), },
); );
let cache_cargo_index_dir = client.cache_volume("cargo_index".into()); let cache_cargo_index_dir = client.cache_volume("cargo_index");
let cache_cargo_deps = client.cache_volume("cargo_deps".into()); let cache_cargo_deps = client.cache_volume("cargo_deps");
let cache_cargo_bin = client.cache_volume("cargo_bin_cache".into()); let cache_cargo_bin = client.cache_volume("cargo_bin_cache");
let minio_url = "https://github.com/mozilla/sccache/releases/download/v0.3.3/sccache-v0.3.3-x86_64-unknown-linux-musl.tar.gz".into(); let minio_url = "https://github.com/mozilla/sccache/releases/download/v0.3.3/sccache-v0.3.3-x86_64-unknown-linux-musl.tar.gz";
let base_image = client let base_image = client
.container(None) .container()
.from("rust:latest".into()) .from("rust:latest")
.with_workdir("app".into()) .with_workdir("app")
.with_exec(vec!["apt-get".into(), "update".into()], None) .with_exec(vec!["apt-get", "update"])
.with_exec( .with_exec(vec!["apt-get", "install", "--yes", "libpq-dev", "wget"])
vec![ .with_exec(vec!["wget", minio_url])
"apt-get".into(), .with_exec(vec![
"install".into(), "tar",
"--yes".into(), "xzf",
"libpq-dev".into(), "sccache-v0.3.3-x86_64-unknown-linux-musl.tar.gz",
"wget".into(), ])
], .with_exec(vec![
None, "mv",
) "sccache-v0.3.3-x86_64-unknown-linux-musl/sccache",
.with_exec(vec!["wget".into(), minio_url], None) "/usr/local/bin/sccache",
.with_exec( ])
vec![ .with_exec(vec!["chmod", "+x", "/usr/local/bin/sccache"])
"tar".into(), .with_env_variable("RUSTC_WRAPPER", "/usr/local/bin/sccache")
"xzf".into(),
"sccache-v0.3.3-x86_64-unknown-linux-musl.tar.gz".into(),
],
None,
)
.with_exec(
vec![
"mv".into(),
"sccache-v0.3.3-x86_64-unknown-linux-musl/sccache".into(),
"/usr/local/bin/sccache".into(),
],
None,
)
.with_exec(
vec!["chmod".into(), "+x".into(), "/usr/local/bin/sccache".into()],
None,
)
.with_env_variable("RUSTC_WRAPPER".into(), "/usr/local/bin/sccache".into())
.with_env_variable( .with_env_variable(
"AWS_ACCESS_KEY_ID".into(), "AWS_ACCESS_KEY_ID",
std::env::var("AWS_ACCESS_KEY_ID").unwrap_or("".into()), std::env::var("AWS_ACCESS_KEY_ID").unwrap_or("".into()),
) )
.with_env_variable( .with_env_variable(
"AWS_SECRET_ACCESS_KEY".into(), "AWS_SECRET_ACCESS_KEY",
std::env::var("AWS_SECRET_ACCESS_KEY").unwrap_or("".into()), std::env::var("AWS_SECRET_ACCESS_KEY").unwrap_or("".into()),
) )
.with_env_variable("SCCACHE_BUCKET".into(), "sccache".into()) .with_env_variable("SCCACHE_BUCKET", "sccache")
.with_env_variable("SCCACHE_REGION".into(), "auto".into()) .with_env_variable("SCCACHE_REGION", "auto")
.with_env_variable( .with_env_variable("SCCACHE_ENDPOINT", "https://api-minio.front.kjuulh.io")
"SCCACHE_ENDPOINT".into(), .with_mounted_cache("~/.cargo/bin", cache_cargo_bin.id()?)
"https://api-minio.front.kjuulh.io".into(), .with_mounted_cache("~/.cargo/registry/index", cache_cargo_bin.id()?)
) .with_mounted_cache("~/.cargo/registry/cache", cache_cargo_bin.id()?)
.with_mounted_cache("~/.cargo/bin".into(), cache_cargo_bin.id(), None) .with_mounted_cache("~/.cargo/git/db", cache_cargo_bin.id()?)
.with_mounted_cache("~/.cargo/registry/index".into(), cache_cargo_bin.id(), None) .with_mounted_cache("target/", cache_cargo_bin.id()?)
.with_mounted_cache("~/.cargo/registry/cache".into(), cache_cargo_bin.id(), None) .with_exec(vec!["cargo", "install", "cargo-chef"]);
.with_mounted_cache("~/.cargo/git/db".into(), cache_cargo_bin.id(), None)
.with_mounted_cache("target/".into(), cache_cargo_bin.id(), None)
.with_exec(
vec!["cargo".into(), "install".into(), "cargo-chef".into()],
None,
);
let recipe = base_image let recipe = base_image
.with_mounted_directory(".".into(), cargo_dir.id()) .with_mounted_directory(".", cargo_dir.id()?)
.with_mounted_cache( .with_mounted_cache("~/.cargo/.package-cache", cache_cargo_index_dir.id()?)
"~/.cargo/.package-cache".into(), .with_exec(vec![
cache_cargo_index_dir.id(), "cargo",
None, "chef",
) "prepare",
.with_exec( "--recipe-path",
vec![ "recipe.json",
"cargo".into(), ])
"chef".into(), .file("/app/recipe.json");
"prepare".into(),
"--recipe-path".into(),
"recipe.json".into(),
],
None,
)
.file("/app/recipe.json".into());
let builder_start = base_image let builder_start = base_image
.with_mounted_file("/app/recipe.json".into(), recipe.id()) .with_mounted_file("/app/recipe.json", recipe.id()?)
.with_exec( .with_exec(vec![
vec![ "cargo",
"cargo".into(), "chef",
"chef".into(), "cook",
"cook".into(), "--release",
"--release".into(), "--workspace",
"--workspace".into(), "--recipe-path",
"--recipe-path".into(), "recipe.json",
"recipe.json".into(), ])
], .with_mounted_cache("/app/", cache_cargo_deps.id()?)
None, .with_mounted_directory("/app/", src_dir.id()?)
) .with_exec(vec!["cargo", "build", "--all", "--release"]);
.with_mounted_cache("/app/".into(), cache_cargo_deps.id(), None)
.with_mounted_directory("/app/".into(), src_dir.id())
.with_exec(
vec![
"cargo".into(),
"build".into(),
"--all".into(),
"--release".into(),
],
None,
);
return builder_start; return Ok(builder_start);
} }
fn select_base_image(client: Arc<Query>) -> Container { fn select_base_image(client: Arc<Query>) -> eyre::Result<Container> {
let src_dir = get_dependencies(client.clone()); let src_dir = get_dependencies(client.clone());
src_dir src_dir
} }
fn validate_pr(_client: Arc<Query>, container: Container) -> eyre::Result<()> { fn validate_pr(_client: Arc<Query>, container: Container) -> eyre::Result<()> {
//let container = container.with_exec(vec!["cargo".into(), "test".into(), "--all".into()], None); //let container = container.with_exec(vec!["cargo", "test", "--all"], None);
let exit = container.exit_code(); let exit = container.exit_code()?;
if exit != 0 { if exit != 0 {
eyre::bail!("container failed with non-zero exit code"); eyre::bail!("container failed with non-zero exit code");
} }