diff --git a/README.md b/README.md index 8e90caf..d33033e 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,13 @@ There are two ways to get started configuring this Action: Although the `flakehub-push` Action requires little configuration, you may benefit from assembling it with our friendly UI at [flakehub.com/new][wizard]. +## Integration + +This action sets the `flakeref` output to the exact name and version that was published. +The flake reference can be used in subsequent steps or workflows as part of a deployment process. + +## More Information + ### Manual configuration The example workflow configuration below pushes new tags matching the conventional format—such as `v1.0.0` or `v0.1.0-rc4`—to [Flakehub]: diff --git a/src/github_actions.rs b/src/github_actions.rs new file mode 100644 index 0000000..6d64146 --- /dev/null +++ b/src/github_actions.rs @@ -0,0 +1,31 @@ +use tokio::io::AsyncWriteExt; + +#[derive(Debug, thiserror::Error)] +pub(crate) enum Error { + #[error("The `GITHUB_OUTPUT` environment variable is unset.")] + GithubOutputUnset, + + #[error("Failure opening {0:?}: {1}")] + OpenFile(std::ffi::OsString, std::io::Error), + + #[error("Writing to {0:?}: {1}")] + WriteFile(std::ffi::OsString, std::io::Error), +} + +pub(crate) async fn set_output<'a>(name: &'a str, value: &'a str) -> Result<(), Error> { + let output_path = std::env::var_os("GITHUB_OUTPUT").ok_or(Error::GithubOutputUnset)?; + let mut fh = tokio::fs::OpenOptions::new() + .create(true) + .read(true) + .write(true) + .truncate(false) + .open(&output_path) + .await + .map_err(|e| Error::OpenFile(output_path.clone(), e))?; + + fh.write_all(format!("{}={}\n", name, value).as_bytes()) + .await + .map_err(|e| Error::WriteFile(output_path, e))?; + + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index e731ace..0ef29b7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,7 @@ mod flakehub_auth_fake; mod flakehub_client; mod git_context; mod github; +mod github_actions; mod gitlab; mod push_context; mod release_metadata; @@ -181,6 +182,15 @@ async fn execute() -> Result { ctx.release_version ); + if let Err(e) = github_actions::set_output( + "flakeref", + &format!("{}/{}", ctx.upload_name, ctx.release_version), + ) + .await + { + tracing::warn!("Failed to set the `flakeref` output: {}", e); + } + Ok(ExitCode::SUCCESS) }