Lines
58.62 %
Functions
33.33 %
Branches
100 %
//! The nomisync CI pipeline, as a program.
//!
//! Invoked from `.forgejo/workflows/ci.yml` as `cargo run -p xtask -- <command>`.
//! Deliberately dependency-light: this is the first thing a CI job builds, and
//! it starts the database the rest of the workspace compiles against, so it
//! must not itself need one.
//! What stays outside: `.forgejo/scripts/deploy.sh` runs in a kaniko init
//! container that has no Rust toolchain, and `ci/builder/build.sh` builds the
//! container image this program runs inside.
mod artifact;
mod clean;
mod coverage;
mod deploy_render;
mod heartbeat;
mod postgres;
mod publish;
mod release;
mod snapshot;
mod watchdog;
#[cfg(test)]
mod test_support;
use anyhow::{Context, Result};
use clap::{Parser, Subcommand};
use artifact::Kind;
use heartbeat::Heartbeat;
#[derive(Parser)]
#[command(about = "nomisync CI tasks")]
struct Cli {
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
/// Run a command against a throwaway, migrated Postgres.
Postgres {
/// The command and its arguments.
#[arg(required = true, trailing_var_arg = true)]
argv: Vec<String>,
},
/// Instrumented test run, grcov, and the coverage payload.
Coverage,
/// Docs and the static musl binaries, and the release payload.
Release,
/// Restore a prod snapshot into fresh local databases and open every
/// transaction and template in the forms; the databases are dropped after.
CheckForms {
/// Directory with `nomisync.dump` and one `<tenant database>.dump` each.
dumps: std::path::PathBuf,
/// Local Postgres server URL without a database name.
#[arg(long, default_value = "postgres://localhost")]
server: String,
/// Restore a prod snapshot as `check-forms` does and list the templates and
/// sources that name accounts or commodities ambiguously or by bare name.
CheckReferences {
/// Directory of shipped source files to scan.
#[arg(long)]
sources: Option<std::path::PathBuf>,
/// Verify the published release, then write the manifest.
Finalize,
fn main() -> Result<()> {
if let Some(failure) = coverage::wrapper::intercept() {
return Err(failure);
match Cli::parse().command {
Command::Postgres { argv } => with_postgres(&argv),
Command::Coverage => produce(Kind::Coverage),
Command::Release => produce(Kind::Release),
Command::CheckForms { dumps, server } => snapshot::check_forms(&dumps, &server),
Command::CheckReferences {
dumps,
server,
sources,
} => snapshot::check_references(&dumps, &server, sources.as_deref()),
Command::Finalize => finalize(),
/// The single writer of the manifest.
///
/// Runs after checks and release, holds the write credential, and is
/// master-only — a PR run never publishes, so there is nothing to finalize.
/// Being the only writer is what removes the concurrent-writer case, and with
/// it any need for a conditional put the object store may not support.
/// Coverage is not waited for; deploy takes the newest report on its own.
fn finalize() -> Result<()> {
let identity = artifact::Identity::from_env()?;
let store = publish::Store::from_env()?;
let work =
tempfile::TempDir::with_prefix("nomisync-finalize").context("cannot make a temp dir")?;
let release = store.fetch_meta(Kind::Release, &identity, work.path())?;
let local = store.verify_object(&release, &identity, work.path())?;
println!(
">> verified {} ({} bytes)",
local.file_name().unwrap_or_default().to_string_lossy(),
release.bytes
);
store.write_manifest(&[release], &identity, work.path())?;
">> manifest written for {} run {}",
identity.commit, identity.run_id
Ok(())
/// True only where the workflow decided this run may hold the artifacts key —
/// master. A PR run builds and verifies everything with no credential mounted.
fn with_artifacts() -> bool {
std::env::var("WITH_ARTIFACTS").as_deref() == Ok("1")
fn produce(kind: Kind) -> Result<()> {
// The heartbeat starts before the database, so the pulse also covers initdb
// and the migrations. Nesting it inside meant a hung startup could still go
// quiet for longer than Forgejo tolerates.
let heartbeat = Heartbeat::start();
heartbeat.phase("starting postgres");
// Both kinds need it: the coverage run executes the suite, and `cargo doc`
// recompiles the sqlx macros.
let (_termination, database) = watched_cluster()?;
let url = database.database_url();
match kind {
Kind::Coverage => coverage::run_coverage(&heartbeat, &url)?,
Kind::Release => release::build(&heartbeat, &url)?,
release::package(kind, &heartbeat, with_artifacts())
/// Watches for termination signals, then starts the cluster, so a signal that
/// arrives at any point afterwards unwinds and drops it instead of leaking it.
fn watched_cluster() -> Result<(watchdog::Termination, postgres::Postgres)> {
let termination = watchdog::Termination::watch()?;
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.context("cannot build a tokio runtime")?;
let database = runtime.block_on(postgres::start())?;
Ok((termination, database))
/// Starts Postgres, exports DATABASE_URL, and runs the given command.
/// The cluster stops when `Postgres` drops, including on the error paths.
fn with_postgres(argv: &[String]) -> Result<()> {
let (termination, database) = watched_cluster()?;
let (program, args) = argv.split_first().context("no command given")?;
watchdog::run_supervised(
program,
args,
&[("DATABASE_URL", &database.database_url())],
&termination,
)