简体   繁体   中英

How to use github nuget packages on Visual Studio 2019

I try to learn Github action and packages. So, I create a sample nuget package and successfully created. However I could not use it.

I follow this step;

  1. Options > Nuget Package Manager > Package Sources
  2. Click add button write organization name and add package source address like that: 在此处输入图像描述

  3. After that Visual Studio doesn't ask me any credential for this address.

  4. If there is no credential, I will expect to an error. And I got it.

    [github] Failed to retrieve metadata from source ' https://nuget.pkg.github.com/[OrganizationName]/query?q=&skip=0&take=26&prerelease=false&semVerLevel=2.0.0 '. Response status code does not indicate success: 401 (Unauthorized).

  5. Then, I decide to some credential like explained in github documentation .

  6. My nuget.config file: 在此处输入图像描述

  7. However still I get Unauthorized error. And also when I click the error link gettin a message like that:

{"errors":[{"code":"Your token has not been granted the required scopes to execute this query. The 'id' field requires one of the following scopes","message":" ['read:packages'], but your token has only been granted the: [''] scopes. Please modify your token's scopes at: https://github.com/settings/tokens ."}]}

but I already have a token with read/write package credential. 在此处输入图像描述

  1. All that fails and I try to another way. I add credential information to Windows Credential Manager. Eveything still same.

So, how I add and use my private nuget packages?

It is not an easy task, but here is the solution (after 2 days of struggling)

  1. First generate PAT (Personal Access Token) in Github
    Follow these steps to generate a PAT
    Very important to select the read:packages options 在此处输入图像描述
    Github will show you the PAT for ONLY ONE TIME, So make sure to copy it to save place, otherwise, you have to generate it again

  2. Now with PAT in your hand, Add Nuget.Config file to your project
    在此处输入图像描述
    The content of the file should be like following

    <?xml version="1.0" encoding="utf-8"?> <configuration> <packageSources> <clear /> <add key="github" value="https://nuget.pkg.github.com/OWNER/index.json" /> </packageSources> <packageSourceCredentials> <github> <add key="Username" value="USERNAME" /> <add key="ClearTextPassword" value="TOKEN" /> </github> </packageSourceCredentials> </configuration>
  3. You must replace:

    • USERNAME with the name of your user account on GitHub.
    • TOKEN with your personal access token (the token you generated in Step 1) .
    • OWNER with the name of the user or organization account that owns the repository containing your project.
  4. You must restart Visual Studio or even Restart the PC This is important

After that open the Terminal and copy and paste (maybe with some modifications) the statement that Github give to you to install the package

在此处输入图像描述

Now you are ready to get the Package .


UPDATE

How to configure github nuget packages for teams?

Configure the nuget.config using environment variables :

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="NuGet official package source" value="https://api.nuget.org/v3/index.json" />
    <add key="Github" value="https://nuget.pkg.github.com/OWNER/index.json" />
  </packageSources>
  <packageSourceCredentials>
    <Github>
      <add key="Username" value="%GITHUB_PACKAGE_USER_NAME%" />
      <add key="ClearTextPassword" value="%GITHUB_PACKAGE_TOKEN%" />
    </Github>
  </packageSourceCredentials>
</configuration>

The GITHUB_PACKAGE_USER_NAME and GITHUB_PACKAGE_TOKEN can be anything you want.

Now, each team member should configure their user environment variables:

  • GITHUB_PACKAGE_USER_NAME : team member github user name
  • GITHUB_PACKAGE_TOKEN : team member Personal Access Token (PAT)

With those configurations, Visual Studio will be able to query and download packages, assuming the team member has access to the OWNER package repository.

How to restore github nuget packages in github actions workflows?

With the previous NuGet.config configured, you need to change your workflow passing the required environment variables, like so:

- name: Restore dependencies
  env:
    GITHUB_PACKAGE_USER_NAME: ${{ github.actor }}
    GITHUB_PACKAGE_TOKEN: ${{ secrets.RESTORE_ORGANIZATION_PACKAGES }}
  run: dotnet restore ./src

Since you can't pass your PAT, you need to configure a github secret (either for the repository or the organization ). In the above example, I created a secret named RESTORE_ORGANIZATION_PACKAGES with read:packages permission at the repository level.

I just faced this problem today with VS 19.

When adding a new source for Nuget packages as shown below, i got prompted a first time for User and Password

Add nuget package source

I did the mistake of entering my github password as password. Instead you should create a token under your github settings as shown below and enter the token itself instead of your account's password.

Generate token

In my case, I selected the following options when creating the token

Token options

VS 19 unfortunately did not prompt another time for credentials and kept showing the error message:

[github] Failed to retrieve metadata from source... .Response status code does not indicate success: 401 (Unauthorized).

To change this parameters on Windows, open Control Panel\User Accounts\Credential Manager as shown below:

Web credentials

Click on Edit, and paste the access token you just created on github as password. Scroll down the list of credentials. In my case, I needed to change the password in a second place as well.

Second web credentials

How to use github nuget packages on Visual Studio 2019

First , make sure that your credential info is correct on the nuget.config file.

I found your document suggest you should put a new nuget.config file to your solution. This nuget.config file is a local action file, it will act on any projects in the current subdirectory and below the current level. You can refer to this document .

In this way, you should restart VS Instance to enable this new nuget.config file.

-------Global nuget.config--------

However , from your description and first picture, you used the global nuget.config file. And UI's info stores under global nuget.config file.

If you want to config this gihub package source for all the projects on your PC, you should config it on the global nuget.config file.

And the first picture which you provided indicates that you used in the global file( C:\Users\xxx(current user)\AppData\Roaming\NuGet\NuGet.Config ).

This function also needs restart VS to enable the new nuget.config file.

Suggestion

  1. please add your content of the file into C:\Users\xxx(current user)\AppData\Roaming\NuGet\NuGet.Config .

  2. restart VS Instance or restart PC to enable this new nuget.config file. It is designed by that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM