We have an on-premises TFS 2017 server with the package management plugin installed to host custom NuGet packages.
I happily set up TFS builds of my solution. I happily set up custom NuGet packages. Then I ran a build of a solution that made use of my custom packages. Imagine my surprise when the TFS build server was unable to download packages from its own TFS server! Furthermore, the error code was the rarely-seen “402 Payment Required”.
Now, it turns out that Package Management generally requires licenses. This hadn’t mattered because it’s free for Visual Studio Enterprise subscribers, which we all have through MSDN. The build server, however runs under its own service account which naturally doesn’t have an MSDN subscription.
How to have the build server run as a user with an MSDN subscription? After a support call, a guy from Microsoft helped me figure it out. Here it is for everybody with the same problem (and, for me, when the solution below expires in a year).
Basically, the solution is to get the build server to run under the Personal Access Token (PAT) of a user with an MSDN license.
Step 1. Generate a Personal Access Token
If you log into TFS 2017, in the corner under the settings there’s an “Access Tokens” setting.
Create a token. I made mine for a year, ensuring I would forget all this by the time it expires and that a year from now I’ll be confounded when all my builds break.
The personal access token will be a long string. Save it somewhere temporarily.
Step 2. Configure the build agent to use the PAT
If you already have the build agent set up, you’re going to have to remove it. Go to the folder and run:
It’s safest to delete the whole folder and recreate it from the Agent you downloaded from TFS. Until I removed the folder entirely this fix failed.
Once you’ve recreated the server, type
from PowerShell to start the process again.
This time, when it gets to “Enter authentication type (press enter for Integrated)” enter “PAT”.
It will ask you for the token. Enter the long string you got above.
Continue as normal.
The server will download NuGet packages with the license you’ve set up above but with the authorization of the user that the server is running as. If you find you’ve traded “Payment Required” for “Unauthorized”, make sure the build agent user has access as a package reader in Package Management’s Security settings.