feat: add option for releaser
All checks were successful
continuous-integration/drone/push Build is passing

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2024-05-25 23:03:07 +02:00
parent cdae730c6b
commit c76601a695
Signed by: kjuulh
GPG Key ID: 9AA7BC13CE474394
6 changed files with 3062 additions and 9 deletions

2964
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -11,5 +11,7 @@ tracing-subscriber = { version = "0.3.18" }
clap = { version = "4", features = ["derive", "env"] } clap = { version = "4", features = ["derive", "env"] }
dotenv = { version = "0.15" } dotenv = { version = "0.15" }
flux-releaser = { git = "https://git.front.kjuulh.io/kjuulh/flux-releaser", branch = "main" }
[workspace.package] [workspace.package]
version = "0.1.1" version = "0.1.1"

View File

@ -20,6 +20,8 @@ walkdir = "2.5.0"
minijinja = "2.0.1" minijinja = "2.0.1"
futures = "0.3.30" futures = "0.3.30"
flux-releaser.workspace = true
[[test]] [[test]]
name = "integration" name = "integration"
path = "tests/tests.rs" path = "tests/tests.rs"

View File

@ -7,3 +7,5 @@ pub mod catalog;
pub mod process; pub mod process;
pub use process::{process, process_opts}; pub use process::{process, process_opts};
pub mod releaser;

View File

@ -29,7 +29,7 @@ pub trait UploadStrategy {
pub struct NoUploadStrategy {} pub struct NoUploadStrategy {}
impl UploadStrategy for NoUploadStrategy { impl UploadStrategy for NoUploadStrategy {
fn upload(&self, input_path: &Path) -> BoxFuture<'_, anyhow::Result<()>> { fn upload(&self, _input_path: &Path) -> BoxFuture<'_, anyhow::Result<()>> {
async move { Ok(()) }.boxed() async move { Ok(()) }.boxed()
} }
} }

View File

@ -0,0 +1,99 @@
use std::{
path::{Path, PathBuf},
process::Command,
};
use flux_releaser::{
app::{LocalApp, SharedLocalApp},
services::flux_local_cluster::extensions::FluxLocalClusterManagerExt,
};
use futures::{future::BoxFuture, FutureExt};
use crate::process::UploadStrategy;
#[derive(Default, Clone)]
pub struct Releaser {
registry: String,
service: String,
}
impl Releaser {
pub fn with_registry(&mut self, registry: impl Into<String>) -> &mut Self {
self.service = registry.into();
self
}
pub fn with_service(&mut self, service: impl Into<String>) -> &mut Self {
self.service = service.into();
self
}
pub async fn release(&self, input_path: impl Into<PathBuf>) -> anyhow::Result<()> {
let input_path = input_path.into();
let branch = self.get_branch()?.ok_or(anyhow::anyhow!(
"failed to find branch, required for triggering release"
))?;
tracing::trace!("triggering release for: {}", input_path.display());
let local_app =
SharedLocalApp::new(LocalApp::new(&self.registry).await?).flux_local_cluster_manager();
let upload_id = local_app.package_clusters(input_path).await?;
local_app
.commit_artifact(&self.service, &branch, upload_id)
.await?;
local_app.trigger_release(&self.service, &branch).await?;
Ok(())
}
pub fn get_branch(&self) -> anyhow::Result<Option<String>> {
let output = Command::new("git")
.args(["rev-parse", "--abbrev-ref", "HEAD"])
.output()?;
if output.status.success() {
let branch = std::str::from_utf8(&output.stdout)?.trim().to_string();
Ok(Some(branch))
} else {
let err = std::str::from_utf8(&output.stderr)?;
anyhow::bail!("Failed to get branch name: {}", err)
}
}
}
impl UploadStrategy for Releaser {
fn upload(&self, input_path: &Path) -> BoxFuture<'_, anyhow::Result<()>> {
let input_path = input_path.to_path_buf();
async move {
self.release(input_path).await?;
Ok(())
}
.boxed()
}
}
#[cfg(test)]
mod test {
use crate::releaser::Releaser;
#[tokio::test]
async fn can_upload_test() -> anyhow::Result<()> {
let releaser = Releaser::default();
releaser
.release(
"/home/kjuulh/git/git.front.kjuulh.io/kjuulh/cuddle-rust-service-plan/.cuddle/tmp/cuddle-clusters",
)
.await?;
Ok(())
}
}