Often there is the question to get the download URL for an asset (e.g. a setup-file) of the latest release of a project. In my case I provide an executable, which includes the version number in its name, like this:
project-X.Y.Z-setup.exe
together with the source as ZIP- and Tarball-archive. Now to get the download URLs of the assets and source archives using the GitHub API one can get and process this URL (replacing USER
and PROJECT
with the GitHub user account and projectname accordingly):
https://api.github.com/repos/USER/PROJECT/releases/latest
Note, that the assets download URL is provided by the browser_download_url
object in the asset
objects list:
{
...
"assets": [
{
...
"browser_download_url": "...",
...
}
]
}
The content provided by the API is also available to Jekyll sites hosted on GitHub pages via the site.github
namespace. You can easily check all the content of this namespace using this approach (somewhere in your code):
{{ site.github | inspect }}
Now to get the download URL of my asset, I just access the first list entry using this:
{{ site.github.latest_release.assets[0].browser_download_url }}
or this approach (less typing):
{% assign release = site.github.latest_release %}
{{ release.assets[0].browser_download_url }}
I use this to create structured data in JSON-LD for a software application. I can even access the file size, the creation and publication date. The following shows a JSON-LD snippet I add to one of my GitHub pages:
{% assign release = site.github.latest_release %}
{
"@context": "http://schema.org/",
"@type": "SoftwareApplication",
"name": "...",
"softwareVersion": "{{ release.tag_name | strip | remove: 'v' }}",
"alternateName": [
"...",
"{{ release.name }}"
],
"description": "...",
"applicationCategory": "...",
"inLanguage": ["..", ".."],
"operatingSystem": [
"...",
"..."
],
"downloadUrl": "{{ release.assets[0].browser_download_url }}",
"fileSize": "{{ release.assets[0].size | divided_by: 1024 }}",
"releaseNotes": "{{ release.html_url }}",
"license": "...",
"url": "{{ site.github.repository_url }}",
"datePublished": "{{ release.published_at }}",
"dateCreated": "{{ release.created_at }}",
"author": {%- include json/person.json -%},
"publisher": {%- include json/publisher.json -%}
}
If there is more than one asset (the source archives are not assets in my case), one probably has to use a more flexibale approach then accessing the first asset list entry via asset[0]
as shown above.
Considering there are several assets and the asset file name is created the same way for every release but includes the version number (e.g.
foo-X.Y.Z-setup.exe
orfoo-X.Y.Z-src.tar.gz
), there is also another approach, that might be to used. One can processand create the download URL like this
(because it is common to tag the version as vX.Y.Z the leading
v
is removed from the version tag in the above examples)