Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export the exact flakeref as an output #168

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading