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