Skip to content

Commit cc32a57

Browse files
authored
om ci: Group log output in github actions (#413)
1 parent 534c84b commit cc32a57

File tree

3 files changed

+92
-32
lines changed

3 files changed

+92
-32
lines changed

crates/omnix-ci/src/command/run.rs

Lines changed: 67 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! The run command
22
use std::{
33
collections::HashMap,
4+
env,
5+
future::Future,
46
io::Write,
57
path::{Path, PathBuf},
68
};
@@ -20,7 +22,10 @@ use omnix_common::config::OmConfig;
2022
use omnix_health::{traits::Checkable, NixHealth};
2123
use serde::{Deserialize, Serialize};
2224

23-
use crate::{config::subflakes::SubflakesConfig, flake_ref::FlakeRef, step::core::StepsResult};
25+
use crate::{
26+
config::subflakes::SubflakesConfig, flake_ref::FlakeRef, github::actions::in_github_log_group,
27+
step::core::StepsResult,
28+
};
2429

2530
use super::run_remote;
2631

@@ -64,6 +69,10 @@ pub struct RunCommand {
6469
#[arg(default_value = ".")]
6570
pub flake_ref: FlakeRef,
6671

72+
/// Print Github Actions log groups (enabled by default when run in Github Actions)
73+
#[clap(long, default_value_t = env::var("GITHUB_ACTION").is_ok())]
74+
pub github_output: bool,
75+
6776
/// Arguments for all steps
6877
#[command(flatten)]
6978
pub steps_args: crate::step::core::StepsArgs,
@@ -114,15 +123,22 @@ impl RunCommand {
114123
// TODO: We'll refactor this function to use steps
115124
// https://github.com/juspay/omnix/issues/216
116125

117-
tracing::info!("{}", "\n👟 Gathering NixInfo".bold());
118-
let nix_info = NixInfo::get()
119-
.await
120-
.as_ref()
121-
.with_context(|| "Unable to gather nix info")?;
126+
let nix_info = in_github_log_group("info", self.github_output, || async {
127+
tracing::info!("{}", "\n👟 Gathering NixInfo".bold());
128+
NixInfo::get()
129+
.await
130+
.as_ref()
131+
.with_context(|| "Unable to gather nix info")
132+
})
133+
.await?;
122134

123135
// First, run the necessary health checks
124-
tracing::info!("{}", "\n🫀 Performing health check".bold());
125-
check_nix_version(&cfg, nix_info).await?;
136+
in_github_log_group("health", self.github_output, || async {
137+
tracing::info!("{}", "\n🫀 Performing health check".bold());
138+
// check_nix_version(&cfg, nix_info).await?;
139+
check_nix_version(&cfg, nix_info).await
140+
})
141+
.await?;
126142

127143
// Then, do the CI steps
128144
tracing::info!(
@@ -131,24 +147,36 @@ impl RunCommand {
131147
);
132148
let res = ci_run(&self.nixcmd, verbose, self, &cfg, &nix_info.nix_config).await?;
133149

134-
let m_out_link = self.get_out_link();
135-
let s = serde_json::to_string(&res)?;
136-
let mut path = tempfile::Builder::new()
137-
.prefix("om-ci-results-")
138-
.suffix(".json")
139-
.tempfile()?;
140-
path.write_all(s.as_bytes())?;
141-
142-
let results_path =
143-
addstringcontext::addstringcontext(&self.nixcmd, path.path(), m_out_link).await?;
144-
println!("{}", results_path.display());
145-
if let Some(m_out_link) = m_out_link {
146-
tracing::info!(
147-
"Result available at {:?} and symlinked at {:?}",
148-
results_path.as_path(),
149-
m_out_link
150-
);
151-
}
150+
let msg = in_github_log_group::<anyhow::Result<String>, _, _>(
151+
"outlink",
152+
self.github_output,
153+
|| async {
154+
let m_out_link = self.get_out_link();
155+
let s = serde_json::to_string(&res)?;
156+
let mut path = tempfile::Builder::new()
157+
.prefix("om-ci-results-")
158+
.suffix(".json")
159+
.tempfile()?;
160+
path.write_all(s.as_bytes())?;
161+
162+
let results_path =
163+
addstringcontext::addstringcontext(&self.nixcmd, path.path(), m_out_link)
164+
.await?;
165+
println!("{}", results_path.display());
166+
167+
let msg = format!(
168+
"Result available at {:?}{}",
169+
results_path.as_path(),
170+
m_out_link
171+
.map(|p| format!(" and symlinked at {:?}", p))
172+
.unwrap_or_default()
173+
);
174+
Ok(msg)
175+
},
176+
)
177+
.await?;
178+
179+
tracing::info!("{}", msg);
152180

153181
Ok(())
154182
}
@@ -213,7 +241,7 @@ pub async fn check_nix_version(cfg: &OmConfig, nix_info: &NixInfo) -> anyhow::Re
213241
Ok(())
214242
}
215243

216-
/// Run CI fo all subflakes
244+
/// Run CI for all subflakes
217245
pub async fn ci_run(
218246
cmd: &NixCmd,
219247
verbose: bool,
@@ -249,11 +277,18 @@ pub async fn ci_run(
249277
continue;
250278
}
251279

252-
tracing::info!("\n🍎 {}", name);
253-
let steps_res = subflake
254-
.steps
255-
.run(cmd, verbose, run_cmd, &systems, &cfg.flake_url, subflake)
256-
.await?;
280+
let steps_res = in_github_log_group(
281+
&format!("subflake={}", name),
282+
run_cmd.github_output,
283+
|| async {
284+
tracing::info!("\n🍎 {}", name);
285+
subflake
286+
.steps
287+
.run(cmd, verbose, run_cmd, &systems, &cfg.flake_url, subflake)
288+
.await
289+
},
290+
)
291+
.await?;
257292
res.insert(subflake_name.clone(), steps_res);
258293
}
259294

crates/omnix-ci/src/github/actions.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//! Working with GitHub Actions
2+
3+
use std::{future::Future, io::Write};
4+
5+
/// Group log lines in GitHub Actions
6+
///
7+
/// https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#grouping-log-lines
8+
pub async fn in_github_log_group<T, F, Fut>(name: &str, enable: bool, f: F) -> T
9+
where
10+
F: FnOnce() -> Fut,
11+
Fut: Future<Output = T>,
12+
{
13+
if enable {
14+
eprintln!("::group::{}", name);
15+
}
16+
17+
let result = f().await;
18+
19+
if enable {
20+
eprintln!("::endgroup::");
21+
}
22+
23+
result
24+
}

crates/omnix-ci/src/github/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
//! GitHub related types and functions.
2+
pub mod actions;
23
pub mod matrix;
34
pub mod pull_request;

0 commit comments

Comments
 (0)