Επεξεργασία

Κοινή χρήση μέσω


Publish NuGet packages with Azure Pipelines (YAML/Classic)

Azure DevOps Services | Azure DevOps Server 2022 - Azure DevOps Server 2019

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:

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

Prerequisites

Publish NuGet packages to a feed in the same organization

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.

  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. 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

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.

    A screenshot displaying how to set up a NuGet service connection to authenticate with an external feed in a different organization.

  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 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          
    

A screenshot displaying the package successfully published to a feed in a different organization.

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)'