Skip to main content

xtask_kit/github_actions/
mod.rs

1use std::fs::OpenOptions;
2use std::io::Write;
3use std::path::{Path, PathBuf};
4
5use anyhow::{Context, Result};
6
7/// Append a key-value pair to a GitHub Actions output file.
8pub fn append_output(path: impl AsRef<Path>, key: &str, value: &str) -> Result<()> {
9    let path = path.as_ref();
10    let mut file = OpenOptions::new()
11        .create(true)
12        .append(true)
13        .open(path)
14        .with_context(|| format!("open GitHub output file {}", path.display()))?;
15    writeln!(file, "{key}={value}")?;
16    Ok(())
17}
18
19/// Append a key-value pair to `GITHUB_OUTPUT` when the variable is present.
20pub fn append_output_env(key: &str, value: &str) -> Result<bool> {
21    let Some(path) = std::env::var_os("GITHUB_OUTPUT") else {
22        return Ok(false);
23    };
24    append_output(PathBuf::from(path), key, value)?;
25    Ok(true)
26}