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

Graham/fh 433 magic nix cache should disable GitHub actions cache when flakehub cache is enabled #110

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/check-and-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:
uses: DeterminateSystems/nix-installer-action@main

- name: Test magic-nix-cache-action@main on ${{ matrix.systems.runner }}
uses: DeterminateSystems/magic-nix-cache-action@main
uses: DeterminateSystems/magic-nix-cache-action@graham/fh-433-magic-nix-cache-should-disable-github-actions-cache-if
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure we want to have this branch be used in production...? If it gets deleted, runs will start failing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EXTREME DERP

with:
source-binary: "${{ env.ARTIFACT_KEY }}/${{ env.ARCHIVE_NAME }}"
_internal-strict-mode: true
Expand Down
2 changes: 1 addition & 1 deletion magic-nix-cache/src/flakehub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub async fn init_cache(
environment: Environment,
flakehub_api_server: &Url,
flakehub_cache_server: &Url,
flakehub_flake_name: Option<String>,
flakehub_flake_name: &Option<String>,
store: Arc<NixStore>,
auth_method: &super::FlakeHubAuthSource,
) -> Result<State> {
Expand Down
52 changes: 33 additions & 19 deletions magic-nix-cache/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ struct Args {

/// Whether to use the GHA cache.
#[arg(long)]
use_gha_cache: bool,
use_gha_cache: Option<Option<CacheTrinary>>,

/// Whether to use the FlakeHub binary cache.
#[arg(long)]
use_flakehub: Option<Option<FlakeHubArg>>,
use_flakehub: Option<Option<CacheTrinary>>,

/// URL to which to post startup notification.
#[arg(long)]
Expand All @@ -130,17 +130,17 @@ struct Args {
}

#[derive(Debug, Clone, Copy, PartialEq, clap::ValueEnum)]
pub enum FlakeHubArg {
pub enum CacheTrinary {
NoPreference,
Enabled,
Disabled,
}

impl From<Option<Option<FlakeHubArg>>> for FlakeHubArg {
fn from(b: Option<Option<FlakeHubArg>>) -> Self {
impl From<Option<Option<CacheTrinary>>> for CacheTrinary {
fn from(b: Option<Option<CacheTrinary>>) -> Self {
match b {
None => FlakeHubArg::NoPreference,
Some(None) => FlakeHubArg::Enabled,
None => CacheTrinary::NoPreference,
Some(None) => CacheTrinary::Enabled,
Some(Some(v)) => v,
}
}
Expand All @@ -164,13 +164,13 @@ impl From<bool> for Dnixd {

impl Args {
fn validate(&self, environment: env::Environment) -> Result<(), error::Error> {
if environment.is_gitlab_ci() && self.use_gha_cache {
if environment.is_gitlab_ci() && self.github_cache_preference() == CacheTrinary::Enabled {
return Err(error::Error::Config(String::from(
"the --use-gha-cache flag should not be applied in GitLab CI",
)));
}

if environment.is_gitlab_ci() && self.flakehub_preference() != FlakeHubArg::Enabled {
if environment.is_gitlab_ci() && self.flakehub_preference() != CacheTrinary::Enabled {
return Err(error::Error::Config(String::from(
"you must set --use-flakehub in GitLab CI",
)));
Expand All @@ -179,7 +179,11 @@ impl Args {
Ok(())
}

fn flakehub_preference(&self) -> FlakeHubArg {
fn github_cache_preference(&self) -> CacheTrinary {
self.use_gha_cache.into()
}

fn flakehub_preference(&self) -> CacheTrinary {
self.use_flakehub.into()
}
}
Expand Down Expand Up @@ -287,17 +291,17 @@ async fn main_cli() -> Result<()> {
dnixd_available,
) {
// User has explicitly pyassed --use-flakehub=disabled, so just straight up don't
(FlakeHubArg::Disabled, _, _) => {
(CacheTrinary::Disabled, _, _) => {
tracing::info!("Disabling FlakeHub cache.");
None
}

// User has no preference, did not pass a netrc, and determinate-nixd is not available
(FlakeHubArg::NoPreference, None, Dnixd::Missing) => None,
(CacheTrinary::NoPreference, None, Dnixd::Missing) => None,

// Use it when determinate-nixd is available, and let the user know what's going on
(pref, user_netrc_path, Dnixd::Available) => {
if pref == FlakeHubArg::NoPreference {
if pref == CacheTrinary::NoPreference {
tracing::info!("Enabling FlakeHub cache because determinate-nixd is available.");
}

Expand All @@ -309,27 +313,34 @@ async fn main_cli() -> Result<()> {
}

// When determinate-nixd is not available, but the user specified a netrc
(_, Some(path), Dnixd::Missing) => Some(FlakeHubAuthSource::Netrc(path.to_owned())),
(_, Some(path), Dnixd::Missing) => {
if path.exists() {
Some(FlakeHubAuthSource::Netrc(path.to_owned()))
} else {
tracing::debug!(path = %path.display(), "User-provided netrc does not exist");
None
}
}

// User explicitly turned on flakehub cache, but we have no netrc and determinate-nixd is not present
(FlakeHubArg::Enabled, None, Dnixd::Missing) => {
(CacheTrinary::Enabled, None, Dnixd::Missing) => {
return Err(anyhow!(
"--flakehub-api-server-netrc is required when determinate-nixd is unavailable"
));
}
};

let flakehub_state = if let Some(auth_method) = flakehub_auth_method {
let flakehub_cache_server = args.flakehub_cache_server;
let flakehub_cache_server = &args.flakehub_cache_server;

let flakehub_api_server = &args.flakehub_api_server;

let flakehub_flake_name = args.flakehub_flake_name;
let flakehub_flake_name = &args.flakehub_flake_name;

match flakehub::init_cache(
environment,
flakehub_api_server,
&flakehub_cache_server,
flakehub_cache_server,
flakehub_flake_name,
store.clone(),
&auth_method,
Expand Down Expand Up @@ -363,7 +374,10 @@ async fn main_cli() -> Result<()> {
None
};

let gha_cache = if args.use_gha_cache {
let gha_cache = if (args.github_cache_preference() == CacheTrinary::Enabled)
|| (args.github_cache_preference() == CacheTrinary::NoPreference
&& flakehub_state.is_none())
{
tracing::info!("Loading credentials from environment");

let credentials = Credentials::load_from_env()
Expand Down
Loading