19

GitHub has a feature on their website that allows you to mark particular snapshots of your repository as release versions of software. Sample URL: https://github.com/github/orchestrator/releases

Is there a way I can do this from the command line, without having to log on and use the interface? I realize the feature is not a part of git, but I was hoping there is some kind of api or solution other people use to make the process automated.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271

9 Answers9

11

There are many projects offering this — the order below is just for the sake indexing things —:

  1. cheton's github-release-cli in Node (JS)
  2. c4milo's github-release in Go (aims simplicity)
  3. aktau's github-release in Go

And you can even do this directly with curl directly:

OWNER=
REPOSITORY=
ACCESS_TOKEN=
VERSION=
curl --data '{"tag_name": "v$VERSION",
                "target_commitish": "master",
                "name": "v$VERSION",
                "body": "Release of version $VERSION",
                "draft": false,
                "prerelease": false}' \
    https://api.github.com/repos/$OWNER/$REPOSITORY/releases?access_token=$ACCESS_TOKEN

from Barry Kooij's Create Github releases via command line.

If you want a full featured answer on StackOverflow: Releasing a build artifact on Github.

vaab
  • 9,685
  • 7
  • 55
  • 60
7

It can be done using github cli

gh release create <tagname> --target <branchname>

Github CLI

If you get a 400 error, check your branch name for typos

rainabba
  • 3,804
  • 35
  • 35
Bhadresh Arya
  • 754
  • 7
  • 6
5

You could use the "Create release" API of the GitHub V3 API.

POST /repos/:owner/:repo/releases

See for instance this ruby script "create-release.rb" by Mathias Lafeldt (mlafeldt):

require "net/https"
require "json"

gh_token     = ENV.fetch("GITHUB_TOKEN")
gh_user      = ARGV.fetch(0)
gh_repo      = ARGV.fetch(1)
release_name = ARGV.fetch(2)
release_desc = ARGV[3]

uri = URI("https://api.github.com")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

request = Net::HTTP::Post.new("/repos/#{gh_user}/#{gh_repo}/releases")
request["Accept"] = "application/vnd.github.manifold-preview"
request["Authorization"] = "token #{gh_token}"
request.body = {
  "tag_name"         => release_name,
  "target_commitish" => "master",
  "name"             => release_name,
  "body"             => release_desc,
  "draft"            => false,
  "prerelease"       => false,
}.to_json

response = http.request(request)
abort response.body unless response.is_a?(Net::HTTPSuccess)

release = JSON.parse(response.body)
puts release
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • here's a python version that also uploads a file: https://stackoverflow.com/questions/38153418/can-someone-give-a-python-requests-example-of-uploading-a-release-asset-in-githu/52354681#52354681 – Ciro Santilli OurBigBook.com Sep 16 '18 at 13:38
4

hub official Go-based GitHub CLI tool

https://github.com/github/hub

An Ubuntu package as added as of 19.04: https://packages.ubuntu.com/search?keywords=hub | https://github.com/github/hub/issues/718

sudo apt install hub

Otherwise, for older Ubuntu, first install Go. On Ubuntu: https://askubuntu.com/questions/959932/installation-instructions-for-golang-1-9-into-ubuntu-16-04/1075726#1075726

Then install hub:

go get github.com/github/hub

Once hub is installed, from inside your repo:

hub release create -a prebuilt.zip -m 'release title' tag-name

This:

  • prompts for your password the first time, and then automatically creates and stores an API token locally
  • creates a non annotated tag on the remote called tag-name
  • creates a release associated to that tag
  • uploads prebuilt.zip as an attachment

You can also provide your existing API token with the GITHUB_TOKEN environment variable.

For other release operations, see:

hub release --help

Tested on hub de684cb613c47572cc9ec90d4fd73eef80aef09c.

Python example without any dependencies

If you are like me and don't want to install yet another language:

Can someone give a python requests example of uploading a release asset in github?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
  • 1
    You can get hub precompiled from many package managers, no need to install go :) But thanks for the post – The Unfun Cat Oct 01 '19 at 07:34
  • @TheUnfunCat hi, thanks for pinging me, a package was juts made available for 19.04, so I updated the answer. Still, when something is available from the "per-language-package" manager, I tend to use that one ;-) – Ciro Santilli OurBigBook.com Oct 01 '19 at 20:05
1

you can do that using the GitHub CLI

To create a release from an annotated git tag, first create one locally with git, push the tag to GitHub, then run this command.

gh release create <tag> [<files>...] --target <branchname>

Options

-d, --draft Save the release as a draft instead of publishing it

-n, --notes string Release notes

-F, --notes-file file Read release notes from file

-p, --prerelease Mark the release as a prerelease

, --target branch Target branch or full commit SHA (default: main branch)

-t, --title string Release title

0

This can be done using a simple curl command:

curl -X POST -u YOURGITUSERNAME:YOURTOKEN --data '{"tag_name": "YOURTAGNAME","target_commitish": "YOURREPO","name": "YOURTAGNAME","body": "YOUR TAG DESCRIPTION","draft": false,"prerelease": false}' https://api.github.com/repos/YOURGITSITE/YOURREPO/releases
Med Shah
  • 91
  • 1
  • 2
0
$version = 'v1.0.0'
$data='{"tag_name": "${version}", "target_commitish": "master", "name": "${version}", "body": "Release of version ${version}", "draft": false, "prerelease": false}'
curl -X POST -H "Authorization: token $(git_token)" -d $ExecutionContext.InvokeCommand.ExpandString($data) https://api.github.com/repos/$OWNER/$REPOSITORY/releases

I was using Powershell in azure DevOps. git_token is a variable defined in my build pipeline. Hope it can help others.

Wesley
  • 387
  • 3
  • 4
0

When using gh release create, make sure to use the new --verify-tag option with gh 2.21.0 (Dec. 2022), from PR 6632 which fixes issue 6566:

When running gh release create <tag> --verify-tag, we query among repository tags via the GitHub API before creating the release, and abort the command if the tag was not found.

Error message:

tag %s doesn't exist in the repo %s, aborting due to --verify-tag flag
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
-1

Assuming you have checked out the right branch from the github repo (origin) and it is in synced with it, to e.g. automagically create a version 2.5.0 release, do:

git tag -a -m 'your comment' v2.5.0
git push origin v2.5.0
jelmd
  • 119
  • 3
  • this creates a tag, not a release – Megan Hardy Nov 09 '22 at 19:08
  • Of course this creates a tag and until Microsoft took over, pushing it to github actually caused the creation of a release. You obviously missed the '-a' option. – jelmd Nov 11 '22 at 18:09
  • https://git-scm.com/docs/git-tag -a creates an ANNOTATED TAG which is not the same as a release – Megan Hardy Jan 23 '23 at 21:15
  • Creating an annotated tag with a proper version string and pushing it to github caused github to create a release. – jelmd Jan 31 '23 at 05:00