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