Publish and upload release to github with bash and curl
You can automate publishing releases to GitHub to save some time and prevent human-factor mistakes. The hint shows you how to create the release and then upload an attachment with GitHub API using curl.
You can run such script manually from your local PC or automate processes using a CI system.
Create bash script (e.g. publish.sh) with next commands:
| !/usr/bin/env bash | |
| # user/organisation where repo is hosted and repo name e.g. for devforth/reponame: | |
| GH_REPO_USER=devforth | |
| GH_REPO=reponame | |
| GH_USER=username | |
| GH_TOKEN=`cat ~/.ghtoken` | |
| GH_TARGET=master | |
| ASSETS_PATH=build | |
| <run some build command> | |
| VERSION=`grep '"version":' version.json | cut -d\" -f4` | |
| git add -u | |
| git commit -m "$VERSION release" | |
| git push | |
| res=`curl --user "$GH_USER:$GH_TOKEN" -X POST https://api.github.com/repos/${GH_REPO_USER}/${GH_REPO}/releases \ | |
| -d " | |
| { | |
| \"tag_name\": \"v$VERSION\", | |
| \"target_commitish\": \"$GH_TARGET\", | |
| \"name\": \"v$VERSION\", | |
| \"body\": \"new version $VERSION\", | |
| \"draft\": false, | |
| \"prerelease\": false | |
| }"` | |
| echo Create release result: ${res} | |
| rel_id=`echo ${res} | python -c 'import json,sys;print(json.load(sys.stdin)["id"])'` | |
| file_name=yourproj-${VERSION}.ext | |
| curl --user "$GH_USER:$GH_TOKEN" -X POST https://uploads.github.com/repos/${GH_REPO_USER}/${GH_REPO}/releases/${rel_id}/assets?name=${file_name}\ | |
| --header 'Content-Type: text/javascript ' --upload-file ${ASSETS_PATH}/${file_name} | |
| rm ${ASSETS_PATH}/${file_name} |
Script assumes that <run some build command> will auto-update version.json file, which can be something like this:
| { | |
| "version": "1.1.5", | |
| } |
Also, it assumes that after <run some build command> file named yourproj-${VERSION}.ext will be created in ASSETS_PATH, e.g. build/yourproj-1.1.5.ext (This file will be uploaded as a release attachment)
We recommend following semver for your projects.
Creating access token
The script assumes that you store your GitHub access token in ~/.ghtoken file in your filesystem.
To create it go to the https://github.com/settings/tokens/new, and tick 'repo':
Enter some description and press Generate token.
Copy and paste token to ~/.ghtoken file:
echo "<yourValueLike:>a1d23d126223b709e8f1211thisfakecf013155" > ~/.ghtoken
Please don't store the token in the file system if you are not sure that it is protected enough from multi-user or remote access on your machine.