ElectronNET/Electron.NET · error · BuildAbortedException

Could not resolve the NuGet API key.

Error message

Could not resolve the NuGet API key.

What it means

The build's NuGet push step requires a NuGet.org API key to authenticate the `dotnet nuget push` of the generated .nupkg packages. Before pushing, it reads the NUGET_API_KEY environment variable; if it is missing or empty, the build aborts with a BuildAbortedException so no unauthenticated push is attempted. This is a deliberate fail-fast guard in the CI publish pipeline.

Solutions

  1. Set the NUGET_API_KEY environment variable before running the build (e.g. export NUGET_API_KEY=... locally, or add it as a secret/env in the CI workflow).
  2. In GitHub Actions, map the repository secret to the env var: env: NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}.
  3. Verify the secret name in the CI provider exactly matches NUGET_API_KEY (case-sensitive) and is scoped to the branch/organization running the build.
  4. If you don't intend to publish, run a target that does not include the NuGet push step.

Example fix

// before (GitHub Actions)
- run: ./build.sh Publish
// after
- run: ./build.sh Publish
  env:
    NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("NUGET_API_KEY")))
    throw new InvalidOperationException("Set NUGET_API_KEY before running the publish target.");

Try / catch

try
{
    // run publish target
}
catch (BuildAbortedException ex) when (ex.Message.Contains("NuGet API key"))
{
    Logger.Error("NUGET_API_KEY is not set; skipping publish.");
}

Prevention

When it happens

Trigger: Running the Nuke Push/Publish target (Build.cs:156) when the NUGET_API_KEY environment variable is unset or an empty string at the moment ResultDirectory.GlobFiles("*.nupkg") packages are about to be pushed.

Common situations: CI pipeline secrets not configured (e.g. GitHub Actions workflow lacks NUGET_API_KEY in env/secrets); running the publish target locally where the key was never exported; secret renamed in the CI provider so the env var name no longer matches; shell profile exporting an empty NUGET_API_KEY.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of ElectronNET/Electron.NET@87cc6f98b6 (2026-09-14). Data as JSON: /api/errors/1b4832c2a68285ca. Report an issue: GitHub.

Appendix: source

Thrown at nuke/Build.cs:156

            DotNetTest(s => s
                .SetProjectFile(TestProject)
                .SetConfiguration(Configuration)
                .When(_ => GitHubActions.Instance is not null, x => x.SetLoggers("GitHubActions"))
            );
        });

    Target PublishPackages => _ => _
        .DependsOn(Compile)
        .DependsOn(RunUnitTests)
        .Executes(() =>
        {
            var apiKey = Environment.GetEnvironmentVariable("NUGET_API_KEY");


            if (apiKey.IsNullOrEmpty())
            {
                throw new BuildAbortedException("Could not resolve the NuGet API key.");
            }

            foreach (var nupkg in ResultDirectory.GlobFiles("*.nupkg"))
            {
                DotNetNuGetPush(s => s
                    .SetTargetPath(nupkg)
                    .SetSource("https://api.nuget.org/v3/index.json")
                    .SetApiKey(apiKey));
            }
        });

    Target PublishPreRelease => _ => _
        .DependsOn(PublishPackages)
        .Executes(() =>
        {
            string gitHubToken;

            if (GitHubActions != null)

View on GitHub (pinned to 87cc6f98b6)