Create GitHub releases via command line

GitHub is offering releases for almost a year now and I love using them. Releases are first-class objects with changelogs and binary assets that present a full project history beyond Git artifacts. Following the conventions of many Git projects, releases are tied to Git tags. You can use an existing tag, or let releases create the tag when it’s published. 

Creating GitHub release through their web interface is pretty self explaining, you can read more on how to do this on the GitHub website.

But if you’re like me, you like to automate processes with shell scripts. Creating releases via a shell script means you need a way to create these releases via command line. To do this we need to use the GitHub API.

An example on how to do this:

curl --data '{"tag_name": "v1.0.0","target_commitish": "master","name": "v1.0.0","body": "Release of version 1.0.0","draft": false,"prerelease": false}'

If you’re using variables like me, you might want to use printf to build the JSON string. An example of how to do this:

API_JSON=$(printf '{"tag_name": "v%s","target_commitish": "master","name": "v%s","body": "Release of version %s","draft": false,"prerelease": false}' $VERSION $VERSION $VERSION)
curl --data "$API_JSON"

You will need an oAuth access token for the above API request. GitHub offers a nice applications panel that allows you to create a personal access token without the whole oAuth dance. Replace :owner with the repository owner, :repository with the repository name and :your_access_token with your access token. Be sure to also change the JSON data to match your release. GitHub will automatically create the tag when you use this API call.

If you’re not using GitHub or you want to create a tag without a release on GitHub. You can use the follow git commands:

git tag -a 1.0.0 -m "Release of version 1.0.0"
git push --tags

Run the above git commands in your git directory and make sure you’re on the branch you would like to create a tag of.

Related Posts

Powered By Related Posts for WordPress
Click Here to Learn More About Related Posts for WordPress

3 thoughts on “Create GitHub releases via command line

  1. How about attaching artifacts… is that possible using this approach?

  2. Use the Tagging functionality. It helps pinpoint out an important point in the history of a project. For instance:
    git tag #helps you view the available tags in the current repo
    git tag -a v1.0.0 -m “Xxx Release 1.0.0”
    that helps you create a tag at that specific time in the history of a project. For more info, look at this page

    • Can this be combined together with a commit message? I need this in order to add “[ci skip]” to the commit, since a git push will re-trigger the same job in Jenkins thus creating a loop of builds. Adding [ci skip] will help me prevent it.

Leave a Reply

Your email address will not be published. Required fields are marked *