chore(ci): with async/await

This commit is contained in:
Kasper Juul Hermansen 2023-02-19 22:51:34 +01:00
parent 04990247ba
commit 45d6462037
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
3 changed files with 44 additions and 41 deletions

37
Cargo.lock generated
View File

@ -114,8 +114,9 @@ version = "0.1.0"
dependencies = [ dependencies = [
"clap", "clap",
"color-eyre", "color-eyre",
"dagger-sdk 0.2.6", "dagger-sdk 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"eyre", "eyre",
"tokio",
] ]
[[package]] [[package]]
@ -328,23 +329,6 @@ dependencies = [
"tempfile", "tempfile",
] ]
[[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 = "dagger-sdk" name = "dagger-sdk"
version = "0.2.8" version = "0.2.8"
@ -363,6 +347,23 @@ dependencies = [
"tokio", "tokio",
] ]
[[package]]
name = "dagger-sdk"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e90636d5b74ce8a62e8507a8d075c4d997c3123a1709596d0f97c3070f993b2d"
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,6 @@ 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.6" dagger-sdk = "0.2.8"
eyre = "0.6.8" eyre = "0.6.8"
tokio = { version = "1.25.0", features = ["full"] }

View File

@ -2,7 +2,8 @@ use std::sync::Arc;
use dagger_sdk::{Container, HostDirectoryOpts, Query}; use dagger_sdk::{Container, HostDirectoryOpts, Query};
fn main() -> eyre::Result<()> { #[tokio::main]
async fn main() -> eyre::Result<()> {
color_eyre::install().unwrap(); color_eyre::install().unwrap();
let matches = clap::Command::new("ci") let matches = clap::Command::new("ci")
@ -15,10 +16,10 @@ fn main() -> eyre::Result<()> {
match matches.subcommand() { match matches.subcommand() {
Some(("pr", _)) => { Some(("pr", _)) => {
let base = select_base_image(client.clone())?; let base = select_base_image(client.clone()).await?;
return validate_pr(client, base); return validate_pr(client, base).await;
} }
Some(("release", subm)) => return release(client, subm), Some(("release", subm)) => return release(client, subm).await,
Some(_) => { Some(_) => {
panic!("invalid subcommand selected!") panic!("invalid subcommand selected!")
} }
@ -28,7 +29,7 @@ fn main() -> eyre::Result<()> {
} }
} }
fn release(client: Arc<Query>, _subm: &clap::ArgMatches) -> Result<(), color_eyre::Report> { async fn release(client: Arc<Query>, _subm: &clap::ArgMatches) -> Result<(), color_eyre::Report> {
let src_dir = client.host().directory_opts( let src_dir = client.host().directory_opts(
".", ".",
HostDirectoryOpts { HostDirectoryOpts {
@ -40,7 +41,7 @@ fn release(client: Arc<Query>, _subm: &clap::ArgMatches) -> Result<(), color_eyr
.container() .container()
.from("rust:latest") .from("rust:latest")
.with_workdir("app") .with_workdir("app")
.with_mounted_directory("/app/", src_dir.id()?); .with_mounted_directory("/app/", src_dir.id().await?);
let container = base_image let container = base_image
.with_exec(vec!["cargo", "install", "cargo-smart-release"]) .with_exec(vec!["cargo", "install", "cargo-smart-release"])
@ -53,7 +54,7 @@ fn release(client: Arc<Query>, _subm: &clap::ArgMatches) -> Result<(), color_eyr
"dagger-rs", "dagger-rs",
"dagger-sdk", "dagger-sdk",
]); ]);
let exit = container.exit_code()?; let exit = container.exit_code().await?;
if exit != 0 { if exit != 0 {
eyre::bail!("container failed with non-zero exit code"); eyre::bail!("container failed with non-zero exit code");
} }
@ -63,7 +64,7 @@ fn release(client: Arc<Query>, _subm: &clap::ArgMatches) -> Result<(), color_eyr
Ok(()) Ok(())
} }
fn get_dependencies(client: Arc<Query>) -> eyre::Result<Container> { async fn get_dependencies(client: Arc<Query>) -> eyre::Result<Container> {
let cargo_dir = client.host().directory_opts( let cargo_dir = client.host().directory_opts(
".", ".",
HostDirectoryOpts { HostDirectoryOpts {
@ -121,16 +122,16 @@ fn get_dependencies(client: Arc<Query>) -> eyre::Result<Container> {
.with_env_variable("SCCACHE_BUCKET", "sccache") .with_env_variable("SCCACHE_BUCKET", "sccache")
.with_env_variable("SCCACHE_REGION", "auto") .with_env_variable("SCCACHE_REGION", "auto")
.with_env_variable("SCCACHE_ENDPOINT", "https://api-minio.front.kjuulh.io") .with_env_variable("SCCACHE_ENDPOINT", "https://api-minio.front.kjuulh.io")
.with_mounted_cache("~/.cargo/bin", cache_cargo_bin.id()?) .with_mounted_cache("~/.cargo/bin", cache_cargo_bin.id().await?)
.with_mounted_cache("~/.cargo/registry/index", cache_cargo_bin.id()?) .with_mounted_cache("~/.cargo/registry/index", cache_cargo_bin.id().await?)
.with_mounted_cache("~/.cargo/registry/cache", cache_cargo_bin.id()?) .with_mounted_cache("~/.cargo/registry/cache", cache_cargo_bin.id().await?)
.with_mounted_cache("~/.cargo/git/db", cache_cargo_bin.id()?) .with_mounted_cache("~/.cargo/git/db", cache_cargo_bin.id().await?)
.with_mounted_cache("target/", cache_cargo_bin.id()?) .with_mounted_cache("target/", cache_cargo_bin.id().await?)
.with_exec(vec!["cargo", "install", "cargo-chef"]); .with_exec(vec!["cargo", "install", "cargo-chef"]);
let recipe = base_image let recipe = base_image
.with_mounted_directory(".", cargo_dir.id()?) .with_mounted_directory(".", cargo_dir.id().await?)
.with_mounted_cache("~/.cargo/.package-cache", cache_cargo_index_dir.id()?) .with_mounted_cache("~/.cargo/.package-cache", cache_cargo_index_dir.id().await?)
.with_exec(vec![ .with_exec(vec![
"cargo", "cargo",
"chef", "chef",
@ -141,7 +142,7 @@ fn get_dependencies(client: Arc<Query>) -> eyre::Result<Container> {
.file("/app/recipe.json"); .file("/app/recipe.json");
let builder_start = base_image let builder_start = base_image
.with_mounted_file("/app/recipe.json", recipe.id()?) .with_mounted_file("/app/recipe.json", recipe.id().await?)
.with_exec(vec![ .with_exec(vec![
"cargo", "cargo",
"chef", "chef",
@ -151,23 +152,23 @@ fn get_dependencies(client: Arc<Query>) -> eyre::Result<Container> {
"--recipe-path", "--recipe-path",
"recipe.json", "recipe.json",
]) ])
.with_mounted_cache("/app/", cache_cargo_deps.id()?) .with_mounted_cache("/app/", cache_cargo_deps.id().await?)
.with_mounted_directory("/app/", src_dir.id()?) .with_mounted_directory("/app/", src_dir.id().await?)
.with_exec(vec!["cargo", "build", "--all", "--release"]); .with_exec(vec!["cargo", "build", "--all", "--release"]);
return Ok(builder_start); return Ok(builder_start);
} }
fn select_base_image(client: Arc<Query>) -> eyre::Result<Container> { async fn select_base_image(client: Arc<Query>) -> eyre::Result<Container> {
let src_dir = get_dependencies(client.clone()); let src_dir = get_dependencies(client.clone()).await;
src_dir src_dir
} }
fn validate_pr(_client: Arc<Query>, container: Container) -> eyre::Result<()> { async fn validate_pr(_client: Arc<Query>, container: Container) -> eyre::Result<()> {
//let container = container.with_exec(vec!["cargo", "test", "--all"], None); //let container = container.with_exec(vec!["cargo", "test", "--all"], None);
let exit = container.exit_code()?; let exit = container.exit_code().await?;
if exit != 0 { if exit != 0 {
eyre::bail!("container failed with non-zero exit code"); eyre::bail!("container failed with non-zero exit code");
} }