joshjohanning / joshjohanning.github.io

josh-ops.com | a devops blog
https://josh-ops.com
MIT License
8 stars 0 forks source link

Programmatically Download a Package Binary from GitHub Packages | josh-ops #22

Open utterances-bot opened 1 year ago

utterances-bot commented 1 year ago

Programmatically Download a Package Binary from GitHub Packages | josh-ops

Programmatically download a package binary (such as NuGet, Maven) from GitHub Packages

https://josh-ops.com/posts/github-download-from-github-packages/

mungojam commented 1 year ago

Thanks for this useful tip. I expect you will find that the GraphQL no longer works once they move the maven packages under the newer package infrastructure that both npm and Nuget are under. It brings other benefits, but it broke a script I was using to download nuget packages. I switched the script to use the REST API but then found the same as you, I couldn't use that to download the package anymore.

Your post inspired me to look into other ways and I found that the URL for downloading NuGet packages is:

https://nuget.pkg.github.com/{org-or-user}/download/{package.name}/{package.version}/{package.name}.{package.version}.nupkg

joshjohanning commented 1 year ago

Ahh, fantastic, thank you for providing this @mungojam! 🎉

joshjohanning commented 1 year ago

I'll have to update the post, but here are additional examples of downloading packages in packages v2.

Note Even if you are referencing public packages, you still need to provide a (classic) PAT

Maven

curl -H "Authorization: token ghp_xyz" -L -O \
  https://maven.pkg.github.com/joshjohanning-org/download/com/sherlock/herokupoc/1.0.0-202202122241/herokupoc-1.0.0-202202122241.jar

NuGet

curl -H "Authorization: token ghp_xyz" -L -O \
  https://nuget.pkg.github.com/joshjohanning-org/download/Wolfringo.Hosting/1.1.1/Wolfringo.Hosting.1.1.1.nupkg

npm

npm must work a bit differently; the download URL is different

# get package versions
version="0.0.3"
token="ghp_xyz"
org="joshjohanning-org"
package_name="npm-package-example"

# get url
url=$(curl -H "Authorization: token $token" -Ls https://npm.pkg.github.com/@$org/$package_name | jq --arg version $version -r '.versions[$version].dist.tarball')
# download 
curl -H "Authorization: token $token" -L -o $package_name-$version.tgz $url

RubyGems

curl -H "Authorization: token ghp_xyz" -L -O \
  https://rubygems.pkg.github.com/$org/gems/packages-migration-demo-0.2.0.gem
joshjohanning commented 5 months ago

I'll have to update the post, but here are additional examples of downloading packages in packages v2.

I finally got around to updating the post!

And added Rubygems as well 😄

arberpolis commented 3 months ago

Very helpful. Thanks Josh.