Skip to content

Commit

Permalink
Merge pull request #168 from DeterminateSystems/flake-ref-output
Browse files Browse the repository at this point in the history
Export the exact flakeref as an output
  • Loading branch information
grahamc authored Sep 24, 2024
2 parents 717ccf6 + fef63bd commit 71cb6f9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
31 changes: 31 additions & 0 deletions src/github_actions.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
10 changes: 10 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -181,6 +182,15 @@ async fn execute() -> Result<std::process::ExitCode> {
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)
}

Expand Down

0 comments on commit 71cb6f9

Please sign in to comment.