Skip to main content

verctl_core/rules/
ci.rs

1//! Which machines run PR and push validation.
2//!
3//! A `ci` entry is one job; its `runners` name machines from `runners`, one
4//! check each. Write nothing and one `verify` job runs on `ubuntu-26.04`,
5//! which is what every repo in this family did before the section existed.
6//!
7//! GitHub needs `runs-on` before a job exists, so a job cannot read its own
8//! machine from verctl. A small `plan` job emits this matrix and the real jobs
9//! consume it, the same bootstrap the publish lane uses for assets.
10
11use std::path::Path;
12
13use anyhow::{Context, Result};
14use serde::Serialize;
15
16use crate::config::Config;
17use crate::{env, runners};
18
19const DEFAULT_JOB: &str = "verify";
20const DEFAULT_MACHINE: &str = "ubuntu-26.04";
21const DEFAULT_LABELS: &[&str] = &["ubuntu-26.04"];
22
23/// One row of a GitHub Actions `strategy.matrix`, and one check.
24#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
25pub struct CiJob {
26    /// The job as declared. Rows share it when the job fans out.
27    pub id: String,
28    /// The check name, unique across rows.
29    pub name: String,
30    /// The machine this row runs on, as declared.
31    pub machine: String,
32    /// Straight into `runs-on:`.
33    pub labels: Vec<String>,
34}
35
36/// A GitHub Actions `strategy.matrix`.
37#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
38pub struct Matrix {
39    /// One row per check.
40    pub include: Vec<CiJob>,
41}
42
43/// The validation jobs to run, as the workflow reads them.
44#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
45pub struct CiPlan {
46    /// The matrix the workflow fans out over.
47    pub matrix: Matrix,
48}
49
50/// Plan the jobs declared under `ci`, or one `verify` job when none is
51/// declared.
52///
53/// # Errors
54///
55/// Returns an error when a declared runner is invalid, or when a job names a
56/// machine that is missing, repeated, or not declared under `runners`.
57pub fn plan(config: &Config) -> Result<CiPlan> {
58    runners::declared(config)?;
59    let mut include = Vec::new();
60    if config.ci.is_empty() {
61        let resolved = machines(config, DEFAULT_JOB, None)?;
62        push(&mut include, DEFAULT_JOB, &resolved);
63        return Ok(CiPlan {
64            matrix: Matrix { include },
65        });
66    }
67    for (id, job) in &config.ci {
68        let resolved = machines(config, id, job.runners.as_ref())?;
69        push(&mut include, id, &resolved);
70    }
71    Ok(CiPlan {
72        matrix: Matrix { include },
73    })
74}
75
76fn machines(
77    config: &Config,
78    id: &str,
79    named: Option<&Vec<String>>,
80) -> Result<Vec<runners::Machine>> {
81    runners::of(config, "ci job", id, named, DEFAULT_MACHINE, DEFAULT_LABELS)
82}
83
84fn push(include: &mut Vec<CiJob>, id: &str, resolved: &[runners::Machine]) {
85    for machine in resolved {
86        include.push(CiJob {
87            id: id.to_owned(),
88            name: runners::check_name(id, &machine.name, resolved),
89            machine: machine.name.clone(),
90            labels: machine.labels.clone(),
91        });
92    }
93}
94
95/// Write `matrix` as a JSON line for `$GITHUB_OUTPUT`.
96///
97/// # Errors
98///
99/// Returns an error when the matrix cannot be encoded as JSON, or when the
100/// file at `path` cannot be opened or written.
101pub fn write_github_output(plan: &CiPlan, path: &Path) -> Result<()> {
102    let encoded = serde_json::to_string(&plan.matrix).context("encode matrix")?;
103    env::write_github_output(path, &format!("matrix={encoded}\n"))
104}
105
106#[cfg(test)]
107mod tests;