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

Flake ref output #169

Merged
merged 10 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
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
- nix-system: aarch64-darwin
runner: macos-latest-xlarge
- nix-system: x86_64-darwin
runner: macos-12
runner: macos-13-large
steps:
- uses: actions/checkout@v3

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ tracing-error = { version = "0.2.0", default-features = false, features = ["trac
tracing-subscriber = { version = "0.3.15", default-features = false, features = [ "std", "registry", "fmt", "json", "ansi", "env-filter" ] }
github-actions-oidc-claims = "0.3.0"
spdx = "0.10.2"
uuid = { version = "1.4.0", features = ["serde", "v7", "rand", "std"] }
uuid = { version = "1.4.0", features = ["serde", "v4", "v7", "rand", "std"] }
semver = { version = "1.0.18", features = ["serde"] }
thiserror = "1.0.56"
url = { version = "2.5.0", features = ["serde"] }
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ Although the `flakehub-push` Action requires little configuration, you may benef

## 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.
This action sets outputs for integrating into continuous delivery pipelines:

| Output | Description | Example |
| ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------- |
| `flake_name` | Name of the flake. | `DeterminateSystems/flakehub-push` |
| `flake_version` | Version of the published flake. | `0.1.99+rev-2075013a3f3544d45a96f4b35df4ed03cd53779c` |
| `flakeref_exact` | A precise reference that always resolves to this to this exact release. | `DeterminateSystems/flakehub-push/=0.1.99+rev-2075013a3f3544d45a96f4b35df4ed03cd53779c` |
| `flakeref_at_least` | A loose reference to this release. Depending on this reference will require at least this version, and will also resolve to newer releases. This output is not sufficient for deployment pipelines, use `flake_exact` instead. | `DeterminateSystems/flakehub-push/0.1.99+rev-2075013a3f3544d45a96f4b35df4ed03cd53779c` |

## More Information

Expand Down
29 changes: 26 additions & 3 deletions src/github_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,45 @@ pub(crate) enum Error {

#[error("Writing to {0:?}: {1}")]
WriteFile(std::ffi::OsString, std::io::Error),

#[error("Key contains delimiter")]
KeyContainsDelimiter,

#[error("Value contains delimiter")]
ValueContainsDelimiter,
}

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)
.append(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())
fh.write_all(escape_key_value(name, value)?.as_bytes())
.await
.map_err(|e| Error::WriteFile(output_path, e))?;

Ok(())
}

fn escape_key_value<'a>(key: &'a str, value: &'a str) -> Result<String, Error> {
// see: https://github.com/actions/toolkit/blob/6dd369c0e648ed58d0ead326cf2426906ea86401/packages/core/src/file-command.ts#L27-L47
let delimiter = format!("ghadelimiter_{}", uuid::Uuid::new_v4());
let eol = '\n';

if key.contains(&delimiter) {
return Err(Error::KeyContainsDelimiter);
}

if value.contains(&delimiter) {
return Err(Error::ValueContainsDelimiter);
}

Ok(format!(
"{key}<<{delimiter}{eol}{value}{eol}{delimiter}{eol}"
))
}
37 changes: 29 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ async fn execute() -> Result<std::process::ExitCode> {
upload_name = ctx.upload_name,
release_version = &ctx.release_version,
);

set_release_outputs(&ctx.upload_name, &ctx.release_version).await;

if ctx.error_if_release_conflicts {
return Err(Error::Conflict {
upload_name: ctx.upload_name.to_string(),
Expand Down Expand Up @@ -182,14 +185,7 @@ 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);
}
set_release_outputs(&ctx.upload_name, &ctx.release_version).await;

Ok(ExitCode::SUCCESS)
}
Expand Down Expand Up @@ -222,3 +218,28 @@ impl Display for Visibility {
}
}
}

async fn set_release_outputs(upload_name: &str, release_version: &str) {
let outputs = [
("flake_name", upload_name),
("flake_version", release_version),
(
"flakeref_at_least",
&format!("{}/{}", upload_name, release_version),
),
(
"flakeref_exact",
&format!("{}/={}", upload_name, release_version),
),
];
for (output_name, value) in outputs.into_iter() {
if let Err(e) = github_actions::set_output(output_name, value).await {
tracing::warn!(
"Failed to set the `{}` output to {}: {}",
output_name,
value,
e
);
}
}
}
Loading