相关文章推荐
仗义的大蒜  ·  win10错误 cortana - ...·  1 年前    · 
  • Use the GitLab endpoint for NuGet Packages
  • Add the Package Registry as a source for NuGet packages
  • Publish a NuGet package
  • Install packages
  • Symbol packages
  • Supported CLI commands
  • Example project
  • Troubleshooting
  • NuGet packages in the Package Registry

    Version history Introduced in GitLab 12.8.
  • Moved from GitLab Premium to GitLab Free in 13.3.
  • Symbol package support added in GitLab 14.1.
  • Publish NuGet packages in your project’s Package Registry. Then, install the packages whenever you need to use them as a dependency.

    The Package Registry works with:

    For documentation of the specific API endpoints that these clients use, see the NuGet API documentation .

    Learn how to install NuGet .

    Use the GitLab endpoint for NuGet Packages

    Introduced group-level endpoint in GitLab 13.8.

    To use the GitLab endpoint for NuGet Packages, choose an option:

      Project-level : Use when you have few NuGet packages and they are not in the same GitLab group.
    • Group-level : Use when you have many NuGet packages in different projects within the same GitLab group.

    Some features such as publishing a package are only available on the project-level endpoint.

    When asking for versions of a given NuGet package name, the GitLab Package Registry returns a maximum of 300 most recent versions.

    Do not use authentication methods other than the methods documented here. Undocumented authentication methods might be removed in the future.

    caution
    Because of how NuGet handles credentials, the Package Registry rejects anonymous requests on the group-level endpoint. To work around this limitation, set up authentication .

    Add the Package Registry as a source for NuGet packages

    To publish and install packages to the Package Registry, you must add the Package Registry as a source for your packages.

    Prerequisites:

  • A name for your source.
  • Depending on the endpoint level you use, either:
  • Your project ID, which is found on your project’s home page.
  • Your group ID, which is found on your group’s home page.
  • You can now add a new source to NuGet with:

    Add a source with the NuGet CLI

    Project-level endpoint

    A project-level endpoint is required to publish NuGet packages to the Package Registry. A project-level endpoint is also required to install NuGet packages from a project.

    To use the project-level NuGet endpoint, add the Package Registry as a source with nuget :

    nuget source Add -Name <source_name> -Source "https://gitlab.example.com/api/v4/projects/<your_project_id>/packages/nuget/index.json" -UserName <gitlab_username or deploy_token_username> -Password <gitlab_personal_access_token or deploy_token>
    <source_name> is the desired source name.

    For example:

    nuget source Add -Name "GitLab" -Source "https://gitlab.example.com/api/v4/projects/10/packages/nuget/index.json" -UserName carol -Password 12345678asdf
    

    Group-level endpoint

    To install a NuGet package from a group, use a group-level endpoint.

    To use the group-level NuGet endpoint, add the Package Registry as a source with nuget:

    nuget source Add -Name <source_name> -Source "https://gitlab.example.com/api/v4/groups/<your_group_id>/-/packages/nuget/index.json" -UserName <gitlab_username or deploy_token_username> -Password <gitlab_personal_access_token or deploy_token>
    <source_name> is the desired source name.

    For example:

    nuget source Add -Name "GitLab" -Source "https://gitlab.example.com/api/v4/groups/23/-/packages/nuget/index.json" -UserName carol -Password 12345678asdf
    

    Add a source with Visual Studio

    Project-level endpoint

    A project-level endpoint is required to publish NuGet packages to the Package Registry. A project-level endpoint is also required to install NuGet packages from a project.

    To use the project-level NuGet endpoint, add the Package Registry as a source with Visual Studio:

    1. Open Visual Studio.
    2. In Windows, select Tools > Options. On macOS, select Visual Studio > Preferences.
    3. In the NuGet section, select Sources to view a list of all your NuGet sources.
    4. Select Add.
    5. Complete the following fields:

        Name: Name for the source.
      • Source: https://gitlab.example.com/api/v4/projects/<your_project_id>/packages/nuget/index.json, where <your_project_id> is your project ID, and gitlab.example.com is your domain name.
    6. Select Save.
    7. When you access the package, you must enter your Username and Password:

        Username: Your GitLab username or deploy token username.
      • Password: Your personal access token or deploy token.

    The source is displayed in your list.

    If you get a warning, ensure that the Source, Username, and Password are correct.

    Group-level endpoint

    To install a package from a group, use a group-level endpoint.

    To use the group-level NuGet endpoint, add the Package Registry as a source with Visual Studio:

    1. Open Visual Studio.
    2. In Windows, select Tools > Options. On macOS, select Visual Studio > Preferences.
    3. In the NuGet section, select Sources to view a list of all your NuGet sources.
    4. Select Add.
    5. Complete the following fields:

        Name: Name for the source.
      • Source: https://gitlab.example.com/api/v4/groups/<your_group_id>/-/packages/nuget/index.json, where <your_group_id> is your group ID, and gitlab.example.com is your domain name.
    6. Select Save.
    7. When you access the package, you must enter your Username and Password.

        Username: Your GitLab username or deploy token username.
      • Password: Your personal access token or deploy token.

    The source is displayed in your list.

    If you get a warning, ensure that the Source, Username, and Password are correct.

    Add a source with the .NET CLI

    Project-level endpoint

    A project-level endpoint is required to publish NuGet packages to the Package Registry. A project-level endpoint is also required to install NuGet packages from a project.

    To use the project-level NuGet endpoint, add the Package Registry as a source with nuget:

    dotnet nuget add source "https://gitlab.example.com/api/v4/projects/<your_project_id>/packages/nuget/index.json" --name <source_name> --username <gitlab_username or deploy_token_username> --password <gitlab_personal_access_token or deploy_token>
    <source_name> is the desired source name.
  • --store-password-in-clear-text might be necessary depending on your operating system.
  • For example:

    dotnet nuget add source "https://gitlab.example.com/api/v4/projects/10/packages/nuget/index.json" --name gitlab --username carol --password 12345678asdf
    

    Group-level endpoint

    To install a NuGet package from a group, use a group-level endpoint.

    To use the group-level NuGet endpoint, add the Package Registry as a source with nuget:

    dotnet nuget add source "https://gitlab.example.com/api/v4/groups/<your_group_id>/-/packages/nuget/index.json" --name <source_name> --username <gitlab_username or deploy_token_username> --password <gitlab_personal_access_token or deploy_token>
    <source_name> is the desired source name.
  • --store-password-in-clear-text might be necessary depending on your operating system.
  • For example:

    dotnet nuget add source "https://gitlab.example.com/api/v4/groups/23/-/packages/nuget/index.json" --name gitlab --username carol --password 12345678asdf
    

    Add a source with a configuration file

    Project-level endpoint

    A project-level endpoint is required to:

    To use the project-level Package Registry as a source for .NET:

    1. In the root of your project, create a file named nuget.config.
    2. Add this content:

      <?xml version="1.0" encoding="utf-8"?>
      <configuration>
       <packageSources>
           <clear />
           <add key="gitlab" value="https://gitlab.example.com/api/v4/projects/<your_project_id>/packages/nuget/index.json" />
       </packageSources>
       <packageSourceCredentials>
           <gitlab>
               <add key="Username" value="%GITLAB_PACKAGE_REGISTRY_USERNAME%" />
               <add key="ClearTextPassword" value="%GITLAB_PACKAGE_REGISTRY_PASSWORD%" />
           </gitlab>
       </packageSourceCredentials>
      </configuration>
      

      Configure the necessary environment variables:

      export GITLAB_PACKAGE_REGISTRY_USERNAME=<gitlab_username or deploy_token_username>
      export GITLAB_PACKAGE_REGISTRY_PASSWORD=<gitlab_personal_access_token or deploy_token>
      

    Group-level endpoint

    To install a package from a group, use a group-level endpoint.

    To use the group-level Package Registry as a source for .NET:

    1. In the root of your project, create a file named nuget.config.
    2. Add this content:

      <?xml version="1.0" encoding="utf-8"?>
      <configuration>
       <packageSources>
           <clear />
           <add key="gitlab" value="https://gitlab.example.com/api/v4/groups/<your_group_id>/-/packages/nuget/index.json" />
       </packageSources>
       <packageSourceCredentials>
           <gitlab>
               <add key="Username" value="%GITLAB_PACKAGE_REGISTRY_USERNAME%" />
               <add key="ClearTextPassword" value="%GITLAB_PACKAGE_REGISTRY_PASSWORD%" />
           </gitlab>
       </packageSourceCredentials>
      </configuration>
      

      Configure the necessary environment variables:

      export GITLAB_PACKAGE_REGISTRY_USERNAME=<gitlab_username or deploy_token_username>
      export GITLAB_PACKAGE_REGISTRY_PASSWORD=<gitlab_personal_access_token or deploy_token>
      

    Publish a NuGet package

    Prerequisite:

    When publishing packages:

    • The Package Registry on GitLab.com can store up to 5 GB of content. This limit is configurable for self-managed GitLab instances.
    • If you publish the same package with the same version multiple times, each consecutive upload is saved as a separate file. When installing a package, GitLab serves the most recent file.
    • When publishing packages to GitLab, they aren’t displayed in the packages user interface of your project immediately. It can take up to 10 minutes to process a package.

    Publish a package with the NuGet CLI

    Prerequisites:

    Publish a package by running this command:

    nuget push <package_file> -Source <source_name>
    <package_file> is your package filename, ending in .nupkg.
  • <source_name> is the source name used during setup.
  • Publish a package with the .NET CLI

    Prerequisites:

    Publish a package by running this command:

    dotnet nuget push <package_file> --source <source_name>
    <package_file> is your package filename, ending in .nupkg.
  • <source_name> is the source name used during setup.
  • For example:

    dotnet nuget push MyPackage.1.0.0.nupkg --source gitlab
    

    Publish a NuGet package by using CI/CD

    Introduced in GitLab 13.3.

    If you’re using NuGet with GitLab CI/CD, a CI job token can be used instead of a personal access token or deploy token. The token inherits the permissions of the user that generates the pipeline.

    This example shows how to create a new package each time the main branch is updated:

      Add a deploy job to your .gitlab-ci.yml file:

      image: mcr.microsoft.com/dotnet/core/sdk:3.1
      stages:
        - deploy
      deploy:
        stage: deploy
        script:
          - dotnet pack -c Release
          - dotnet nuget add source "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/nuget/index.json" --name gitlab --username gitlab-ci-token --password $CI_JOB_TOKEN --store-password-in-clear-text
          - dotnet nuget push "bin/Release/*.nupkg" --source gitlab
        only:
          - main
        environment: production
      

      Commit the changes and push it to your GitLab repository to trigger a new CI/CD build.

    Publishing a package with the same name or version

    When you publish a package with the same name or version as an existing package, the existing package is overwritten.

    Install packages

    If multiple packages have the same name and version, when you install a package, the most recently-published package is retrieved.

    To install a NuGet package from the Package Registry, you must first add a project-level or group-level endpoint.

    Install a package with the NuGet CLI caution
    By default, nuget checks the official source at nuget.org first. If you have a NuGet package in the Package Registry with the same name as a package at nuget.org, you must specify the source name to install the correct package.

    Install the latest version of a package by running this command:

    Install the latest version of a package by running this command:

    If you push a .nupkg, symbol package files in the .snupkg format are uploaded automatically. You can also push them manually:

    nuget push My.Package.snupkg -Source <source_name>
    

    Consuming symbol packages is not yet guaranteed using clients such as Visual Studio or dotnet-symbol. The .snupkg files are available for download through the UI or the API.

    Follow the NuGet symbol package issue for further updates.

    Supported CLI commands

    The GitLab NuGet repository supports the following commands for the NuGet CLI (nuget) and the .NET CLI (dotnet):

    Example project

    For an example, see the Guided Exploration project Utterly Automated Software and Artifact Versioning with GitVersion. This project:

    • Generates NuGet packages by the msbuild method.
    • Generates NuGet packages by the nuget.exe method.
    • Uses GitLab releases and release-cli in connection with NuGet packaging.
    • Uses a tool called GitVersion to automatically determine and increment versions for the NuGet package in complex repositories.

    You can copy this example project to your own group or instance for testing. See the project page for more details on what other GitLab CI patterns are demonstrated.

    Troubleshooting

    Clear NuGet cache

    To improve performance, NuGet caches files related to a package. If you encounter issues, clear the cache with this command:

    Help & feedback

    Docs

    Edit this page to fix an error or add an improvement in a merge request.
    Create an issue to suggest an improvement to this page.

    Product

    Create an issue if there's something you don't like about this feature.
    Propose functionality by submitting a feature request.
    Join First Look to help shape new features.

    Feature availability and product trials

    View pricing to see all GitLab tiers and features, or to upgrade.
    Try GitLab for free with access to all features for 30 days.

    Get Help

    If you didn't find what you were looking for, search the docs.
    If you want help with something specific and could use community support, post on the GitLab forum.
    For problems setting up or using this feature (depending on your GitLab subscription).

    Request support