// Oxygen's own pipeline (OxygenFlow, docs in the oxygenflow repository).
// Every push to a bookmark on OxygenHub runs it in Firecracker microVMs on
// Earth; the jobs appear as checks on the change.
schema-version 1

// The OxygenFlow build image: Rust with rustfmt and clippy, Node, and every
// crate of Cargo.lock vendored (oxygenflow/images/rust). Rebuild and re-pin
// it when Cargo.lock changes.
sandbox "rust" {
    runtime "microvm"
    rootfs "cas://blob/affd50628428966adc46670047468d4471cd589b4b92ef889981b2587a4eeed1"
    memory-limit "8GiB"
    cpu-limit 4
    // Tests serve and connect on 127.0.0.1; nothing leaves the guest.
    network "loopback"
    // CI builds are thrown away: no incremental state and no debug info,
    // so less to compile, link and write (as in oxygenhub's pipeline).
    env {
        CARGO_INCREMENTAL "0"
        CARGO_PROFILE_DEV_DEBUG "0"
        CARGO_PROFILE_TEST_DEBUG "0"
        CARGO_PROFILE_RELEASE_DEBUG "0"
    }
}

sandbox "rust-small" {
    runtime "microvm"
    rootfs "cas://blob/affd50628428966adc46670047468d4471cd589b4b92ef889981b2587a4eeed1"
    memory-limit "1GiB"
    cpu-limit 1
    network "isolated"
}

job "fmt" {
    sandbox "rust-small"
    exec "cargo" "fmt" "--all" "--check"
}

job "clippy" {
    sandbox "rust"
    depends-on "fmt"
    mount {
        source "ephemeral://disk"
        target "/src/target"
        size "24GiB"
    }
    exec "sh" ".flow/warm-target.sh" "cargo" "clippy" "--workspace" "--all-targets" "--locked" "--" "-D" "warnings"
}

job "test" {
    sandbox "rust"
    depends-on "fmt"
    mount {
        source "ephemeral://disk"
        target "/src/target"
        size "24GiB"
    }
    exec "sh" ".flow/warm-target.sh" "cargo" "test" "--workspace" "--locked"
}

job "build" {
    sandbox "rust"
    depends-on "fmt"
    mount {
        source "ephemeral://disk"
        target "/src/target"
        size "24GiB"
    }
    exec "sh" ".flow/warm-target.sh" "cargo" "build" "--release" "--locked"
    // The binary, kept as a blob of this repository and named by the
    // job's signed receipt; the run page links it.
    output {
        artifact "oxy" {
            path "/src/target/release/oxy"
        }
    }
}

// Release files for every platform (oxygen#59): scripts/dist.sh in the
// build image's cross toolchains. A push runs it only when it touches
// these paths (a release moves Cargo.toml's version); "Run pipeline" and
// reruns always do. The files are kept as this run's artifacts, named by
// its signed receipt, and attached to the release from there, so the
// release's trust page shows each as built here.
job "dist" {
    sandbox "rust"
    depends-on "fmt"
    scope "Cargo.toml" "scripts/dist.sh" "packaging/**"
    mount {
        source "ephemeral://disk"
        target "/src/target"
        size "24GiB"
    }
    env {
        DIST_FLAT "/src/target/dist-flat"
        ZIG_GLOBAL_CACHE_DIR "/src/target/zig-cache"
    }
    exec "bash" "scripts/dist.sh"
    output {
        artifact "oxy-linux-x86_64.tar.gz" {
            path "/src/target/dist-flat/oxy-linux-x86_64.tar.gz"
        }
        artifact "oxy-linux-arm64.tar.gz" {
            path "/src/target/dist-flat/oxy-linux-arm64.tar.gz"
        }
        artifact "oxy-linux-riscv64.tar.gz" {
            path "/src/target/dist-flat/oxy-linux-riscv64.tar.gz"
        }
        artifact "oxy-macos-x86_64.tar.gz" {
            path "/src/target/dist-flat/oxy-macos-x86_64.tar.gz"
        }
        artifact "oxy-macos-arm64.tar.gz" {
            path "/src/target/dist-flat/oxy-macos-arm64.tar.gz"
        }
        artifact "oxy-windows-x86_64.zip" {
            path "/src/target/dist-flat/oxy-windows-x86_64.zip"
        }
        artifact "oxy_amd64.deb" {
            path "/src/target/dist-flat/oxy_amd64.deb"
        }
        artifact "oxy_arm64.deb" {
            path "/src/target/dist-flat/oxy_arm64.deb"
        }
        artifact "oxy_riscv64.deb" {
            path "/src/target/dist-flat/oxy_riscv64.deb"
        }
        artifact "SHA256SUMS" {
            path "/src/target/dist-flat/SHA256SUMS"
        }
    }
}
