Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/src/content/docs/jobs.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ To choose a GitHub runner for a container job, set `runs-on` in the job's `githu

`needs` takes one job id or a list. Every id must be a job in the config, and a job can't need itself. Generated jobs
keep the dependencies. A job's hash includes the hashes of the jobs it needs, so it runs again whenever one of them
would (see [inputs](/docs/inputs/)).
would (see [inputs](/docs/inputs/)). A job that a matrix runs in several stages (`- { stage: us }`, `- { stage: eu }`,
with `stage_prefix` on) is needed stage by stage: a job in the `us` stage needs `us_build`, and a job in a stage `build`
has no instance in (an approval in `review`) needs every instance.

## Services

Expand Down
3 changes: 2 additions & 1 deletion internal_docs/STATUS.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ What the code does today, checked against the code in December 2026. Where cigen
extras, a workflow's becomes its workflow settings (`on`, `permissions`, `concurrency`), and the top-level block
goes into the provider's raw config.
- Job ids are unique across workflows. In a nested job, `needs: [compile]` names a sibling (`build/compile`) when one
exists.
exists. With `stage_prefix` on, a job needs the instances of a job in its own stage when there are any, and every
instance in the other stages otherwise.
- **JSON schemas** (`schemas/v1/`, generated from `src/schema/` by `src/schema/json.rs`): `cigen.schema.json` for a whole
`cigen.yml` (and `.cigen/config.yml`, `.cigen/config/*.yml`), and `workflow`, `job` and `command` schemas for the
other files of a `.cigen/` directory. They are as strict as the loader: unknown keys are errors except inside
Expand Down
72 changes: 42 additions & 30 deletions src/orchestrator/dag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,43 +84,28 @@ impl JobDAG {

let mut new_needs = BTreeSet::new();

// 2a. Explicit Dependencies
// 2a. Explicit Dependencies: an exact instance id, or every instance of a job id.
// With stage prefixing, a job takes the instances in its own stage when there are
// any, and otherwise those in every other stage (an approval stage that gates the
// builds of every region).
for needed_job_id in &concrete_job.job.needs {
let mut found_match = false;
for (candidate_id, candidate) in &jobs {
// Match exact instance ID
if candidate_id == needed_job_id {
new_needs.insert(candidate_id.clone());
graph.update_edge(node_map[candidate_id], dependent_node, ());
found_match = true;
continue;
}

// Match base job ID (scoped to same stage if prefixing active)
if &candidate.job_id == needed_job_id {
if should_prefix(&concrete_job.stage, wf_config)
&& should_prefix(&candidate.stage, wf_config)
{
if concrete_job.stage == candidate.stage {
new_needs.insert(candidate_id.clone());
graph.update_edge(node_map[candidate_id], dependent_node, ());
found_match = true;
}
} else {
// Global match (e.g. no stage prefixing)
new_needs.insert(candidate_id.clone());
graph.update_edge(node_map[candidate_id], dependent_node, ());
found_match = true;
}
}
}
if !found_match {
let matched = resolve_need(
&jobs,
&concrete_job,
needed_job_id,
should_prefix(&concrete_job.stage, wf_config),
);
if matched.is_empty() {
bail!(
"Job '{}' depends on '{}', but no matching job instance exists",
instance_id,
needed_job_id
);
}
for candidate_id in matched {
graph.update_edge(node_map[&candidate_id], dependent_node, ());
new_needs.insert(candidate_id);
}
}

// 2b. Stage Dependencies (Implicit)
Expand Down Expand Up @@ -423,6 +408,33 @@ fn expand_matrix_job(
}
}

/// The instances a `needs` entry names: the instance with that id, or every instance of that job.
/// With stage prefixing, the instances in `dependent`'s own stage win when there are any.
fn resolve_need(
jobs: &BTreeMap<String, ConcreteJob>,
dependent: &ConcreteJob,
needed: &str,
prefixed: bool,
) -> Vec<String> {
if jobs.contains_key(needed) {
return vec![needed.to_string()];
}
let all: Vec<(&String, &ConcreteJob)> = jobs
.iter()
.filter(|(_, candidate)| candidate.job_id == needed)
.collect();
let same_stage: Vec<&String> = all
.iter()
.filter(|(_, candidate)| prefixed && candidate.stage == dependent.stage)
.map(|(id, _)| *id)
.collect();
if same_stage.is_empty() {
all.into_iter().map(|(id, _)| id.clone()).collect()
} else {
same_stage.into_iter().cloned().collect()
}
}

fn should_prefix(stage: &str, config: &WorkflowConfig) -> bool {
if stage == "default" {
config.default_stage_prefix
Expand Down
68 changes: 68 additions & 0 deletions tests/stage_needs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//! `needs` with stage prefixing: a job needs the instances of a job in its own stage when there
//! are any, and otherwise every instance of that job in the other stages (an approval job in a
//! `review` stage needs the build of every region).

mod support;

use serde_yaml::Value;
use support::fixture_repo::FixtureRepo;

const CONFIG: &str = r#"provider: github
workflows:
ci:
stage_prefix: true
jobs:
build:
image: rust:latest
matrix:
- { stage: us }
- { stage: eu }
steps:
- run: make build
deploy:
image: rust:latest
needs: [build]
matrix:
- { stage: us }
- { stage: eu }
steps:
- run: make deploy
approve:
image: rust:latest
needs: [build]
matrix:
- { stage: review }
steps:
- run: echo approved
"#;

fn needs(workflow: &Value, job: &str) -> Vec<String> {
workflow["jobs"][job]["needs"]
.as_sequence()
.unwrap_or_else(|| panic!("job {job} has no needs: {workflow:?}"))
.iter()
.map(|need| need.as_str().unwrap().to_string())
.collect()
}

#[test]
fn a_job_needs_its_own_stage_first_then_every_other_stage() {
let repo = FixtureRepo::new();
repo.write("cigen.yml", CONFIG);
let output = support::cigen()
.args(["generate"])
.current_dir(repo.path())
.output()
.unwrap();
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
let text = std::fs::read_to_string(repo.path().join(".github/workflows/ci.yml")).unwrap();
let workflow: Value = serde_yaml::from_str(&text).unwrap();

assert_eq!(needs(&workflow, "us_deploy"), ["us_build"]);
assert_eq!(needs(&workflow, "eu_deploy"), ["eu_build"]);
assert_eq!(needs(&workflow, "review_approve"), ["eu_build", "us_build"]);
}
Loading