Liero
Liero

Reputation: 27390

How to set nuget server apikey in TFS build server

Question

How to set nuget source api key, so it works with TFS Build running "Network Service"?

Scenario

I have TFS build server and Build Definition that executes as "Network Service".

during the build i'm trying to publish nuget packages:

<Exec Command='"nuget.exe" push "mypackages\*.nupkg" -source http://mynugetserver/'/>

however, I don't know how to set apikey for my nuget server.

What have I tried

nuget setapi "myapikey" -source http://mynugetserver/

when I log in to build server and runthis command It saves the apikey for current user and it is ignored in TFS build that runs as network service. build.proj publishes the packages when executed manually since the key is saved for current user, but it doesn't work when I queue new tfs build.

I have tried to create custom nuget.config in the root directory for my TFS project builds 'C:\Builds\1\MyProject\nuget.config` and run

nuget setapi "myapikey" -source http://mynugetserver/ -ConfigFile "C:\Builds\1\MyProject\nuget.config"

but the build ends with message:

"Key not valid for use in specified state"

Upvotes: 1

Views: 1902

Answers (1)

Eddie Chen - MSFT
Eddie Chen - MSFT

Reputation: 29976

You can set the apiKey with push command directly like following format:

nuget push <packages> <apiKey> -source <nuget server url>

You could also create an individual proj file with following contents and then create a build definition to build it once:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="SetApiKey">
    <Exec Command='nuget.exe setapikey xxxxx -source '/>
</Target>
</Project>

After this, you should be able to push the packages without apikey under "Network Service" and you can delete the build definition and proj file. I just tried it under my environment and it works.

Upvotes: 1

Related Questions