Merge pull request #110 from DeterminateSystems/graham/fh-433-magic-nix-cache-should-disable-github-actions-cache-if

Graham/fh 433 magic nix cache should disable GitHub actions cache when flakehub cache is enabled
This commit is contained in:
Graham Christensen 2024-11-06 12:05:23 -05:00 committed by GitHub
commit 448d84e32f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 35 additions and 21 deletions

View file

@ -72,7 +72,7 @@ jobs:
uses: DeterminateSystems/nix-installer-action@main uses: DeterminateSystems/nix-installer-action@main
- name: Test magic-nix-cache-action@main on ${{ matrix.systems.runner }} - 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
with: with:
source-binary: "${{ env.ARTIFACT_KEY }}/${{ env.ARCHIVE_NAME }}" source-binary: "${{ env.ARTIFACT_KEY }}/${{ env.ARCHIVE_NAME }}"
_internal-strict-mode: true _internal-strict-mode: true

View file

@ -33,7 +33,7 @@ pub async fn init_cache(
environment: Environment, environment: Environment,
flakehub_api_server: &Url, flakehub_api_server: &Url,
flakehub_cache_server: &Url, flakehub_cache_server: &Url,
flakehub_flake_name: Option<String>, flakehub_flake_name: &Option<String>,
store: Arc<NixStore>, store: Arc<NixStore>,
auth_method: &super::FlakeHubAuthSource, auth_method: &super::FlakeHubAuthSource,
) -> Result<State> { ) -> Result<State> {

View file

@ -110,11 +110,11 @@ struct Args {
/// Whether to use the GHA cache. /// Whether to use the GHA cache.
#[arg(long)] #[arg(long)]
use_gha_cache: bool, use_gha_cache: Option<Option<CacheTrinary>>,
/// Whether to use the FlakeHub binary cache. /// Whether to use the FlakeHub binary cache.
#[arg(long)] #[arg(long)]
use_flakehub: Option<Option<FlakeHubArg>>, use_flakehub: Option<Option<CacheTrinary>>,
/// URL to which to post startup notification. /// URL to which to post startup notification.
#[arg(long)] #[arg(long)]
@ -130,17 +130,17 @@ struct Args {
} }
#[derive(Debug, Clone, Copy, PartialEq, clap::ValueEnum)] #[derive(Debug, Clone, Copy, PartialEq, clap::ValueEnum)]
pub enum FlakeHubArg { pub enum CacheTrinary {
NoPreference, NoPreference,
Enabled, Enabled,
Disabled, Disabled,
} }
impl From<Option<Option<FlakeHubArg>>> for FlakeHubArg { impl From<Option<Option<CacheTrinary>>> for CacheTrinary {
fn from(b: Option<Option<FlakeHubArg>>) -> Self { fn from(b: Option<Option<CacheTrinary>>) -> Self {
match b { match b {
None => FlakeHubArg::NoPreference, None => CacheTrinary::NoPreference,
Some(None) => FlakeHubArg::Enabled, Some(None) => CacheTrinary::Enabled,
Some(Some(v)) => v, Some(Some(v)) => v,
} }
} }
@ -164,13 +164,13 @@ impl From<bool> for Dnixd {
impl Args { impl Args {
fn validate(&self, environment: env::Environment) -> Result<(), error::Error> { 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( return Err(error::Error::Config(String::from(
"the --use-gha-cache flag should not be applied in GitLab CI", "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( return Err(error::Error::Config(String::from(
"you must set --use-flakehub in GitLab CI", "you must set --use-flakehub in GitLab CI",
))); )));
@ -179,7 +179,11 @@ impl Args {
Ok(()) 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() self.use_flakehub.into()
} }
} }
@ -287,17 +291,17 @@ async fn main_cli() -> Result<()> {
dnixd_available, dnixd_available,
) { ) {
// User has explicitly pyassed --use-flakehub=disabled, so just straight up don't // User has explicitly pyassed --use-flakehub=disabled, so just straight up don't
(FlakeHubArg::Disabled, _, _) => { (CacheTrinary::Disabled, _, _) => {
tracing::info!("Disabling FlakeHub cache."); tracing::info!("Disabling FlakeHub cache.");
None None
} }
// User has no preference, did not pass a netrc, and determinate-nixd is not available // 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 // Use it when determinate-nixd is available, and let the user know what's going on
(pref, user_netrc_path, Dnixd::Available) => { (pref, user_netrc_path, Dnixd::Available) => {
if pref == FlakeHubArg::NoPreference { if pref == CacheTrinary::NoPreference {
tracing::info!("Enabling FlakeHub cache because determinate-nixd is available."); tracing::info!("Enabling FlakeHub cache because determinate-nixd is available.");
} }
@ -309,10 +313,17 @@ async fn main_cli() -> Result<()> {
} }
// When determinate-nixd is not available, but the user specified a netrc // 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 // 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!( return Err(anyhow!(
"--flakehub-api-server-netrc is required when determinate-nixd is unavailable" "--flakehub-api-server-netrc is required when determinate-nixd is unavailable"
)); ));
@ -320,16 +331,16 @@ async fn main_cli() -> Result<()> {
}; };
let flakehub_state = if let Some(auth_method) = flakehub_auth_method { 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_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( match flakehub::init_cache(
environment, environment,
flakehub_api_server, flakehub_api_server,
&flakehub_cache_server, flakehub_cache_server,
flakehub_flake_name, flakehub_flake_name,
store.clone(), store.clone(),
&auth_method, &auth_method,
@ -363,7 +374,10 @@ async fn main_cli() -> Result<()> {
None 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"); tracing::info!("Loading credentials from environment");
let credentials = Credentials::load_from_env() let credentials = Credentials::load_from_env()