flagon-io/g1t

public

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

g1t/crates/runner/src/actions/mod.rs

601 lines25,807 bytesCodeBlame
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 files;
13mod process;
14mod report;
15mod uses;
16
17use std::collections::BTreeMap;
18use std::path::{Path, PathBuf};
19use std::process::Command;
20use std::time::{Duration, Instant};
21
22use anyhow::{Context, Result};
23use g1t_actions::events::WORKSPACE;
24use g1t_actions::expr::{self, Scope, Status};
25use serde_json::{Map, Value, json};
26
27use files::StepFiles;
28use process::{Commands, Ended};
29use report::{Api, Log};
30
31const TEMP: &str = "/home/runner/_temp";
32
33/// Who is running steps: the job itself, or a composite action inside it.
34#[derive(Clone, Default)]
35pub(crate) struct Frame {
36 /// The `steps` context.
37 pub(crate) steps: Map<String, Value>,
38 /// A composite action's `inputs`, in place of the workflow's.
39 pub(crate) inputs: Option<Value>,
40 /// A composite action's folder, for `github.action_path`.
41 pub(crate) action_path: Option<String>,
42 /// Variables a composite action's caller set for its steps.
43 pub(crate) env: BTreeMap<String, String>,
44}
45
46/// A step run when the job's steps are done: an action's `post`, or
47/// saving the cache.
48pub(crate) struct Post {
49 pub(crate) name: String,
50 pub(crate) condition: String,
51 pub(crate) env: BTreeMap<String, String>,
52 pub(crate) run: PostRun,
53}
54
55pub(crate) enum PostRun {
56 Node { action_dir: PathBuf, script: String },
57 CacheSave { key: String, paths: Vec<String> },
58}
59
60pub(crate) struct Job {
61 pub(crate) log: Log,
62 pub(crate) spec: Value,
63 pub(crate) workspace: PathBuf,
64 pub(crate) temp: PathBuf,
65 /// This process's own variables, less its credentials, and GitHub's.
66 base_env: BTreeMap<String, String>,
67 /// Written to `GITHUB_ENV` by earlier steps.
68 added_env: BTreeMap<String, String>,
69 /// Written to `GITHUB_PATH` by earlier steps, newest first.
70 path_prepend: Vec<String>,
71 workflow_env: BTreeMap<String, String>,
72 job_env: BTreeMap<String, String>,
73 /// github, vars, secrets, inputs, matrix, needs, strategy, runner.
74 pub(crate) contexts: Map<String, Value>,
75 pub(crate) failed: bool,
76 pub(crate) posts: Vec<Post>,
77 step_names: Vec<String>,
78 deadline: Instant,
79 debug: bool,
80 /// What the last Node process left, for the step that ran it.
81 pub(crate) last_node_outputs: BTreeMap<String, String>,
82 pub(crate) last_node_state: BTreeMap<String, String>,
83}
84
85fn text_map(value: Option<&Value>) -> BTreeMap<String, String> {
86 value
87 .and_then(Value::as_object)
88 .map(|map| map.iter().map(|(k, v)| (k.clone(), expr::to_text(v))).collect())
89 .unwrap_or_default()
90}
91
92/// A step's title when it has no name, as GitHub shows it.
93fn default_title(step: &Map<String, Value>) -> String {
94 if let Some(uses) = step.get("uses").and_then(Value::as_str) {
95 return format!("Run {uses}");
96 }
97 let run = step.get("run").map(expr::to_text).unwrap_or_default();
98 let first = run.lines().find(|line| !line.trim().is_empty()).unwrap_or_default().trim();
99 format!("Run {first}")
100}
101
102impl Job {
103 pub(crate) fn base_env_value(&self, name: &str) -> Option<String> {
104 self.base_env.get(name).cloned()
105 }
106
107 fn status(&self) -> Status {
108 if self.failed { Status::Failure } else { Status::Success }
109 }
110
111 /// The contexts an expression in a step can use.
112 pub(crate) fn contexts_for(&self, frame: &Frame, env: &BTreeMap<String, String>) -> Map<String, Value> {
113 let mut contexts = self.contexts.clone();
114 contexts.insert("env".into(), Value::Object(env.iter().map(|(k, v)| (k.clone(), Value::String(v.clone()))).collect()));
115 contexts.insert("steps".into(), Value::Object(frame.steps.clone()));
116 contexts.insert("job".into(), json!({ "status": if self.failed { "failure" } else { "success" } }));
117 if let Some(inputs) = &frame.inputs {
118 contexts.insert("inputs".into(), inputs.clone());
119 }
120 if let Some(path) = &frame.action_path
121 && let Some(github) = contexts.get_mut("github")
122 {
123 github["action_path"] = Value::String(path.clone());
124 }
125 contexts
126 }
127
128 /// Runs `f` with a scope over these contexts.
129 pub(crate) fn with_scope<T>(&self, contexts: &Map<String, Value>, f: impl FnOnce(&Scope) -> T) -> T {
130 let workspace = self.workspace.clone();
131 let hash = move |patterns: &[String]| files::hash_files(&workspace, patterns);
132 let scope = Scope {
133 contexts,
134 status: self.status(),
135 hash_files: Some(&hash),
136 };
137 f(&scope)
138 }
139
140 /// The `env` context for a step: the workflow's, the job's, what earlier
141 /// steps wrote to `GITHUB_ENV`, and the frame's.
142 fn env_context(&self, frame: &Frame) -> BTreeMap<String, String> {
143 let mut env = self.added_env.clone();
144 env.extend(self.workflow_env.clone());
145 env.extend(self.job_env.clone());
146 env.extend(frame.env.clone());
147 env
148 }
149
150 /// What a process for a step is given.
151 pub(crate) fn process_env(&self, env: &BTreeMap<String, String>, files: &StepFiles) -> BTreeMap<String, String> {
152 let mut out = self.base_env.clone();
153 out.extend(env.clone());
154 for (name, value) in files.variables() {
155 out.insert(name.to_owned(), value);
156 }
157 if !self.path_prepend.is_empty() {
158 let current = out.get("PATH").cloned().unwrap_or_default();
159 out.insert("PATH".into(), format!("{}:{current}", self.path_prepend.join(":")));
160 }
161 out
162 }
163
164 /// Takes in what a step wrote to its files. Returns its outputs.
165 pub(crate) fn absorb(&mut self, files: &StepFiles, commands: &Commands) -> (BTreeMap<String, String>, BTreeMap<String, String>) {
166 let mut outputs: BTreeMap<String, String> = commands.outputs.clone();
167 match files::key_values(&StepFiles::read(&files.output)) {
168 Ok(values) => outputs.extend(values),
169 Err(problem) => self.log.line(&format!("##[error]$GITHUB_OUTPUT: {problem}")),
170 }
171 match files::key_values(&StepFiles::read(&files.env)) {
172 Ok(values) => {
173 for (name, value) in values {
174 if name.starts_with("GITHUB_") || name == "NODE_OPTIONS" {
175 self.log.line(&format!("##[warning]{name} cannot be set through $GITHUB_ENV."));
176 continue;
177 }
178 self.added_env.insert(name, value);
179 }
180 }
181 Err(problem) => self.log.line(&format!("##[error]$GITHUB_ENV: {problem}")),
182 }
183 for line in StepFiles::read(&files.path).lines().map(str::trim).filter(|l| !l.is_empty()) {
184 self.path_prepend.insert(0, line.to_owned());
185 }
186 let mut state = commands.state.clone();
187 if let Ok(values) = files::key_values(&StepFiles::read(&files.state)) {
188 state.extend(values);
189 }
190 let summary = StepFiles::read(&files.summary);
191 if !summary.trim().is_empty() {
192 self.log.line("##[group]Step summary");
193 for line in summary.lines() {
194 self.log.line(line);
195 }
196 self.log.line("##[endgroup]");
197 }
198 (outputs, state)
199 }
200
201 pub(crate) fn remaining_time(&self) -> Duration {
202 self.remaining()
203 }
204
205 fn remaining(&self) -> Duration {
206 self.deadline.saturating_duration_since(Instant::now())
207 }
208
209 /// Runs a shell script for a `run` step.
210 pub(crate) fn run_script(
211 &mut self,
212 script: &str,
213 shell: Option<&str>,
214 working_directory: Option<&str>,
215 env: &BTreeMap<String, String>,
216 timeout: Duration,
217 ) -> (bool, BTreeMap<String, String>, BTreeMap<String, String>) {
218 let id = format!("{:x}", rand_id());
219 let shell = shell.map(str::trim).filter(|s| !s.is_empty());
220 let (program, args, extension): (String, Vec<String>, &str) = match shell {
221 None => ("bash".into(), vec!["-e".into(), "{0}".into()], "sh"),
222 Some("bash") => ("bash".into(), vec!["--noprofile".into(), "--norc".into(), "-eo".into(), "pipefail".into(), "{0}".into()], "sh"),
223 Some("sh") => ("sh".into(), vec!["-e".into(), "{0}".into()], "sh"),
224 Some("python") => ("python3".into(), vec!["{0}".into()], "py"),
225 Some(other @ ("pwsh" | "powershell" | "cmd")) => {
226 self.log.line(&format!("##[error]`shell: {other}` needs Windows or PowerShell, which g1t's Linux runners do not have."));
227 return (false, BTreeMap::new(), BTreeMap::new());
228 }
229 Some(custom) => {
230 let mut parts = custom.split_whitespace().map(str::to_owned);
231 let program = parts.next().unwrap_or_default();
232 let mut args: Vec<String> = parts.collect();
233 if !args.iter().any(|a| a.contains("{0}")) {
234 args.push("{0}".into());
235 }
236 (program, args, "sh")
237 }
238 };
239 let script_path = self.temp.join(format!("{id}.{extension}"));
240 if let Err(error) = std::fs::write(&script_path, script) {
241 self.log.line(&format!("##[error]Could not write the script: {error}"));
242 return (false, BTreeMap::new(), BTreeMap::new());
243 }
244 let files = match StepFiles::new(&self.temp, &id) {
245 Ok(files) => files,
246 Err(error) => {
247 self.log.line(&format!("##[error]Could not make the step's files: {error}"));
248 return (false, BTreeMap::new(), BTreeMap::new());
249 }
250 };
251 let args: Vec<String> = args.iter().map(|a| a.replace("{0}", &script_path.display().to_string())).collect();
252 self.log.line(&format!("shell: {program} {}", args.join(" ")));
253 let dir = match working_directory {
254 Some(dir) if Path::new(dir).is_absolute() => PathBuf::from(dir),
255 Some(dir) => self.workspace.join(dir),
256 None => self.workspace.clone(),
257 };
258 let mut command = Command::new(&program);
259 command.args(&args).current_dir(&dir).env_clear().envs(self.process_env(env, &files));
260 let mut commands = Commands {
261 debug: self.debug,
262 ..Commands::default()
263 };
264 let ended = process::run(command, timeout.min(self.remaining()), &mut self.log, &mut commands);
265 let ok = match ended {
266 Ok(Ended::Exited(0)) => true,
267 Ok(Ended::Exited(code)) => {
268 self.log.line(&format!("##[error]Process completed with exit code {code}."));
269 false
270 }
271 Ok(Ended::TimedOut) => {
272 self.log.line("##[error]The step ran past its time limit and was stopped.");
273 false
274 }
275 Err(error) => {
276 self.log.line(&format!("##[error]{program} could not be started: {error}"));
277 false
278 }
279 };
280 let (outputs, state) = self.absorb(&files, &commands);
281 (ok, outputs, state)
282 }
283
284 /// Runs one step of a frame. Returns whether it succeeded (its
285 /// conclusion). `number` is the step the log belongs to.
286 pub(crate) fn step(&mut self, frame: &mut Frame, step: &Map<String, Value>, number: u32, report: bool, defaults: &Map<String, Value>) -> bool {
287 let env_before = self.env_context(frame);
288 let contexts = self.contexts_for(frame, &env_before);
289 let title = match step.get("name").map(expr::to_text) {
290 Some(name) => self.with_scope(&contexts, |scope| expr::interpolate(&name, scope)).unwrap_or(name),
291 None => default_title(step),
292 };
293 let condition = step.get("if").map(expr::to_text).unwrap_or_default();
294 let run_it = match self.with_scope(&contexts, |scope| expr::condition(&condition, scope)) {
295 Ok(run_it) => run_it,
296 Err(problem) => {
297 self.log.line(&format!("##[error]The step's `if` does not read: {problem}"));
298 self.failed = true;
299 if report {
300 self.log.step_state(number, &title, "completed", Some("failure"));
301 }
302 return false;
303 }
304 };
305 let id = step.get("id").map(expr::to_text);
306 if !run_it {
307 if let Some(id) = &id {
308 frame.steps.insert(id.clone(), json!({ "outputs": {}, "outcome": "skipped", "conclusion": "skipped" }));
309 }
310 if report {
311 self.log.step_state(number, &title, "completed", Some("skipped"));
312 }
313 return true;
314 }
315 if report {
316 self.log.step(number);
317 self.log.step_state(number, &title, "in_progress", None);
318 }
319
320 // The step's own env, read with the contexts before it.
321 let mut env = env_before.clone();
322 if let Some(Value::Object(step_env)) = step.get("env") {
323 for (name, value) in step_env {
324 let value = self.with_scope(&contexts, |scope| expr::interpolate_value(value, scope)).unwrap_or(Value::Null);
325 env.insert(name.clone(), expr::to_text(&value));
326 }
327 }
328 let timeout = step
329 .get("timeout-minutes")
330 .and_then(|v| self.with_scope(&contexts, |scope| expr::interpolate_value(v, scope)).ok())
331 .and_then(|v| v.as_f64().or_else(|| expr::to_text(&v).parse().ok()))
332 .map_or(Duration::from_secs(6 * 3600), |minutes| Duration::from_secs_f64(minutes * 60.0));
333 let continue_on_error = step
334 .get("continue-on-error")
335 .and_then(|v| self.with_scope(&contexts, |scope| expr::interpolate_value(v, scope)).ok())
336 .is_some_and(|v| expr::truthy(&v));
337
338 let (ok, outputs) = if let Some(run) = step.get("run").map(expr::to_text) {
339 let script = match self.with_scope(&contexts, |scope| expr::interpolate(&run, scope)) {
340 Ok(script) => script,
341 Err(problem) => {
342 self.log.line(&format!("##[error]The script does not read: {problem}"));
343 String::new()
344 }
345 };
346 self.log.line(&format!("##[group]{title}"));
347 for line in script.lines() {
348 self.log.line(line);
349 }
350 self.log.line("##[endgroup]");
351 let shell = step
352 .get("shell")
353 .map(expr::to_text)
354 .or_else(|| defaults.get("shell").map(expr::to_text));
355 if frame.action_path.is_some() && shell.is_none() {
356 self.log.line("##[error]A composite action's `run` steps need a `shell`.");
357 (false, BTreeMap::new())
358 } else {
359 let working_directory = step
360 .get("working-directory")
361 .or_else(|| defaults.get("working-directory"))
362 .map(|v| self.with_scope(&contexts, |scope| expr::interpolate(&expr::to_text(v), scope)).unwrap_or_else(|_| expr::to_text(v)));
363 let mut env = env;
364 if let Some(path) = &frame.action_path {
365 env.insert("GITHUB_ACTION_PATH".into(), path.clone());
366 }
367 let (ok, outputs, _) = self.run_script(&script, shell.as_deref(), working_directory.as_deref(), &env, timeout);
368 (ok, outputs)
369 }
370 } else if let Some(uses) = step.get("uses").map(expr::to_text) {
371 let with: BTreeMap<String, String> = match step.get("with") {
372 Some(Value::Object(with)) => with
373 .iter()
374 .map(|(k, v)| {
375 let value = self.with_scope(&contexts, |scope| expr::interpolate_value(v, scope)).unwrap_or(Value::Null);
376 (k.clone(), expr::to_text(&value))
377 })
378 .collect(),
379 _ => BTreeMap::new(),
380 };
381 self.uses(&uses, &with, &env, frame, &title, id.as_deref(), timeout)
382 } else {
383 self.log.line("##[error]A step needs `run` or `uses`.");
384 (false, BTreeMap::new())
385 };
386
387 let outcome = if ok { "success" } else { "failure" };
388 let conclusion = if ok || continue_on_error { "success" } else { "failure" };
389 if !ok && continue_on_error {
390 self.log.line("##[warning]The step failed, and `continue-on-error` lets the job go on.");
391 }
392 if let Some(id) = &id {
393 let outputs: Map<String, Value> = outputs.iter().map(|(k, v)| (k.clone(), Value::String(v.clone()))).collect();
394 frame.steps.insert(id.clone(), json!({ "outputs": outputs, "outcome": outcome, "conclusion": conclusion }));
395 }
396 if conclusion == "failure" {
397 self.failed = true;
398 }
399 if report {
400 self.log.step_state(number, &title, "completed", Some(conclusion));
401 }
402 conclusion == "success"
403 }
404
405 fn report_steps(&self) {
406 self.log.steps(&self.step_names);
407 }
408}
409
410/// An id for files, unique enough within one job.
411fn rand_id() -> u64 {
412 use std::sync::atomic::{AtomicU64, Ordering};
413 static NEXT: AtomicU64 = AtomicU64::new(1);
414 let nanos = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_nanos() as u64).unwrap_or(0);
415 nanos ^ (NEXT.fetch_add(1, Ordering::Relaxed) << 48)
416}
417
418fn interpolated_map(job: &Job, value: Option<&Value>, contexts: &Map<String, Value>) -> BTreeMap<String, String> {
419 let mut out = BTreeMap::new();
420 if let Some(Value::Object(map)) = value {
421 for (name, value) in map {
422 let value = job.with_scope(contexts, |scope| expr::interpolate_value(value, scope)).unwrap_or(Value::Null);
423 out.insert(name.clone(), expr::to_text(&value));
424 }
425 }
426 out
427}
428
429fn setup(spec: Value, api: Api) -> Result<Job> {
430 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();
431 let log = Log::new(api, masks);
432 let workspace = PathBuf::from(WORKSPACE);
433 let temp = PathBuf::from(TEMP);
434 std::fs::create_dir_all(&workspace).context("could not make the workspace")?;
435 std::fs::create_dir_all(&temp).context("could not make the temporary folder")?;
436 std::fs::write(temp.join("event.json"), serde_json::to_string_pretty(&spec["event"])?)?;
437
438 // This process's environment, less what only it should see.
439 let mut base_env: BTreeMap<String, String> =
440 std::env::vars().filter(|(name, _)| !matches!(name.as_str(), "ACTIONS_TOKEN" | "ACTIONS_JOB" | "MODE") && !name.starts_with("G1T_")).collect();
441 base_env.insert("HOME".into(), std::env::var("HOME").unwrap_or_else(|_| "/home/node".into()));
442 base_env.extend(text_map(spec.get("variables")));
443 base_env.insert("GITHUB_EVENT_PATH".into(), temp.join("event.json").display().to_string());
444
445 let mut contexts: Map<String, Value> = spec["contexts"].as_object().cloned().unwrap_or_default();
446 contexts.insert("github".into(), spec["github"].clone());
447 let debug = contexts
448 .get("secrets")
449 .and_then(|s| s.get("ACTIONS_STEP_DEBUG"))
450 .or_else(|| contexts.get("vars").and_then(|v| v.get("ACTIONS_STEP_DEBUG")))
451 .is_some_and(|v| expr::to_text(v) == "true");
452
453 let timeout = spec["timeoutMinutes"].as_u64().unwrap_or(60);
454 let mut job = Job {
455 log,
456 spec,
457 workspace,
458 temp,
459 base_env,
460 added_env: BTreeMap::new(),
461 path_prepend: Vec::new(),
462 workflow_env: BTreeMap::new(),
463 job_env: BTreeMap::new(),
464 contexts,
465 failed: false,
466 posts: Vec::new(),
467 step_names: Vec::new(),
468 deadline: Instant::now() + Duration::from_secs(timeout * 60),
469 debug,
470 last_node_outputs: BTreeMap::new(),
471 last_node_state: BTreeMap::new(),
472 };
473
474 // The workflow's env reads github, secrets, inputs and vars; the job's
475 // also its matrix, needs and strategy.
476 let mut contexts = job.contexts.clone();
477 contexts.insert("env".into(), json!({}));
478 job.workflow_env = interpolated_map(&job, job.spec["workflow"].get("env"), &contexts);
479 contexts.insert("env".into(), Value::Object(job.workflow_env.iter().map(|(k, v)| (k.clone(), json!(v))).collect()));
480 job.job_env = interpolated_map(&job, job.spec["spec"].get("env"), &contexts);
481 Ok(job)
482}
483
484/// The job's `defaults.run`, its own over the workflow's.
485fn run_defaults(spec: &Value) -> Map<String, Value> {
486 let mut defaults = spec["workflow"]["defaults"]["run"].as_object().cloned().unwrap_or_default();
487 if let Some(own) = spec["spec"]["defaults"]["run"].as_object() {
488 defaults.extend(own.clone());
489 }
490 defaults
491}
492
493fn run_job(job: &mut Job) {
494 let steps: Vec<Map<String, Value>> = job.spec["spec"]["steps"]
495 .as_array()
496 .map(|steps| steps.iter().filter_map(|s| s.as_object().cloned()).collect())
497 .unwrap_or_default();
498 let defaults = run_defaults(&job.spec);
499
500 // Step names as they read before anything has run.
501 let frame = Frame::default();
502 let env = job.env_context(&frame);
503 let contexts = job.contexts_for(&frame, &env);
504 job.step_names = steps
505 .iter()
506 .map(|step| match step.get("name").map(expr::to_text) {
507 Some(name) => job.with_scope(&contexts, |scope| expr::interpolate(&name, scope)).unwrap_or(name),
508 None => default_title(step),
509 })
510 .collect();
511 job.report_steps();
512
513 job.log.step(0);
514 job.log.line(&format!("Job: {}", job.spec["name"].as_str().unwrap_or_default()));
515 job.log.line("Runner: g1t, Linux X64 (Debian bookworm, Node 24, Python 3, Go, Rust)");
516 if let Some(Value::Object(matrix)) = job.contexts.get("matrix")
517 && !matrix.is_empty()
518 {
519 job.log.line(&format!("Matrix: {}", serde_json::to_string(matrix).unwrap_or_default()));
520 }
521 job.log.flush();
522
523 let mut frame = Frame::default();
524 for (index, step) in steps.iter().enumerate() {
525 job.step(&mut frame, step, index as u32 + 1, true, &defaults);
526 if job.remaining().is_zero() {
527 job.log.line("##[error]The job ran past its time limit.");
528 job.failed = true;
529 break;
530 }
531 }
532
533 // Post steps, last registered first.
534 let posts: Vec<Post> = std::mem::take(&mut job.posts);
535 for post in posts.into_iter().rev() {
536 let number = job.step_names.len() as u32 + 1;
537 job.step_names.push(post.name.clone());
538 job.report_steps();
539 let contexts = job.contexts_for(&frame, &job.env_context(&frame));
540 let run_it = job.with_scope(&contexts, |scope| expr::condition(&post.condition, scope)).unwrap_or(true);
541 if !run_it {
542 job.log.step_state(number, &post.name, "completed", Some("skipped"));
543 continue;
544 }
545 job.log.step(number);
546 job.log.step_state(number, &post.name, "in_progress", None);
547 let ok = match &post.run {
548 PostRun::Node { action_dir, script } => job.run_node(action_dir, script, &post.env),
549 PostRun::CacheSave { key, paths } => job.cache_save(key, paths),
550 };
551 job.log.step_state(number, &post.name, "completed", Some(if ok { "success" } else { "failure" }));
552 if !ok {
553 job.failed = true;
554 }
555 }
556
557 // The job's outputs, read now that every step has run.
558 let env = job.env_context(&frame);
559 let contexts = job.contexts_for(&frame, &env);
560 let mut outputs = Map::new();
561 if let Some(Value::Object(declared)) = job.spec["spec"].get("outputs") {
562 for (name, value) in declared {
563 let value = job.with_scope(&contexts, |scope| expr::interpolate_value(value, scope)).unwrap_or(Value::Null);
564 outputs.insert(name.clone(), Value::String(expr::to_text(&value)));
565 }
566 }
567 let conclusion = if job.failed { "failure" } else { "success" };
568 job.log.done(conclusion, &outputs, None);
569}
570
571pub(crate) fn main() -> i32 {
572 let api = match (crate::env("G1T_API"), crate::env("ACTIONS_JOB"), crate::env("ACTIONS_TOKEN")) {
573 (Ok(base), Ok(job), Ok(token)) => Api { base, job, token },
574 _ => {
575 eprintln!("g1t-runner: G1T_API, ACTIONS_JOB and ACTIONS_TOKEN are needed");
576 return 2;
577 }
578 };
579 let spec = match api.spec() {
580 Ok(spec) => spec,
581 Err(error) => {
582 eprintln!("g1t-runner: could not fetch the job: {error:#}");
583 api.report(json!({ "kind": "done", "conclusion": "failure", "reason": format!("The runner could not fetch the job: {error}") }));
584 return 1;
585 }
586 };
587 let reporter = Api {
588 base: api.base.clone(),
589 job: api.job.clone(),
590 token: api.token.clone(),
591 };
592 let mut job = match setup(spec, reporter) {
593 Ok(job) => job,
594 Err(error) => {
595 api.report(json!({ "kind": "done", "conclusion": "failure", "reason": format!("The runner could not set up: {error:#}") }));
596 return 1;
597 }
598 };
599 run_job(&mut job);
600 if job.failed { 1 } else { 0 }
601}