use std::sync::Arc; use dagger_sdk::{HostDirectoryOptsBuilder, QueryContainerOptsBuilder}; use tokio::sync::Mutex; #[tokio::main] async fn main() -> eyre::Result<()> { let _ = dotenv::dotenv(); let client = dagger_sdk::connect().await?; let src = client.host().directory_opts( ".", HostDirectoryOptsBuilder::default() .exclude(vec![".git", "node_modules", "public", "ci", "target"]) .build()?, ); let node_cache = client.cache_volume("node_cache"); let debian_cache = client.cache_volume("debian_cache"); let styles_file = client .container() .from("node:16") .with_workdir("/app") .with_directory(".", src.id().await?) .with_mounted_cache("node_modules", node_cache.id().await?) .with_exec(vec!["yarn"]) .with_exec(vec!["yarn", "compile"]) .file("static/styles/styles.css"); let zola_version = "0.17.2-1"; let debian_edition = "bullseye"; let debian_platform = "amd64"; let tag = chrono::Utc::now().timestamp(); let github_zola_download = format!("https://github.com/barnumbirr/zola-debian/releases/download/v{zola_version}/zola_{zola_version}_{debian_platform}_{debian_edition}.deb"); let pull_articles_docker_image = "docker.io/kasperhermansen/pull-articles:1690413158"; let update_deployments_docker_image = "docker.io/kasperhermansen/update-deployment:1690401410"; let pull_articles = client .container() .from(pull_articles_docker_image) .with_exec(vec!["echo", &format!("{}", tag)]) .with_env_variable("GIT_USERNAME", "kjuulh") .with_env_variable("GIT_PASSWORD", std::env::var("GIT_PASSWORD").unwrap()) .with_exec(vec![ "pull-articles", "--repo", "https://git.front.kjuulh.io/kjuulh/obsidian.git", "--path", "areas/blog/posts", "--out", "/mnt/posts", "--static-out=/mnt/static", ]) .directory("/mnt/"); let dist_dir = client .container_opts( dagger_sdk::QueryContainerOptsBuilder::default() .platform(format!("linux/{debian_platform}")) .build()?, ) .from(format!("debian:{debian_edition}")) .with_exec(vec!["apt", "update"]) .with_exec(vec!["apt", "install", "wget", "-y"]) .with_workdir("/mnt") .with_mounted_cache("/mnt", debian_cache.id().await?) .with_exec(vec!["wget", &github_zola_download]) .with_exec(vec![ "dpkg", "-i", format!("zola_{zola_version}_{debian_platform}_{debian_edition}.deb").as_str(), ]) .with_workdir("/app") .with_directory(".", src.id().await?) .with_directory( "content/posts", pull_articles.directory("posts").id().await?, ) .with_directory( "static/assets", pull_articles.directory("static").id().await?, ) .with_file( "content/posts/_index.md", src.file("content/posts/_index.md").id().await?, ) .with_file("static/styles/styles.css", styles_file.id().await?) .with_exec(vec!["zola", "build"]) .directory("public"); let caddy_file = client.host().directory("deployment").file("Caddyfile"); let variants = vec!["linux/amd64", "linux/arm64"]; let platform_variants = Arc::new(Mutex::new(Vec::new())); tokio_scoped::scope(|s| { for platform in variants { let caddy_file = caddy_file.clone(); let client = client.clone(); let dist_dir = dist_dir.clone(); let platform_variants = platform_variants.clone(); s.spawn(async move { let dep_image = client .container_opts( QueryContainerOptsBuilder::default() .platform(platform) .build() .unwrap(), ) .from("caddy") .with_directory("/usr/share/caddy", dist_dir.id().await.unwrap()) .with_file("/etc/caddy/Caddyfile", caddy_file.id().await.unwrap()); let mut platform_variants = platform_variants.lock().await; platform_variants.push(dep_image.id().await.unwrap()) }); } }); let variants = platform_variants .lock() .await .iter() .map(|c| c.clone()) .collect::>(); let version = client .container() .publish_opts( format!("kasperhermansen/kasperhermansen-blog:{tag}"), dagger_sdk::ContainerPublishOptsBuilder::default() .platform_variants(variants) .build()?, ) .await?; let update_deployment = client .container() .from(update_deployments_docker_image) .with_env_variable("GIT_USERNAME", "kjuulh") .with_env_variable("GIT_PASSWORD", std::env::var("GIT_PASSWORD").unwrap()) .with_exec(vec![ "update-deployment", "--repo", "https://git.front.kjuulh.io/kjuulh/blog-deployment.git", "--service", "blog", "--image", &format!("kasperhermansen/kasperhermansen-blog:{tag}"), ]) .exit_code() .await?; Ok(()) }