Skip to content
735 linesCodeBlameRaw
1//! Runs one GitHub Actions job, as GitHub's runner would: its steps in
2//! order, each `run` in a shell and each `uses` as the action it names,
3//! with the `${{ }}` contexts, the `GITHUB_*` variables and files, and the
4//! workflow commands steps print. It reports every step and the log to
5//! g1t as it goes.
6//!
7//! Configuration comes from the environment: `G1T_API`, and `ACTIONS_JOB`
8//! and `ACTIONS_TOKEN`, the job and its own token. Everything else, the
9//! job's definition, its contexts and its secrets, is fetched with them.
10
11mod blobs;
12mod containers;
13mod files;
14mod paths;
15mod process;
16mod report;
17mod uses;
18
19use std::collections::{BTreeMap, BTreeSet};
20use std::path::{Path, PathBuf};
21use std::process::Command;
22use std::time::{Duration, Instant};
23
24use anyhow::{Context, Result};
25use g1t_actions::events::WORKSPACE;
26use g1t_actions::expr::{self, Scope, Status};
27use serde_json::{Map, Value, json};
28
29use files::StepFiles;
30use process::{Commands, Ended};
31use report::{Api, Log};
32
33const TEMP: &str = "/home/runner/_temp";
34
35/// Who is running steps: the job itself, or a composite action inside it.
36#[derive(Clone, Default)]
37pub(crate) struct Frame {
38 /// The `steps` context.
39 pub(crate) steps: Map<String, Value>,
40 /// A composite action's `inputs`, in place of the workflow's.
41 pub(crate) inputs: Option<Value>,
42 /// A composite action's folder, for `github.action_path`.
43 pub(crate) action_path: Option<String>,
44 /// Variables a composite action's caller set for its steps.
45 pub(crate) env: BTreeMap<String, String>,
46}
47
48/// A step run when the job's steps are done: an action's `post`, or
49/// saving the cache.
50pub(crate) struct Post {
51 pub(crate) name: String,
52 pub(crate) condition: String,
53 pub(crate) env: BTreeMap<String, String>,
54 pub(crate) run: PostRun,
55}
56
57pub(crate) enum PostRun {
58 Node { action_dir: PathBuf, script: String },
59 CacheSave { key: String, paths: Vec<String> },
60 /// A Docker action's `post-entrypoint`.
61 Docker(containers::DockerRun),
62}
63
64pub(crate) struct Job {
65 pub(crate) log: Log,
66 pub(crate) spec: Value,
67 pub(crate) workspace: PathBuf,
68 pub(crate) temp: PathBuf,
69 /// This process's own variables, less its credentials, and GitHub's.
70 base_env: BTreeMap<String, String>,
71 /// Written to `GITHUB_ENV` by earlier steps.
72 added_env: BTreeMap<String, String>,
73 /// Written to `GITHUB_PATH` by earlier steps, newest first.
74 path_prepend: Vec<String>,
75 workflow_env: BTreeMap<String, String>,
76 job_env: BTreeMap<String, String>,
77 /// github, vars, secrets, inputs, matrix, needs, strategy, runner.
78 pub(crate) contexts: Map<String, Value>,
79 pub(crate) failed: bool,
80 pub(crate) posts: Vec<Post>,
81 step_names: Vec<String>,
82 deadline: Instant,
83 debug: bool,
84 /// What the last Node process left, for the step that ran it.
85 pub(crate) last_node_outputs: BTreeMap<String, String>,
86 pub(crate) last_node_state: BTreeMap<String, String>,
87 /// The names of the sandbox's own variables, which a container does
88 /// not get.
89 host_env: BTreeSet<String>,
90 /// Whether this job has a Docker Engine of its own (g1t's machines).
91 pub(crate) docker_hosted: bool,
92 /// The job's network, once its containers have one.
93 pub(crate) network: Option<String>,
94 /// `services:`, by their names, and their containers' names.
95 pub(crate) services: Vec<(String, String)>,
96 /// `container:`, once started.
97 pub(crate) container: Option<containers::JobContainer>,
98 /// The `job` context's `container` and `services`.
99 pub(crate) job_context: Map<String, Value>,
100 /// Docker actions' images built in this job.
101 pub(crate) built_actions: containers::Built,
102}
103
104fn text_map(value: Option<&Value>) -> BTreeMap<String, String> {
105 value
106 .and_then(Value::as_object)
107 .map(|map| map.iter().map(|(k, v)| (k.clone(), expr::to_text(v))).collect())
108 .unwrap_or_default()
109}
110
111/// A step's title when it has no name, as GitHub shows it.
112fn default_title(step: &Map<String, Value>) -> String {
113 if let Some(uses) = step.get("uses").and_then(Value::as_str) {
114 return format!("Run {uses}");
115 }
116 let run = step.get("run").map(expr::to_text).unwrap_or_default();
117 let first = run.lines().find(|line| !line.trim().is_empty()).unwrap_or_default().trim();
118 format!("Run {first}")
119}
120
121impl Job {
122 pub(crate) fn base_env_value(&self, name: &str) -> Option<String> {
123 self.base_env.get(name).cloned()
124 }
125
126 /// What earlier steps added to `PATH`, newest first.
127 pub(crate) fn path_prepend_entries(&self) -> &[String] {
128 &self.path_prepend
129 }
130
131 fn status(&self) -> Status {
132 if self.failed { Status::Failure } else { Status::Success }
133 }
134
135 /// The contexts an expression in a step can use.
136 pub(crate) fn contexts_for(&self, frame: &Frame, env: &BTreeMap<String, String>) -> Map<String, Value> {
137 let mut contexts = self.contexts.clone();
138 contexts.insert("env".into(), Value::Object(env.iter().map(|(k, v)| (k.clone(), Value::String(v.clone()))).collect()));
139 contexts.insert("steps".into(), Value::Object(frame.steps.clone()));
140 contexts.insert("job".into(), containers::job_context(if self.failed { "failure" } else { "success" }, &self.job_context));
141 if let Some(inputs) = &frame.inputs {
142 contexts.insert("inputs".into(), inputs.clone());
143 }
144 if let Some(path) = &frame.action_path
145 && let Some(github) = contexts.get_mut("github")
146 {
147 github["action_path"] = Value::String(path.clone());
148 }
149 contexts
150 }
151
152 /// Runs `f` with a scope over these contexts.
153 pub(crate) fn with_scope<T>(&self, contexts: &Map<String, Value>, f: impl FnOnce(&Scope) -> T) -> T {
154 let workspace = self.workspace.clone();
155 let hash = move |patterns: &[String]| files::hash_files(&workspace, patterns);
156 let scope = Scope {
157 contexts,
158 status: self.status(),
159 hash_files: Some(&hash),
160 };
161 f(&scope)
162 }
163
164 /// The `env` context for a step: the workflow's, the job's, what earlier
165 /// steps wrote to `GITHUB_ENV`, and the frame's.
166 pub(crate) fn env_context(&self, frame: &Frame) -> BTreeMap<String, String> {
167 let mut env = self.added_env.clone();
168 env.extend(self.workflow_env.clone());
169 env.extend(self.job_env.clone());
170 env.extend(frame.env.clone());
171 env
172 }
173
174 /// What a process for a step is given.
175 pub(crate) fn process_env(&self, env: &BTreeMap<String, String>, files: &StepFiles) -> BTreeMap<String, String> {
176 let mut out = self.base_env.clone();
177 out.extend(env.clone());
178 for (name, value) in files.variables() {
179 out.insert(name.to_owned(), value);
180 }
181 if !self.path_prepend.is_empty() {
182 let current = out.get("PATH").cloned().unwrap_or_default();
183 let separator = paths::PATH_SEPARATOR;
184 out.insert("PATH".into(), format!("{}{separator}{current}", self.path_prepend.join(separator)));
185 }
186 out
187 }
188
189 /// Takes in what a step wrote to its files. Returns its outputs.
190 pub(crate) fn absorb(&mut self, files: &StepFiles, commands: &Commands) -> (BTreeMap<String, String>, BTreeMap<String, String>) {
191 let mut outputs: BTreeMap<String, String> = commands.outputs.clone();
192 match files::key_values(&StepFiles::read(&files.output)) {
193 Ok(values) => outputs.extend(values),
194 Err(problem) => self.log.line(&format!("##[error]$GITHUB_OUTPUT: {problem}")),
195 }
196 match files::key_values(&StepFiles::read(&files.env)) {
197 Ok(values) => {
198 for (name, value) in values {
199 if name.starts_with("GITHUB_") || name == "NODE_OPTIONS" {
200 self.log.line(&format!("##[warning]{name} cannot be set through $GITHUB_ENV."));
201 continue;
202 }
203 self.added_env.insert(name, value);
204 }
205 }
206 Err(problem) => self.log.line(&format!("##[error]$GITHUB_ENV: {problem}")),
207 }
208 for line in StepFiles::read(&files.path).lines().map(str::trim).filter(|l| !l.is_empty()) {
209 self.path_prepend.insert(0, line.to_owned());
210 }
211 let mut state = commands.state.clone();
212 if let Ok(values) = files::key_values(&StepFiles::read(&files.state)) {
213 state.extend(values);
214 }
215 let summary = StepFiles::read(&files.summary);
216 if !summary.trim().is_empty() {
217 self.log.line("##[group]Step summary");
218 for line in summary.lines() {
219 self.log.line(line);
220 }
221 self.log.line("##[endgroup]");
222 }
223 (outputs, state)
224 }
225
226 pub(crate) fn remaining_time(&self) -> Duration {
227 self.remaining()
228 }
229
230 fn remaining(&self) -> Duration {
231 self.deadline.saturating_duration_since(Instant::now())
232 }
233
234 /// Runs a shell script for a `run` step.
235 pub(crate) fn run_script(
236 &mut self,
237 script: &str,
238 shell: Option<&str>,
239 working_directory: Option<&str>,
240 env: &BTreeMap<String, String>,
241 timeout: Duration,
242 ) -> (bool, BTreeMap<String, String>, BTreeMap<String, String>) {
243 crate::abuse::touch();
244 // Mining is never a workflow's job (abuse.rs).
245 if let Some(miner) = crate::abuse::miner_in(script) {
246 self.log.line(&format!("##[error]g1t does not run cryptocurrency miners ({miner}). This step was not run."));
247 return (false, BTreeMap::new(), BTreeMap::new());
248 }
249 let id = format!("{:x}", rand_id());
250 let shell = shell.map(str::trim).filter(|s| !s.is_empty());
251 let (program, args, extension): (String, Vec<String>, &str) = match shell {
252 // A self-hosted Windows runner, as GitHub's: PowerShell.
253 None if cfg!(windows) => (windows_powershell(), powershell_args(), "ps1"),
254 // A job container without bash, as GitHub's runner does.
255 None if self.container.as_ref().is_some_and(|c| c.shell == "sh") => ("sh".into(), vec!["-e".into(), "{0}".into()], "sh"),
256 None => ("bash".into(), vec!["-e".into(), "{0}".into()], "sh"),
257 Some("bash") => ("bash".into(), vec!["--noprofile".into(), "--norc".into(), "-eo".into(), "pipefail".into(), "{0}".into()], "sh"),
258 Some("sh") => ("sh".into(), vec!["-e".into(), "{0}".into()], "sh"),
259 Some("python") => ("python3".into(), vec!["{0}".into()], "py"),
260 Some("pwsh") if cfg!(windows) || program_exists("pwsh") => ("pwsh".into(), powershell_args(), "ps1"),
261 Some("powershell") if cfg!(windows) => ("powershell".into(), powershell_args(), "ps1"),
262 Some("cmd") if cfg!(windows) => (
263 "cmd".into(),
264 vec!["/D".into(), "/E:ON".into(), "/V:OFF".into(), "/S".into(), "/C".into(), "CALL \"{0}\"".into()],
265 "cmd",
266 ),
267 Some(other @ ("pwsh" | "powershell" | "cmd")) => {
268 self.log.line(&format!(
269 "##[error]`shell: {other}` needs Windows or PowerShell, which g1t's Linux runners do not have. A self-hosted Windows runner can run it: `runs-on: [self-hosted, windows]`."
270 ));
271 return (false, BTreeMap::new(), BTreeMap::new());
272 }
273 Some(custom) => {
274 let mut parts = custom.split_whitespace().map(str::to_owned);
275 let program = parts.next().unwrap_or_default();
276 let mut args: Vec<String> = parts.collect();
277 if !args.iter().any(|a| a.contains("{0}")) {
278 args.push("{0}".into());
279 }
280 (program, args, "sh")
281 }
282 };
283 let script_path = self.temp.join(format!("{id}.{extension}"));
284 if let Err(error) = std::fs::write(&script_path, script) {
285 self.log.line(&format!("##[error]Could not write the script: {error}"));
286 return (false, BTreeMap::new(), BTreeMap::new());
287 }
288 let files = match StepFiles::new(&self.temp, &id) {
289 Ok(files) => files,
290 Err(error) => {
291 self.log.line(&format!("##[error]Could not make the step's files: {error}"));
292 return (false, BTreeMap::new(), BTreeMap::new());
293 }
294 };
295 let args: Vec<String> = args.iter().map(|a| a.replace("{0}", &paths::shown(&script_path))).collect();
296 self.log.line(&format!("shell: {program} {}", args.join(" ")));
297 let dir = match working_directory {
298 Some(dir) if Path::new(dir).is_absolute() => PathBuf::from(dir),
299 Some(dir) => self.workspace.join(dir),
300 None => self.workspace.clone(),
301 };
302 let full = self.process_env(env, &files);
303 let in_container = self.container.as_ref().map(|c| c.path.clone()).and_then(|image_path| {
304 let inside = self.container_env(env, full.clone(), &image_path);
305 self.in_container(&program, &args, &dir, inside)
306 });
307 let command = match in_container {
308 Some(command) => command,
309 None => {
310 let mut command = Command::new(&program);
311 command.args(&args).current_dir(&dir).env_clear().envs(full);
312 command
313 }
314 };
315 let mut commands = Commands {
316 debug: self.debug,
317 ..Commands::default()
318 };
319 let ended = process::run(command, timeout.min(self.remaining()), &mut self.log, &mut commands);
320 let ok = match ended {
321 Ok(Ended::Exited(0)) => true,
322 Ok(Ended::Exited(code)) => {
323 self.log.line(&format!("##[error]Process completed with exit code {code}."));
324 false
325 }
326 Ok(Ended::TimedOut) => {
327 self.log.line("##[error]The step ran past its time limit and was stopped.");
328 false
329 }
330 Err(error) => {
331 self.log.line(&format!("##[error]{program} could not be started: {error}"));
332 false
333 }
334 };
335 let (outputs, state) = self.absorb(&files, &commands);
336 (ok, outputs, state)
337 }
338
339 /// Runs one step of a frame. Returns whether it succeeded (its
340 /// conclusion). `number` is the step the log belongs to.
341 pub(crate) fn step(&mut self, frame: &mut Frame, step: &Map<String, Value>, number: u32, report: bool, defaults: &Map<String, Value>) -> bool {
342 let env_before = self.env_context(frame);
343 let contexts = self.contexts_for(frame, &env_before);
344 let title = match step.get("name").map(expr::to_text) {
345 Some(name) => self.with_scope(&contexts, |scope| expr::interpolate(&name, scope)).unwrap_or(name),
346 None => default_title(step),
347 };
348 let condition = step.get("if").map(expr::to_text).unwrap_or_default();
349 let run_it = match self.with_scope(&contexts, |scope| expr::condition(&condition, scope)) {
350 Ok(run_it) => run_it,
351 Err(problem) => {
352 self.log.line(&format!("##[error]The step's `if` does not read: {problem}"));
353 self.failed = true;
354 if report {
355 self.log.step_state(number, &title, "completed", Some("failure"));
356 }
357 return false;
358 }
359 };
360 let id = step.get("id").map(expr::to_text);
361 if !run_it {
362 if let Some(id) = &id {
363 frame.steps.insert(id.clone(), json!({ "outputs": {}, "outcome": "skipped", "conclusion": "skipped" }));
364 }
365 if report {
366 self.log.step_state(number, &title, "completed", Some("skipped"));
367 }
368 return true;
369 }
370 if report {
371 self.log.step(number);
372 self.log.step_state(number, &title, "in_progress", None);
373 }
374
375 // The step's own env, read with the contexts before it.
376 let mut env = env_before.clone();
377 if let Some(Value::Object(step_env)) = step.get("env") {
378 for (name, value) in step_env {
379 let value = self.with_scope(&contexts, |scope| expr::interpolate_value(value, scope)).unwrap_or(Value::Null);
380 env.insert(name.clone(), expr::to_text(&value));
381 }
382 }
383 let timeout = step
384 .get("timeout-minutes")
385 .and_then(|v| self.with_scope(&contexts, |scope| expr::interpolate_value(v, scope)).ok())
386 .and_then(|v| v.as_f64().or_else(|| expr::to_text(&v).parse().ok()))
387 .map_or(Duration::from_secs(6 * 3600), |minutes| Duration::from_secs_f64(minutes * 60.0));
388 let continue_on_error = step
389 .get("continue-on-error")
390 .and_then(|v| self.with_scope(&contexts, |scope| expr::interpolate_value(v, scope)).ok())
391 .is_some_and(|v| expr::truthy(&v));
392
393 let (ok, outputs) = if let Some(run) = step.get("run").map(expr::to_text) {
394 let script = match self.with_scope(&contexts, |scope| expr::interpolate(&run, scope)) {
395 Ok(script) => script,
396 Err(problem) => {
397 self.log.line(&format!("##[error]The script does not read: {problem}"));
398 String::new()
399 }
400 };
401 self.log.line(&format!("##[group]{title}"));
402 for line in script.lines() {
403 self.log.line(line);
404 }
405 self.log.line("##[endgroup]");
406 let shell = step
407 .get("shell")
408 .map(expr::to_text)
409 .or_else(|| defaults.get("shell").map(expr::to_text));
410 if frame.action_path.is_some() && shell.is_none() {
411 self.log.line("##[error]A composite action's `run` steps need a `shell`.");
412 (false, BTreeMap::new())
413 } else {
414 let working_directory = step
415 .get("working-directory")
416 .or_else(|| defaults.get("working-directory"))
417 .map(|v| self.with_scope(&contexts, |scope| expr::interpolate(&expr::to_text(v), scope)).unwrap_or_else(|_| expr::to_text(v)));
418 let mut env = env;
419 if let Some(path) = &frame.action_path {
420 env.insert("GITHUB_ACTION_PATH".into(), path.clone());
421 }
422 let (ok, outputs, _) = self.run_script(&script, shell.as_deref(), working_directory.as_deref(), &env, timeout);
423 (ok, outputs)
424 }
425 } else if let Some(uses) = step.get("uses").map(expr::to_text) {
426 let with: BTreeMap<String, String> = match step.get("with") {
427 Some(Value::Object(with)) => with
428 .iter()
429 .map(|(k, v)| {
430 let value = self.with_scope(&contexts, |scope| expr::interpolate_value(v, scope)).unwrap_or(Value::Null);
431 (k.clone(), expr::to_text(&value))
432 })
433 .collect(),
434 _ => BTreeMap::new(),
435 };
436 self.uses(&uses, &with, &env, frame, &title, id.as_deref(), timeout)
437 } else {
438 self.log.line("##[error]A step needs `run` or `uses`.");
439 (false, BTreeMap::new())
440 };
441
442 let outcome = if ok { "success" } else { "failure" };
443 let conclusion = if ok || continue_on_error { "success" } else { "failure" };
444 if !ok && continue_on_error {
445 self.log.line("##[warning]The step failed, and `continue-on-error` lets the job go on.");
446 }
447 if let Some(id) = &id {
448 let outputs: Map<String, Value> = outputs.iter().map(|(k, v)| (k.clone(), Value::String(v.clone()))).collect();
449 frame.steps.insert(id.clone(), json!({ "outputs": outputs, "outcome": outcome, "conclusion": conclusion }));
450 }
451 if conclusion == "failure" {
452 self.failed = true;
453 }
454 if report {
455 self.log.step_state(number, &title, "completed", Some(conclusion));
456 }
457 conclusion == "success"
458 }
459
460 fn report_steps(&self) {
461 self.log.steps(&self.step_names);
462 }
463}
464
465/// PowerShell on Windows: `pwsh` (PowerShell 7) if it is installed, as on
466/// GitHub's Windows runners, else Windows PowerShell.
467fn windows_powershell() -> String {
468 if program_exists("pwsh") { "pwsh".into() } else { "powershell".into() }
469}
470
471/// How GitHub runs a PowerShell step: the script, stopping at the first error.
472fn powershell_args() -> Vec<String> {
473 vec!["-NoLogo".into(), "-NoProfile".into(), "-NonInteractive".into(), "-Command".into(), ". '{0}'".into()]
474}
475
476/// Whether `program` is on `PATH`.
477fn program_exists(program: &str) -> bool {
478 Command::new(program)
479 .arg(if program == "cmd" { "/C" } else { "-Version" })
480 .stdout(std::process::Stdio::null())
481 .stderr(std::process::Stdio::null())
482 .status()
483 .is_ok()
484}
485
486/// An id for files, unique enough within one job.
487fn rand_id() -> u64 {
488 use std::sync::atomic::{AtomicU64, Ordering};
489 static NEXT: AtomicU64 = AtomicU64::new(1);
490 let nanos = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_nanos() as u64).unwrap_or(0);
491 nanos ^ (NEXT.fetch_add(1, Ordering::Relaxed) << 48)
492}
493
494fn interpolated_map(job: &Job, value: Option<&Value>, contexts: &Map<String, Value>) -> BTreeMap<String, String> {
495 let mut out = BTreeMap::new();
496 if let Some(Value::Object(map)) = value {
497 for (name, value) in map {
498 let value = job.with_scope(contexts, |scope| expr::interpolate_value(value, scope)).unwrap_or(Value::Null);
499 out.insert(name.clone(), expr::to_text(&value));
500 }
501 }
502 out
503}
504
505fn setup(mut spec: Value, api: Api) -> Result<Job> {
506 let masks: Vec<String> = spec["masks"].as_array().map(|m| m.iter().filter_map(|v| v.as_str().map(str::to_owned)).collect()).unwrap_or_default();
507 let log = Log::new(api, masks);
508 // On a self-hosted runner's own machine, GitHub's layout lives in a
509 // folder of the runner's (paths.rs).
510 for part in ["variables", "github"] {
511 paths::relocate(&mut spec[part]);
512 }
513 paths::relocate(&mut spec["contexts"]["runner"]);
514 let workspace = paths::under_home(WORKSPACE);
515 let temp = paths::under_home(TEMP);
516 std::fs::create_dir_all(&workspace).context("could not make the workspace")?;
517 std::fs::create_dir_all(&temp).context("could not make the temporary folder")?;
518 std::fs::write(temp.join("event.json"), serde_json::to_string_pretty(&spec["event"])?)?;
519
520 let host_env: BTreeSet<String> = std::env::vars().map(|(name, _)| name).collect();
521 // Docker of the job's own, on g1t's machines (crate::docker).
522 let docker_hosted = cfg!(target_os = "linux")
523 && std::env::var("G1T_DOCKER").as_deref() == Ok("on")
524 && spec["variables"]["RUNNER_ENVIRONMENT"].as_str() != Some("self-hosted");
525 // This process's environment, less what only it should see.
526 let mut base_env: BTreeMap<String, String> =
527 std::env::vars().filter(|(name, _)| !matches!(name.as_str(), "ACTIONS_TOKEN" | "ACTIONS_JOB" | "MODE") && !name.starts_with("G1T_")).collect();
528 base_env.insert("HOME".into(), std::env::var("HOME").unwrap_or_else(|_| "/home/node".into()));
529 base_env.extend(text_map(spec.get("variables")));
530 base_env.insert("GITHUB_EVENT_PATH".into(), paths::shown(&temp.join("event.json")));
531
532 let mut contexts: Map<String, Value> = spec["contexts"].as_object().cloned().unwrap_or_default();
533 contexts.insert("github".into(), spec["github"].clone());
534 let debug = contexts
535 .get("secrets")
536 .and_then(|s| s.get("ACTIONS_STEP_DEBUG"))
537 .or_else(|| contexts.get("vars").and_then(|v| v.get("ACTIONS_STEP_DEBUG")))
538 .is_some_and(|v| expr::to_text(v) == "true");
539
540 // `timeoutMinutes` is how the API spelled it before its bodies were
541 // `snake_case`.
542 let timeout = spec["timeout_minutes"]
543 .as_u64()
544 .or_else(|| spec["timeoutMinutes"].as_u64())
545 .unwrap_or(60);
546 let mut job = Job {
547 log,
548 spec,
549 workspace,
550 temp,
551 base_env,
552 added_env: BTreeMap::new(),
553 path_prepend: Vec::new(),
554 workflow_env: BTreeMap::new(),
555 job_env: BTreeMap::new(),
556 contexts,
557 failed: false,
558 posts: Vec::new(),
559 step_names: Vec::new(),
560 deadline: Instant::now() + Duration::from_secs(timeout * 60),
561 debug,
562 last_node_outputs: BTreeMap::new(),
563 last_node_state: BTreeMap::new(),
564 host_env,
565 docker_hosted,
566 network: None,
567 services: Vec::new(),
568 container: None,
569 job_context: Map::new(),
570 built_actions: containers::Built::new(),
571 };
572
573 // The workflow's env reads github, secrets, inputs and vars; the job's
574 // also its matrix, needs and strategy.
575 let mut contexts = job.contexts.clone();
576 contexts.insert("env".into(), json!({}));
577 job.workflow_env = interpolated_map(&job, job.spec["workflow"].get("env"), &contexts);
578 contexts.insert("env".into(), Value::Object(job.workflow_env.iter().map(|(k, v)| (k.clone(), json!(v))).collect()));
579 job.job_env = interpolated_map(&job, job.spec["spec"].get("env"), &contexts);
580 Ok(job)
581}
582
583/// The job's `defaults.run`, its own over the workflow's.
584fn run_defaults(spec: &Value) -> Map<String, Value> {
585 let mut defaults = spec["workflow"]["defaults"]["run"].as_object().cloned().unwrap_or_default();
586 if let Some(own) = spec["spec"]["defaults"]["run"].as_object() {
587 defaults.extend(own.clone());
588 }
589 defaults
590}
591
592fn run_job(job: &mut Job) {
593 let steps: Vec<Map<String, Value>> = job.spec["spec"]["steps"]
594 .as_array()
595 .map(|steps| steps.iter().filter_map(|s| s.as_object().cloned()).collect())
596 .unwrap_or_default();
597 let defaults = run_defaults(&job.spec);
598
599 // Step names as they read before anything has run.
600 let frame = Frame::default();
601 let env = job.env_context(&frame);
602 let contexts = job.contexts_for(&frame, &env);
603 job.step_names = steps
604 .iter()
605 .map(|step| match step.get("name").map(expr::to_text) {
606 Some(name) => job.with_scope(&contexts, |scope| expr::interpolate(&name, scope)).unwrap_or(name),
607 None => default_title(step),
608 })
609 .collect();
610 job.report_steps();
611
612 job.log.step(0);
613 job.log.line(&format!("Job: {}", job.spec["name"].as_str().unwrap_or_default()));
614 let variables = &job.spec["variables"];
615 if variables["RUNNER_ENVIRONMENT"] == "self-hosted" {
616 let text = |name: &str| variables[name].as_str().unwrap_or_default().to_owned();
617 job.log.line(&format!("Runner: {}, self-hosted, {} {}", text("RUNNER_NAME"), text("RUNNER_OS"), text("RUNNER_ARCH")));
618 } else {
619 job.log.line("Runner: g1t, Linux X64 (Debian bookworm, Node 24, Python 3, Go, Rust)");
620 }
621 if let Some(Value::Object(matrix)) = job.contexts.get("matrix")
622 && !matrix.is_empty()
623 {
624 job.log.line(&format!("Matrix: {}", serde_json::to_string(matrix).unwrap_or_default()));
625 }
626 if job.docker_hosted {
627 let registry = job.contexts["github"]["server_url"].as_str().and_then(crate::docker::engine::registry_host);
628 let token = job.contexts.get("secrets").and_then(|s| s.get("G1T_TOKEN")).map(expr::to_text).filter(|t| !t.is_empty());
629 let options = crate::docker::engine::Options { registry: registry.zip(token) };
630 if let Err(problem) = crate::docker::engine::enable(options) {
631 job.log.line(&format!("##[warning]Docker is not available in this job: {problem}"));
632 job.docker_hosted = false;
633 }
634 }
635 let containers_started = job.start_containers();
636 job.log.flush();
637
638 let mut frame = Frame::default();
639 if !containers_started {
640 job.failed = true;
641 for (index, name) in job.step_names.clone().iter().enumerate() {
642 job.log.step_state(index as u32 + 1, name, "completed", Some("skipped"));
643 }
644 }
645 for (index, step) in steps.iter().enumerate().filter(|_| containers_started) {
646 job.step(&mut frame, step, index as u32 + 1, true, &defaults);
647 job.log_docker_notes();
648 if job.remaining().is_zero() {
649 job.log.line("##[error]The job ran past its time limit.");
650 job.failed = true;
651 break;
652 }
653 }
654
655 // Post steps, last registered first.
656 let posts: Vec<Post> = std::mem::take(&mut job.posts);
657 for post in posts.into_iter().rev() {
658 let number = job.step_names.len() as u32 + 1;
659 job.step_names.push(post.name.clone());
660 job.report_steps();
661 let contexts = job.contexts_for(&frame, &job.env_context(&frame));
662 let run_it = job.with_scope(&contexts, |scope| expr::condition(&post.condition, scope)).unwrap_or(true);
663 if !run_it {
664 job.log.step_state(number, &post.name, "completed", Some("skipped"));
665 continue;
666 }
667 job.log.step(number);
668 job.log.step_state(number, &post.name, "in_progress", None);
669 let ok = match &post.run {
670 PostRun::Node { action_dir, script } => job.run_node(action_dir, script, &post.env),
671 PostRun::CacheSave { key, paths } => job.cache_save(key, paths),
672 PostRun::Docker(run) => job.run_docker(run).0,
673 };
674 job.log.step_state(number, &post.name, "completed", Some(if ok { "success" } else { "failure" }));
675 if !ok {
676 job.failed = true;
677 }
678 }
679
680 // GitHub's "Stop containers": services' logs, and everything removed.
681 if job.has_containers() {
682 let number = job.step_names.len() as u32 + 1;
683 job.step_names.push("Stop containers".into());
684 job.report_steps();
685 job.log.step(number);
686 job.log.step_state(number, "Stop containers", "in_progress", None);
687 job.stop_containers();
688 job.log.step_state(number, "Stop containers", "completed", Some("success"));
689 }
690
691 // The job's outputs, read now that every step has run.
692 let env = job.env_context(&frame);
693 let contexts = job.contexts_for(&frame, &env);
694 let mut outputs = Map::new();
695 if let Some(Value::Object(declared)) = job.spec["spec"].get("outputs") {
696 for (name, value) in declared {
697 let value = job.with_scope(&contexts, |scope| expr::interpolate_value(value, scope)).unwrap_or(Value::Null);
698 outputs.insert(name.clone(), Value::String(expr::to_text(&value)));
699 }
700 }
701 let conclusion = if job.failed { "failure" } else { "success" };
702 job.log.done(conclusion, &outputs, None);
703}
704
705pub(crate) fn main() -> i32 {
706 let api = match (crate::env("G1T_API"), crate::env("ACTIONS_JOB"), crate::env("ACTIONS_TOKEN")) {
707 (Ok(base), Ok(job), Ok(token)) => Api { base, job, token },
708 _ => {
709 eprintln!("g1t-runner: G1T_API, ACTIONS_JOB and ACTIONS_TOKEN are needed");
710 return 2;
711 }
712 };
713 let spec = match api.spec() {
714 Ok(spec) => spec,
715 Err(error) => {
716 eprintln!("g1t-runner: could not fetch the job: {error:#}");
717 api.report(json!({ "kind": "done", "conclusion": "failure", "reason": format!("The runner could not fetch the job: {error}") }));
718 return 1;
719 }
720 };
721 let reporter = Api {
722 base: api.base.clone(),
723 job: api.job.clone(),
724 token: api.token.clone(),
725 };
726 let mut job = match setup(spec, reporter) {
727 Ok(job) => job,
728 Err(error) => {
729 api.report(json!({ "kind": "done", "conclusion": "failure", "reason": format!("The runner could not set up: {error:#}") }));
730 return 1;
731 }
732 };
733 run_job(&mut job);
734 if job.failed { 1 } else { 0 }
735}