Wednesday, November 18, 2020

Push Azure DevOps NuGet Packages to GitHub Packages via Azure DevOps Pipelines

 Azure DevOps has two mechanisms to keep deployable packages. One is publish to pipeline itself and other way is publish to Azure DevOps artifacts feed. We can publish deployable out come of the build pipeline to Azure DevOps feed as NuGet packages. However, integration capability of modern software allows us to share these packages between different tools. This post will explains how to push a  NuGet package in Azure DevOps feed to GitHub via Azure DevOps pipelines.

Prerequisites

  1.                 NuGet package publish to  Azure DevOps feed
  2.                 GitHub PAT

Let's get started.

  • Create Azure DevOps deployment pipeline by selecting empty template.
  • Add build pipeline as the artifact to the deployment pipeline which is necessary to set continuous deployment triggers.

  • Add Download package pipeline task to download the NuGet package from the Azure DevOps feed.

  • You need to provide following inputs to download package task.

1 - Select package type from dropdown

2 - Select the Azure DevOps feed from the drop down

3 - Give the package name which going to push to GitHub

4 - Select which version need to be pushed to GitHub

5 - Give agent location to download the package

  • In this sample we copy downloaded NuGet package to new location which help us to clearly identify the location where we need to execute GitHub commands

  • You need to provide following as input to copy files task.

1 - Source folder is the location in deployment agent where we have kept the downloaded NuGet packages 

2 - We can give names of the files we need to get copied to new location. In this sample we have given NuGet package name.

3 - Destination folder inside the deployment agent where we copy the selected content.

  • Go to GitHub package location and you can find  GitHub commands to push NuGet package to the GitHub package location. Once you  select the package type you want to push to GitHub,  it will display the steps that need to be followed to push the package


  • Let's move back to Azure DevOps deployment pipeline. In this sample we use command line to execute dotnet commands mentioned in the above image.



1 - Authenticate and build up  connection to GitHub

$(SourceName) - give name to identify the source

$(Source) - GitHub package URL

User - GitHub repository owner

GitHubPAT - GitHub PAT

dotnet nuget add source $(Source) -n $(SourceName) -u $(User) -p $(GitHubPAT)

2 - Before execute the push commands change the directory to location where we keep the NuGet package

cd $(System.ArtifactsDirectory)/_publish

3 - Push the package to GitHub

dotnet nuget push $(PackageName).nupkg -k $(GitHubPAT) -s $(SourceName) --skip-duplicate

Once pipeline setup is done, we can trigger a release to push Azure DevOps NuGet package to GitHub via this pipeline. 

No comments:

Post a Comment