aboutBlog

Learn DevOps Step-by-Step Tutorials and fixing related issues.

Welcome to Py-Bucket, your go-to blog for DevOps tutorials and production issues fixes guide.

  • ✔ Beginner-friendly DevOps guides
  • ✔ Real-world production issues and fixes

How to Create and Publish a NuGet Package to Azure Artifacts

How to Create and Publish a NuGet Package to Azure Artifacts
nuget


What is Nuget?

NuGet is the essential package management tool for .NET development, simplifying the creation, sharing, and consumption of reusable code packages. Below, we break down key NuGet commands—Pack, Push, and Spec—along with practical examples and best practices.
for Basics please refer : Microsoft Document

NuGet Pack: Creating a NuGet Package

The nuget pack command generates a .nupkg file from your project, bundling compiled output and dependencies for seamless distribution.

nuget workflow


Key Features:

1. Converts project files (.csproj) into a deployable NuGet package.

2. Automatically includes required dependencies.

3. Supports metadata customization via a .nuspec file or command-line arguments.

Example Usage:

nuget pack MyProject.csproj  

Customizing Package Metadata:

To override default settings, use a .nuspec file or pass arguments:Copy

nuget pack MyProject.csproj -Properties Configuration=Release -OutputDirectory C:\packages -Version 1.0.0  

NuGet Push: Publishing Packages to a Server

The nuget push command uploads your .nupkg file to a NuGet server (like NuGet.org or a private feed).

Requirements:

1. Authentication (API key or credentials).

2. Valid .nupkg file path.

Example Usage

nuget push MyProject.1.0.0.nupkg -Source https://nuget.example.com/api/v2/package -ApiKey your-api-key  

NuGet Spec: Generating a .nuspec File

The nuget spec command creates a basic .nuspec file (XML metadata) for your project, which you can edit before packaging.

Key Features:

  1. Auto-generates metadata (ID, version, dependencies).
  2. Serves as a template for package customization.

Example Usage:

nuget spec MyProject.csproj  

After editing the .nuspec file, use nuget pack to build the package:

nuget pack MyProject.nuspec  

NuGet Spec vs. NuGet Pack: What’s the Difference?

  1. nuget spec: Generates a .nuspec metadata file.
  2. nuget pack: Creates a .nupkg from either a .nuspec or .csproj file.


What is Azure Artifacts?

Azure Artifacts is Microsoft’s package management service for hosting NuGet, npm, Maven, and other packages. Integrated with Azure DevOps, it enables:

  1. Centralized package storage.

  2. Secure sharing across teams.

  3. Seamless CI/CD pipeline integration.

Benefits:

  1. Supports NuGet (C#/.NET), npm (JavaScript), and Maven (Java).

  2. Streamlines dependency management in enterprise environments.


Final Thoughts

Mastering nuget packpush, and spec ensures efficient .NET package management. For teams, Azure Artifacts provides a robust solution for hosting and sharing packages securely.

Need help with NuGet or Azure Artifacts? Please contact us.

FAQ:

Q1. What is NuGet and why is it used in .NET development?

NuGet is the official package manager for .NET development. It lets developers create, share, and consume reusable code libraries called packages. Instead of manually copying DLL files between projects, NuGet handles downloading, installing, and updating dependencies automatically — saving time and reducing version conflicts across teams.


Q2. What is the difference between nuget pack and nuget spec?

nuget spec generates a .nuspec metadata file that describes your package — including its ID, version, author, and dependencies. nuget pack uses either that .nuspec file or a .csproj directly to build the actual distributable .nupkg file. Think of nuget spec as writing the label, and nuget pack as sealing the box.


Q3. What does the nuget push command do?

nuget push uploads your compiled .nupkg file to a NuGet server — either the public NuGet.org registry or a private feed like Azure Artifacts. You need a valid API key and the server URL to authenticate the upload. Without nuget push, your package exists only on your local machine and cannot be consumed by other developers or pipelines.


Q4. What is a .nuspec file and do I always need one?

A .nuspec file is an XML metadata file that defines package properties like ID, version, description, authors, and dependencies. You don't always need one — if you're packing directly from a .csproj, NuGet can read metadata from the project file itself. However a .nuspec file gives you full control over customisation, especially useful when your package has complex dependency rules or multiple target frameworks.


Q5. What is Azure Artifacts and how is it different from NuGet.org?

NuGet.org is the public registry where anyone can publish and download open-source .NET packages. Azure Artifacts is Microsoft's private package management service inside Azure DevOps — it lets your team host internal packages securely without exposing them publicly. Azure Artifacts supports not just NuGet but also npm, Maven, and Python packages, making it a central dependency store for enterprise teams working across multiple tech stacks.


Q6. How do I push a NuGet package to Azure Artifacts instead of NuGet.org?

Replace the -Source URL in your nuget push command with your Azure Artifacts feed URL, which follows the format https://pkgs.dev.azure.com/{organisation}/{project}/_packaging/{feedName}/nuget/v3/index.json. You also need to authenticate using a Personal Access Token (PAT) from Azure DevOps instead of a NuGet.org API key. The rest of the command syntax stays identical.


Q7. Can I use NuGet packages in a CI/CD pipeline with Azure DevOps?

Yes — Azure DevOps pipelines have built-in NuGet tasks that handle restore, pack, and push operations without manually running CLI commands. In your YAML pipeline, use the NuGetCommand@2 task with the appropriate command (restore, pack, or push) to fully automate package management as part of your build and release process. Azure Artifacts integrates directly with these tasks, so authentication is handled automatically within the pipeline.

==========================

You may like these blogs as well:

Azure devops pipeline jobs, stages, tasks

Featured posts

🔥 Featured Tutorials

Devops

DevOps Tutorials

Author Details

Hi, I'm Prashant — a full-time software engineer with a passion for automation, DevOps, and sharing what I learn. I started Py-Bucket to document my journey through tools like Docker, Kubernetes, Azure DevOps, and PowerShell scripting — and to help others navigate the same path. When I’m not coding or writing, I’m experimenting with side projects, exploring productivity hacks, or learning how to build passive income streams online. This blog is my sandbox — and you're welcome to explore it with me. Get in touch or follow me for future updates!