Skip to content

Latest commit

 

History

History
300 lines (194 loc) · 13.7 KB

File metadata and controls

300 lines (194 loc) · 13.7 KB
title description ms.assetid ms.topic ms.date monikerRange recommendations
Publish NuGet packages with Azure Pipelines
Learn how to publish NuGet packages to internal and external feeds using Classic and YAML Pipelines.
29101A33-7C17-437C-B61D-DF7AA4CB9EA2
tutorial
09/11/2024
>= azure-devops-2020
true

Publish NuGet packages with Azure Pipelines (YAML/Classic)

[!INCLUDE version-lt-eq-azure-devops]

Using Azure Pipelines, you can publish your NuGet packages to Azure Artifacts feeds in your organization, in other organizations, and to public registries such as nuget.org, using either Classic or YAML pipelines. In this article, you'll learn how to:

[!div class="checklist"]

  • Publish packages to an internal feed
  • Publish packages to a feed in a different organization
  • Package versioning

Prerequisites

Note

If you're using Ubuntu 24.04 or higher, you must use the NuGetAuthenticate task with the .NET CLI instead of the nuget.exe. See Support for newer Ubuntu hosted images for more details.

Publish NuGet packages to a feed in the same organization

::: moniker range="azure-devops-2020"

  1. Sign in to your Azure DevOps organization, and then navigate to your project.

  2. Select Pipelines, and then select your pipeline definition.

  3. Select Edit, and then add the following snippet to your YAML pipeline.

steps:
- task: NuGetToolInstaller@1                            # Minimum required NuGet version: 4.8.0.5385+.
  displayName: 'NuGet Tool Installer'

- task: NuGetAuthenticate@0
  displayName: 'NuGet Authenticate'

- script: |
      nuget.exe push -Source "https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/nuget/v3/index.json" -ApiKey az $(Build.ArtifactStagingDirectory)\*.nupkg
  displayName: Push
  1. Navigate to the Azure DevOps portal, and then select your project.

  2. Select Pipelines, and then select your pipeline definition.

  3. Select Edit, and then select the + sign to add a new task. Add the NuGet tool installer, NuGet Authenticate and Command line tasks to your pipeline definition. You can leave the NuGet tool installer and NuGet Authenticate tasks with their default settings and configure the Command line task as follows:

    :::image type="content" source="media/nuget/nuget-push-cli-task.png" alt-text="A screenshot displaying how to configure the publish task in Azure Pipelines." lightbox="media/nuget/nuget-push-cli-task.png":::

    • Display name: Push.
    • Script:
      nuget.exe push -Source "https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/nuget/v3/index.json" -ApiKey az $(Build.ArtifactStagingDirectory)\*.nupkg
      

::: moniker-end

::: moniker range=">= azure-devops-2022"

  1. Sign in to your Azure DevOps organization, and then navigate to your project.

  2. Select Pipelines, and then select your pipeline definition.

  3. Select Edit, and then add the following snippet to your YAML pipeline.

steps:
- task: NuGetToolInstaller@1                            # Minimum required NuGet version: 4.8.0.5385+.
  displayName: 'NuGet Tool Installer'

- task: NuGetAuthenticate@1
  displayName: 'NuGet Authenticate'

- script: |
      nuget.exe push -Source "https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/nuget/v3/index.json" -ApiKey az $(Build.ArtifactStagingDirectory)\*.nupkg
  displayName: Push
  1. Sign in to your Azure DevOps organization, and then navigate to your project.

  2. Select Pipelines, and then select your pipeline definition.

  3. Select Edit, and then select the + sign to add a new task. Add the NuGet tool installer, NuGet Authenticate and Command line tasks to your pipeline definition. You can leave the NuGet tool installer and NuGet Authenticate tasks with their default settings and configure the Command line task as follows:

    :::image type="content" source="media/nuget/nuget-push-cli-task.png" alt-text="A screenshot displaying how to configure the CLI publish task in Azure Pipelines." lightbox="media/nuget/nuget-push-cli-task.png":::

    • Display name: Push.
    • Script:
      nuget.exe push -Source "https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/nuget/v3/index.json" -ApiKey az $(Build.ArtifactStagingDirectory)\*.nupkg
      

::: moniker-end

Note

To publish your packages to a feed using Azure Pipelines, make sure that both the Project Collection Build Service and your project's Build Service identities are granted the Feed Publisher (Contributor) role assigned in your feed settings. See Manage permissions for more details.

Publish NuGet packages to a feed in another organization

To publish your NuGet packages to a feed in a different Azure DevOps organization, you must first create a personal access token (PAT) in the target organization. Navigate to the organization hosting your target feed and Create a personal access token with Packaging > Read & write scope. Once the PAT is created, copy and store it in a secure location, as you'll need it in the following section to set up a service connection.

  1. Sign in to the Azure DevOps organization where your pipeline will run, and then navigate to your project.

  2. Navigate to your Project settings > Service connections.

  3. Select New service connection, select NuGet, and then select Next.

  4. Select External Azure DevOps Server as the Authentication method, and then enter your target Feed URL. Paste the Personal Access Token you created earlier, provide a name for your service connection, and check Grant access permission to all pipelines if applicable to your scenario.

  5. Select Save when you're done.

    :::image type="content" source="media/nuget/nuget-service-connection-external-devops-server.png" alt-text="A screenshot displaying how to set up a NuGet service connection to authenticate with an external feed in a different organization." lightbox="media/nuget/nuget-service-connection-external-devops-server.png":::

