Skip to main content

demo/
demo.rs

1//! Run with `cargo run --example demo` and eyeball the workflow commands.
2//!
3//! To see the env-file/summary side-effects, point the variables at temp
4//! files first, e.g.:
5//!
6//! ```sh
7//! GITHUB_OUTPUT=$(mktemp) GITHUB_ENV=$(mktemp) GITHUB_STEP_SUMMARY=$(mktemp) \
8//!     cargo run --example demo
9//! ```
10
11use actions_rs::{Annotation, Cell, Summary, env, log, output};
12
13fn main() {
14    log::info(format!(
15        "in GitHub Actions: {} | CI: {} | step-debug: {}",
16        env::is_github_actions(),
17        env::is_ci(),
18        log::is_debug()
19    ));
20
21    let ctx = actions_rs::Context::new();
22    log::info(format!(
23        "repo={:?} ref={:?} sha={:?}",
24        ctx.repository(),
25        ctx.ref_name(),
26        ctx.sha()
27    ));
28
29    // Located annotation with a line range — should print:
30    // ::warning title=demo,file=src/lib.rs,line=10,endLine=12::heads up
31    Annotation::new()
32        .file("src/lib.rs")
33        .line(10)
34        .end_line(12)
35        .title("demo")
36        .warning("heads up: this span looks suspicious");
37
38    // Escaping check: newline in data, colon/comma in a property.
39    Annotation::new()
40        .title("type: mismatch, really")
41        .error("line one\nline two");
42
43    let total = log::group("expensive step", || {
44        log::info("...working...");
45        2 + 2
46    });
47    log::info(format!("group returned {total}"));
48
49    actions_rs::warning!("formatted macro: {} items left", 7);
50
51    output::set_output("answer", 42).expect("set_output");
52    match output::export_var("DEMO_FLAG", true) {
53        Ok(()) => {}
54        Err(actions_rs::Error::UnavailableFileCommand {
55            var: "GITHUB_ENV", ..
56        }) => {
57            log::info("GITHUB_ENV unset; skipping export_var in local demo");
58        }
59        Err(err) => panic!("export_var: {err}"),
60    }
61
62    let mut summary = Summary::new();
63    summary
64        .heading("Demo Report", 2)
65        .raw("Built by the `demo` example.", true)
66        .table([
67            vec![Cell::header("Check"), Cell::header("Result")],
68            vec![Cell::new("clippy"), Cell::new("pass")],
69            vec![Cell::new("tests"), Cell::new("36 pass")],
70        ])
71        .code_block("cargo test", Some("sh"));
72    summary.write().expect("write summary");
73    log::info("summary written (if GITHUB_STEP_SUMMARY was set)");
74}