2

Hello I'm trying to extract a version number from a github release site. Which I later need for a Ansible role.

For example I want to get the exact neovim version number of the latest release from github. So far I've got this:

curl -s https://api.github.com/repos/neovim/neovim/releases/latest | grep NVIM | cut -c 1-35

Output is:

  "body": "```\nNVIM v0.9.0\nBuild

I tried to extract the version number using sed but I didnt get it to work.

Unfortunately this blog article doesn't also work for me https://fabianlee.org/2021/02/16/bash-determining-latest-github-release-tag-and-version/

0

3 Answers 3

1

Since this repository publishes tags other than plain versions, you need to retrieve the full page of latest releases and look for a tag starting with “v” followed by a digit:

$ curl -s https://api.github.com/repos/neovim/neovim/releases | \
  jq -r 'first(.[].tag_name | select(test("^v[0-9]")))'
v0.9.0

This will work for any repository which published releases with a tag reflecting a version starting with “v”.

A more general approach is to look for tags only:

curl -s https://api.github.com/repos/neovim/neovim/tags | \
jq -r 'first(.[].name | select(test("^v[0-9]")))'

This will also work on projects which don’t publish GitHub releases. On projects which do publish releases, this will match any tag, not just those corresponding to releases.

Some projects use version tags without a leading “v”; the regular expression can be refined to allow for that:

curl -s https:/api.github.com/repos/akucukelbir/resmap/tags | \
jq -r 'first(.[].name | select(test("^v?[0-9]")))'

Some projects don’t push tags at all; for those, there’s no general solution.

2
  • I think the OP was asking for a general solution, neovim was just an example. Way too many packages do not version the releases in a sensible fashion. Commented Apr 15, 2023 at 19:02
  • @doneal24 fair point, this will work for any repo with v... releases. Commented Apr 15, 2023 at 19:04
0

pipe your command to :

sed -E 's/^.*v([0-9.]*).*$/\1/'
3
  • This works for neovim but it is not a general solution. Try you addition on curl -sL https://api.github.com/repos/kuixu/ResMap. Commented Apr 15, 2023 at 19:01
  • @doneal24 that repo doesn’t have any metadata identifying releases so there isn’t anything generic that can be used there... Commented Apr 16, 2023 at 7:16
  • Quite common for bioinformatics packages. Many academic groups don’t care to or know how to maintain metadata. I never count on it in my puppet rules. Commented Apr 16, 2023 at 15:21
0

with gron and GNU grep:

$ gron https://api.github.com/repos/neovim/neovim/releases/latest |
    grep -oP '^json.body = ".*\KNVIM v(\d+\.?)+'

Or with Perl:

$ gron https://api.github.com/repos/neovim/neovim/releases/latest |
    perl -pe 'print $& if /^json.body = ".*\KNVIM v(\d+\.?)+/' 

Output

NVIM v0.9.0

As you probably noticed, the API don't generate same JSON between projects.

4
  • As mentioned in other answers, this works for neovim but the OP is looking for a general solution. The repository has set version numbers in a specified fashion for this to work. Commented Apr 15, 2023 at 19:42
  • The API don't generate same JSON between projects. Commented Apr 15, 2023 at 19:48
  • What is the version from ResMap? Commented Apr 15, 2023 at 20:00
  • From the api the version is null. Running from memory I think the package reports something like 0.1.x, not unusual for bioinformatics packages. Commented Apr 15, 2023 at 20:19

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.