::: moniker range=">= azure-devops-2022"

  1. Sign in to your Azure DevOps organization, and then navigate to your project.

  2. Select Pipelines, and then select your pipeline definition.

  3. Select Edit, and then add the following snippet to your YAML pipeline.

    - task: NuGetToolInstaller@1                                # Minimum required NuGet version: 4.8.0.5385+.
      displayName: 'NuGet Tool Installer'
    
    - task: NuGetAuthenticate@1
      inputs:
        nuGetServiceConnections: <SERVICE_CONNECTION_NAME>
        
    - script: |
          nuget.exe push -Source "https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/nuget/v3/index.json" -ApiKey az $(Build.ArtifactStagingDirectory)\*.nupkg
      displayName: Push       
  1. Sign in to your Azure DevOps organization, and then navigate to your project.

  2. Select Pipelines, and then select your pipeline definition.

  3. Select Edit, and then select the + sign to add a new task. Add the NuGet tool installer, NuGet Authenticate and Command line tasks to your pipeline definition. You can leave the NuGet tool installer with its default settings and configure the other tasks as follows:

    :::image type="content" source="media/nuget/nuget-push-cli-task.png" alt-text="A screenshot displaying how to configure the publish task to a feed in another organization." lightbox="media/nuget/nuget-push-cli-task.png":::

    1. NuGet Authenticate task: select your service connection from the Service connection credentials for feeds outside this organization dropdown menu.

    2. Command line task:

      • Display name: Push.
      • Script:
        nuget.exe push -Source "https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/nuget/v3/index.json" -ApiKey az $(Build.ArtifactStagingDirectory)\*.nupkg
        

::: moniker-end

::: moniker range="azure-devops-2020"

  1. Sign in to your Azure DevOps organization, and then navigate to your project.

  2. Select Pipelines, and then select your pipeline definition.

  3. Select Edit, and then add the following snippet to your YAML pipeline.

    - task: NuGetToolInstaller@1                            # Minimum required NuGet version: 4.8.0.5385+.
      displayName: 'NuGet Tool Installer'
    
    - task: NuGetAuthenticate@0
      inputs:
        nuGetServiceConnections: <SERVICE_CONNECTION_NAME>
        
    - script: |
        nuget.exe push -Source "https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/nuget/v3/index.json" -ApiKey az $(Build.ArtifactStagingDirectory)\*.nupkg
      displayName: Push          
  1. Sign in to your Azure DevOps organization, and then navigate to your project.

  2. Select Pipelines, and then select your pipeline definition.

  3. Select Edit, and then select the + sign to add a new task. Add the NuGet tool installer, NuGet Authenticate and Command line tasks to your pipeline definition. You can leave the NuGet tool installer with its default settings and configure the other tasks as follows:

    :::image type="content" source="media/nuget/nuget-push-cli-task.png" alt-text="A screenshot displaying how to configure the CLI publish task to a feed in another organization." lightbox="media/nuget/nuget-push-cli-task.png":::

    1. NuGet Authenticate task: select your service connection from the Service connection credentials for feeds outside this organization dropdown menu.

    2. Command line task:

      • Display name: Push.
      • Script:
        nuget.exe push -Source "https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/nuget/v3/index.json" -ApiKey az $(Build.ArtifactStagingDirectory)\*.nupkg
        

::: moniker-end

:::image type="content" source="media/nuget/package-published-to-external-feed.png" alt-text="A screenshot displaying the package successfully published to a feed in a different organization." lightbox="media/nuget/package-published-to-external-feed.png":::

NuGet task package versioning

Azure Pipelines supports Semantic Versioning and provides the following configuration options for NuGet tasks:

  • Use the date and time (Classic) | byPrereleaseNumber (YAML): Your package version will follow the format: Major.Minor.Patch-ci-datetime where you have the flexibility to customize the Major, Minor, and Patch values.

  • Use an environment variable (Classic) | byEnvVar (YAML): Your package version is set to the value of the specified environment variable.

  • Use the build number (Classic) | byBuildNumber (YAML): Your package version is set to the build number. Make sure you define the build number format in your pipeline Options as $(BuildDefinitionName)_$(Year:yyyy).$(Month).$(DayOfMonth)$(Rev:.r). To specify the format in YAML, add a name: property at the root of your pipeline and define your format.

The following is an example demonstrating how to use the date and time versioning to generate a SemVer-compliant package formatted as: Major.Minor.Patch-ci-datetime.

variables:
  Major: '1'
  Minor: '0'
  Patch: '0'

steps:
- task: NuGetCommand@2
  inputs:
    command: pack
    versioningScheme: byPrereleaseNumber
    majorVersion: '$(Major)'
    minorVersion: '$(Minor)'
    patchVersion: '$(Patch)'

Note

DotNetCore and DotNetStandard packages should be packaged with the DotNetCoreCLI@2 task to avoid System.InvalidCastExceptions. See the .NET Core CLI task for more details.

task: DotNetCoreCLI@2
inputs:
    command: pack
    versioningScheme: byPrereleaseNumber
    majorVersion: '$(Major)'
    minorVersion: '$(Minor)'
    patchVersion: '$(Patch)'
  1. Sign in to your Azure DevOps organization, and then navigate to your project.

  2. Select Pipelines, and then select your pipeline definition.

  3. Select Edit, and then select your NuGet task.

  4. Make sure the command is set to Pack, then under Pack options, select Use the date and time from the dropdown menu.

    :::image type="content" source="media/package-versioning-classic.png" alt-text="A screenshot displaying how to enable package versioning in a classic pipeline.":::


Related content