Hands-on Demo – Install, delete and restore packages in GitHub Packages
In this demo, we will walk through the processes of installing, deleting, and restoring packages in GitHub Packages for different package types such as NuGet (for .NET) and npm (for JavaScript). These steps will involve configuring your environment, authenticating with GitHub Packages, and performing the actions on the packages.
Prerequisites
GitHub Account: You need a GitHub account with access to the repository hosting the package.
GitHub Repository: A repository where the package is hosted (either public or private).
Personal Access Token (PAT): You need a PAT with appropriate permissions (
read:packages,write:packages,delete:packages).CLI Tools: Depending on the type of package (e.g., NuGet or npm), you’ll need the respective CLI tools installed (e.g.,
dotnetfor NuGet,npmfor npm packages).
1. Install a Package from GitHub Packages
To install a package from GitHub Packages, you need to authenticate with GitHub, specify the source of the package, and then run the installation command.
1.1. Install a NuGet Package
Generate Personal Access Token (PAT) with
read:packagesandwrite:packagesscopes from your GitHub account.Configure the
nuget.config** file** to point to GitHub Packages.
Example configuration in nuget.config:
xxxxxxxxxx111<configuration>2 <packageSources>3 <add key="GitHubPackages" value="https://nuget.pkg.github.com/YOUR_ORG_NAME/index.json" />4 </packageSources>5 <packageSourceCredentials>6 <GitHubPackages>7 <add key="Username" value="YOUR_GITHUB_USERNAME" />8 <add key="Password" value="YOUR_PAT" />9 </GitHubPackages>10 </packageSourceCredentials>11</configuration>Install the NuGet Package:
Run the following command to install the package into your project:
xxxxxxxxxx11dotnet add package MY_PACKAGE_NAME --version VERSION_NUMBER --source "GitHubPackages"Replace MY_PACKAGE_NAME with the name of the package and VERSION_NUMBER with the version you want to install.
1.2. Install an npm Package
Generate Personal Access Token (PAT) with
read:packagesandwrite:packagesscopes.Authenticate with GitHub Packages:
Run the following command to authenticate with GitHub:
xxxxxxxxxx11npm login --registry=https://npm.pkg.github.com --scope=@YOUR_ORG_NAMEInstall the npm Package:
Once authenticated, run the following command to install the npm package:
xxxxxxxxxx11npm install @YOUR_ORG_NAME/MY_PACKAGE_NAME@VERSIONReplace MY_PACKAGE_NAME with the name of the package and VERSION with the version you wish to install.
2. Delete a Package in GitHub Packages
You can delete packages using either the GitHub UI or the GitHub API. For this demo, we’ll focus on using the GitHub API and GitHub CLI to delete a package version.
2.1. Delete a NuGet Package
To delete a NuGet package version,
You need to use the GitHub API (since the GitHub UI doesn’t directly support deleting NuGet packages).
Authenticate using your PAT.
Delete the NuGet package version using the
curlcommand:
xxxxxxxxxx21curl -X DELETE -H "Authorization: Bearer YOUR_PAT" \2"https://api.github.com/orgs/YOUR_ORG_NAME/packages/nuget/MY_PACKAGE_NAME/versions/VERSION_ID"Replace:
YOUR_PATwith your personal access token.YOUR_ORG_NAMEwith your GitHub organization or username.MY_PACKAGE_NAMEwith the name of the NuGet package.VERSION_IDwith the specific version ID of the package you want to delete.
2.2. Delete an npm Package
To delete an npm package version, you can use the GitHub API or npm CLI.
Authenticate using your PAT
Delete the npm package version using
npm:
xxxxxxxxxx11npm unpublish @YOUR_ORG_NAME/MY_PACKAGE_NAME@VERSION --registry=https://npm.pkg.github.comReplace:
YOUR_ORG_NAMEwith your GitHub organization or username.MY_PACKAGE_NAMEwith the name of the npm package.VERSIONwith the specific version you want to delete.
3. Restore (Reinstall) a Package from GitHub Packages
Restoring or reinstalling a package is essentially the same as installing a package (as explained in the Install section).
3.1. Restore a NuGet Package
To restore a NuGet package that was previously installed, run the following command:
xxxxxxxxxx11dotnet restoreThis command will restore all NuGet packages in your project, including those hosted on GitHub Packages (as long as they are correctly referenced in your nuget.config file).
3.2. Restore an npm Package
To restore an npm package, simply run the npm install command:
xxxxxxxxxx11npm installThis command will restore all npm packages listed in your project's package.json, including those from GitHub Packages.
4. Example Workflow: Complete Steps to Install, Delete, and Restore a Package
Scenario: You are working with an npm package hosted in a private GitHub repository.
Authenticate with GitHub Packages:
xxxxxxxxxx11npm login --registry=https://npm.pkg.github.com --scope=@YOUR_ORG_NAMEInstall the npm package:
xxxxxxxxxx11npm install @YOUR_ORG_NAME/MY_PACKAGE_NAME@1.0.0Delete the npm package (if needed):
xxxxxxxxxx11npm unpublish @YOUR_ORG_NAME/MY_PACKAGE_NAME@1.0.0 --registry=https://npm.pkg.github.comRestore the npm package after deletion (to make sure it still works after re-publishing):
xxxxxxxxxx11npm install @YOUR_ORG_NAME/MY_PACKAGE_NAME@1.0.0 --registry=https://npm.pkg.github.com5. Key Takeaways
Installing Packages:
You authenticate with GitHub Packages and then use the appropriate command (e.g., dotnet add package for NuGet or npm install for npm) to install packages.
Deleting Packages:
Packages can be deleted using the GitHub API or, for npm, via the npm unpublish command. Deleting packages in GitHub Packages requires appropriate permissions.
Restoring Packages:
Restoring a package is simply a matter of using the install command again, ensuring the correct authentication and nuget.config or npm registry settings are in place.
By following these steps, you can efficiently manage and interact with packages hosted on GitHub Packages.
Troubleshooting
Authentication Issues:
If you receive authentication errors, ensure your PAT has the correct scopes (read:packages, write:packages), and that you’re logged in using the correct credentials.
Package Visibility:
If a package isn’t appearing or cannot be installed, ensure that the package is correctly published and that you have access to it (based on the repository's visibility and access control settings).
Permission Errors:
Ensure you have the necessary permissions for deleting or managing packages, either at the repository or organization level.






















Leave a Reply