g1t/crates/runner/src/actions/mod.rs
Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| GitHub Actions on g1t, part two: running workflows | 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 | ||
| A repository has its own sidebar, as settings do | 11 | mod blobs; |
| GitHub Actions on g1t, part two: running workflows | 12 | mod files; |
| 13 | mod process; | |
| 14 | mod report; | |
| 15 | mod uses; | |
| 16 | ||
| 17 | use std::collections::BTreeMap; | |
| 18 | use std::path::{Path, PathBuf}; | |
| 19 | use std::process::Command; | |
| 20 | use std::time::{Duration, Instant}; | |
| 21 | ||
| 22 | use anyhow::{Context, Result}; | |
| 23 | use g1t_actions::events::WORKSPACE; | |
| 24 | use g1t_actions::expr::{self, Scope, Status}; | |
| 25 | use serde_json::{Map, Value, json}; | |
| 26 | ||
| 27 | use files::StepFiles; | |
| 28 | use process::{Commands, Ended}; | |
| 29 | use report::{Api, Log}; | |
| 30 | ||
| 31 | const TEMP: &str = "/home/runner/_temp"; | |
| 32 | ||
| 33 | /// Who is running steps: the job itself, or a composite action inside it. | |
| 34 | #[derive(Clone, Default)] | |
| 35 | pub(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 | ||
| A repository has its own sidebar, as settings do | 46 | /// A step run when the job's steps are done: an action's `post`, or |
| 47 | /// saving the cache. | |
| GitHub Actions on g1t, part two: running workflows | 48 | pub(crate) struct Post { |
| 49 | pub(crate) name: String, | |
| 50 | pub(crate) condition: String, | |
| 51 | pub(crate) env: BTreeMap<String, String>, | |
| A repository has its own sidebar, as settings do | 52 | pub(crate) run: PostRun, |
| GitHub Actions on g1t, part two: running workflows | 53 | } |
| 54 | ||
| A repository has its own sidebar, as settings do | 55 | pub(crate) enum PostRun { |
| 56 | Node { action_dir: PathBuf, script: String }, | |
| 57 | CacheSave { key: String, paths: Vec<String> }, | |
| 58 | } | |
| 59 | ||
| GitHub Actions on g1t, part two: running workflows | 60 | pub(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 | ||
| 85 | fn 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. | |
| 93 | fn 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 | ||
| 102 | impl Job { | |
| A repository has its own sidebar, as settings do | 103 | pub(crate) fn base_env_value(&self, name: &str) -> Option<String> { |
| 104 | self.base_env.get(name).cloned() | |
| 105 | } | |
| 106 | ||
| GitHub Actions on g1t, part two: running workflows | 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. | |
| 411 | fn 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 | ||
| 418 | fn 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 | ||
| 429 | fn 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 | ||
| Agents get guardrails, run credentials, an audit log, a context hub, repository instructions and mentions; security upkeep; snake_case API | 453 | // `timeoutMinutes` is how the API spelled it before its bodies were |
| 454 | // `snake_case`. | |
| 455 | let timeout = spec["timeout_minutes"] | |
| 456 | .as_u64() | |
| 457 | .or_else(|| spec["timeoutMinutes"].as_u64()) | |
| 458 | .unwrap_or(60); | |
| GitHub Actions on g1t, part two: running workflows | 459 | let mut job = Job { |
| 460 | log, | |
| 461 | spec, | |
| 462 | workspace, | |
| 463 | temp, | |
| 464 | base_env, | |
| 465 | added_env: BTreeMap::new(), | |
| 466 | path_prepend: Vec::new(), | |
| 467 | workflow_env: BTreeMap::new(), | |
| 468 | job_env: BTreeMap::new(), | |
| 469 | contexts, | |
| 470 | failed: false, | |
| 471 | posts: Vec::new(), | |
| 472 | step_names: Vec::new(), | |
| 473 | deadline: Instant::now() + Duration::from_secs(timeout * 60), | |
| 474 | debug, | |
| 475 | last_node_outputs: BTreeMap::new(), | |
| 476 | last_node_state: BTreeMap::new(), | |
| 477 | }; | |
| 478 | ||
| 479 | // The workflow's env reads github, secrets, inputs and vars; the job's | |
| 480 | // also its matrix, needs and strategy. | |
| 481 | let mut contexts = job.contexts.clone(); | |
| 482 | contexts.insert("env".into(), json!({})); | |
| 483 | job.workflow_env = interpolated_map(&job, job.spec["workflow"].get("env"), &contexts); | |
| 484 | contexts.insert("env".into(), Value::Object(job.workflow_env.iter().map(|(k, v)| (k.clone(), json!(v))).collect())); | |
| 485 | job.job_env = interpolated_map(&job, job.spec["spec"].get("env"), &contexts); | |
| 486 | Ok(job) | |
| 487 | } | |
| 488 | ||
| 489 | /// The job's `defaults.run`, its own over the workflow's. | |
| 490 | fn run_defaults(spec: &Value) -> Map<String, Value> { | |
| 491 | let mut defaults = spec["workflow"]["defaults"]["run"].as_object().cloned().unwrap_or_default(); | |
| 492 | if let Some(own) = spec["spec"]["defaults"]["run"].as_object() { | |
| 493 | defaults.extend(own.clone()); | |
| 494 | } | |
| 495 | defaults | |
| 496 | } | |
| 497 | ||
| 498 | fn run_job(job: &mut Job) { | |
| 499 | let steps: Vec<Map<String, Value>> = job.spec["spec"]["steps"] | |
| 500 | .as_array() | |
| 501 | .map(|steps| steps.iter().filter_map(|s| s.as_object().cloned()).collect()) | |
| 502 | .unwrap_or_default(); | |
| 503 | let defaults = run_defaults(&job.spec); | |
| 504 | ||
| 505 | // Step names as they read before anything has run. | |
| 506 | let frame = Frame::default(); | |
| 507 | let env = job.env_context(&frame); | |
| 508 | let contexts = job.contexts_for(&frame, &env); | |
| 509 | job.step_names = steps | |
| 510 | .iter() | |
| 511 | .map(|step| match step.get("name").map(expr::to_text) { | |
| 512 | Some(name) => job.with_scope(&contexts, |scope| expr::interpolate(&name, scope)).unwrap_or(name), | |
| 513 | None => default_title(step), | |
| 514 | }) | |
| 515 | .collect(); | |
| 516 | job.report_steps(); | |
| 517 | ||
| 518 | job.log.step(0); | |
| 519 | job.log.line(&format!("Job: {}", job.spec["name"].as_str().unwrap_or_default())); | |
| Actions: workflow_run, workflow.completed, artifacts on the run page, Node 24 | 520 | job.log.line("Runner: g1t, Linux X64 (Debian bookworm, Node 24, Python 3, Go, Rust)"); |
| GitHub Actions on g1t, part two: running workflows | 521 | if let Some(Value::Object(matrix)) = job.contexts.get("matrix") |
| 522 | && !matrix.is_empty() | |
| 523 | { | |
| 524 | job.log.line(&format!("Matrix: {}", serde_json::to_string(matrix).unwrap_or_default())); | |
| 525 | } | |
| 526 | job.log.flush(); | |
| 527 | ||
| 528 | let mut frame = Frame::default(); | |
| 529 | for (index, step) in steps.iter().enumerate() { | |
| 530 | job.step(&mut frame, step, index as u32 + 1, true, &defaults); | |
| 531 | if job.remaining().is_zero() { | |
| 532 | job.log.line("##[error]The job ran past its time limit."); | |
| 533 | job.failed = true; | |
| 534 | break; | |
| 535 | } | |
| 536 | } | |
| 537 | ||
| 538 | // Post steps, last registered first. | |
| 539 | let posts: Vec<Post> = std::mem::take(&mut job.posts); | |
| 540 | for post in posts.into_iter().rev() { | |
| 541 | let number = job.step_names.len() as u32 + 1; | |
| 542 | job.step_names.push(post.name.clone()); | |
| 543 | job.report_steps(); | |
| 544 | let contexts = job.contexts_for(&frame, &job.env_context(&frame)); | |
| 545 | let run_it = job.with_scope(&contexts, |scope| expr::condition(&post.condition, scope)).unwrap_or(true); | |
| 546 | if !run_it { | |
| 547 | job.log.step_state(number, &post.name, "completed", Some("skipped")); | |
| 548 | continue; | |
| 549 | } | |
| 550 | job.log.step(number); | |
| 551 | job.log.step_state(number, &post.name, "in_progress", None); | |
| A repository has its own sidebar, as settings do | 552 | let ok = match &post.run { |
| 553 | PostRun::Node { action_dir, script } => job.run_node(action_dir, script, &post.env), | |
| 554 | PostRun::CacheSave { key, paths } => job.cache_save(key, paths), | |
| 555 | }; | |
| GitHub Actions on g1t, part two: running workflows | 556 | job.log.step_state(number, &post.name, "completed", Some(if ok { "success" } else { "failure" })); |
| 557 | if !ok { | |
| 558 | job.failed = true; | |
| 559 | } | |
| 560 | } | |
| 561 | ||
| 562 | // The job's outputs, read now that every step has run. | |
| 563 | let env = job.env_context(&frame); | |
| 564 | let contexts = job.contexts_for(&frame, &env); | |
| 565 | let mut outputs = Map::new(); | |
| 566 | if let Some(Value::Object(declared)) = job.spec["spec"].get("outputs") { | |
| 567 | for (name, value) in declared { | |
| 568 | let value = job.with_scope(&contexts, |scope| expr::interpolate_value(value, scope)).unwrap_or(Value::Null); | |
| 569 | outputs.insert(name.clone(), Value::String(expr::to_text(&value))); | |
| 570 | } | |
| 571 | } | |
| 572 | let conclusion = if job.failed { "failure" } else { "success" }; | |
| 573 | job.log.done(conclusion, &outputs, None); | |
| 574 | } | |
| 575 | ||
| 576 | pub(crate) fn main() -> i32 { | |
| 577 | let api = match (crate::env("G1T_API"), crate::env("ACTIONS_JOB"), crate::env("ACTIONS_TOKEN")) { | |
| 578 | (Ok(base), Ok(job), Ok(token)) => Api { base, job, token }, | |
| 579 | _ => { | |
| 580 | eprintln!("g1t-runner: G1T_API, ACTIONS_JOB and ACTIONS_TOKEN are needed"); | |
| 581 | return 2; | |
| 582 | } | |
| 583 | }; | |
| 584 | let spec = match api.spec() { | |
| 585 | Ok(spec) => spec, | |
| 586 | Err(error) => { | |
| 587 | eprintln!("g1t-runner: could not fetch the job: {error:#}"); | |
| 588 | api.report(json!({ "kind": "done", "conclusion": "failure", "reason": format!("The runner could not fetch the job: {error}") })); | |
| 589 | return 1; | |
| 590 | } | |
| 591 | }; | |
| 592 | let reporter = Api { | |
| 593 | base: api.base.clone(), | |
| 594 | job: api.job.clone(), | |
| 595 | token: api.token.clone(), | |
| 596 | }; | |
| 597 | let mut job = match setup(spec, reporter) { | |
| 598 | Ok(job) => job, | |
| 599 | Err(error) => { | |
| 600 | api.report(json!({ "kind": "done", "conclusion": "failure", "reason": format!("The runner could not set up: {error:#}") })); | |
| 601 | return 1; | |
| 602 | } | |
| 603 | }; | |
| 604 | run_job(&mut job); | |
| 605 | if job.failed { 1 } else { 0 } | |
| 606 | } |