Trevor Hickey
Trevor Hickey

Reputation: 37894

How to release versions on GitHub through the command line?

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.

Upvotes: 22

Views: 28180

Answers (9)

VonC
VonC

Reputation: 1326736

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

Upvotes: -1

Bhadresh Arya
Bhadresh Arya

Reputation: 811

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

Upvotes: 12

Wesley
Wesley

Reputation: 525

$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.

Upvotes: -1

Med Shah
Med Shah

Reputation: 89

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

Upvotes: -1

jelmd
jelmd

Reputation: 250

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

Upvotes: 0

Hossam EL-Kashef
Hossam EL-Kashef

Reputation: 568

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

Upvotes: 1

vaab
vaab

Reputation: 10122

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.

Upvotes: 13

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?

Upvotes: 3

VonC
VonC

Reputation: 1326736

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

Upvotes: 4

Related Questions