github.sh 3.49 KB
Newer Older
Benoit Lavenier's avatar
Benoit Lavenier committed
1 2 3 4 5 6 7 8 9 10 11 12 13
#!/bin/bash

### Control that the script is run on `dev` branch
branch=`git rev-parse --abbrev-ref HEAD`
if [[ "$branch" = "master" ]];
then
  echo ">> This script must be run under a branch (tag)"
  exit 1
fi

### Get version to release
current=`grep -m1 -P "\<version>[0-9A−Z.]+(-\w*)?</version>" pom.xml | grep -oP "\d+.\d+.\d+(-\w*)?"`
echo "Current version: $current"
14
remote_tag=cesium-plus-pod-$current
Benoit Lavenier's avatar
Benoit Lavenier committed
15 16

### Get repo URL
17
REPO="duniter/cesium-plus-pod"
Benoit Lavenier's avatar
Benoit Lavenier committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
REPO_URL=https://api.github.com/repos/$REPO

###  get auth token
GITHUB_TOKEN=`cat ~/.config/duniter/.github`
if [[ "_$GITHUB_TOKEN" != "_" ]]; then
    GITHUT_AUTH="Authorization: token $GITHUB_TOKEN"
else
    echo "Unable to find github authentication token file: "
    echo " - You can create such a token at https://github.com/settings/tokens > 'Generate a new token'."
    echo " - Then copy the token and paste it in the file '~/.config/duniter/.github' using a valid token."
    exit 1
fi

case "$1" in
  del)
33
    result=`curl -i "$REPO_URL/releases/tags/$remote_tag"`
Benoit Lavenier's avatar
Benoit Lavenier committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    release_url=`echo "$result" | grep -P "\"url\": \"[^\"]+"  | grep -oP "$REPO_URL/releases/\d+"`
    if [[ $release_url != "" ]]; then
        echo "Deleting existing release..."
        curl -H 'Authorization: token $GITHUB_TOKEN'  -XDELETE $release_url
    fi
  ;;

  pre|rel)

    if [[ $1 = "pre" ]]; then
      prerelease="true"
    else
      prerelease="false"
    fi

    description=`echo $2`
    if [[ "_$description" = "_" ]]; then
        description="Release v$current"
    fi

54
    result=`curl -s -H ''"$GITHUT_AUTH"'' "$REPO_URL/releases/tags/$remote_tag"`
Benoit Lavenier's avatar
Benoit Lavenier committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68
    release_url=`echo "$result" | grep -P "\"url\": \"[^\"]+" | grep -oP "https://[A-Za-z0-9/.-]+/releases/\d+"`
    if [[ "_$release_url" != "_" ]]; then
        echo "Deleting existing release... $release_url"
        result=`curl -H ''"$GITHUT_AUTH"'' -s -XDELETE $release_url`
        if [[ "_$result" != "_" ]]; then
            error_message=`echo "$result" | grep -P "\"message\": \"[^\"]+" | grep -oP ": \"[^\"]+\""`
            echo "Delete existing release failed with error$error_message"
            exit 1
        fi
    else
        echo "Release not exists yet on github."
    fi

    echo "Creating new release..."
69
    echo " - tag: $remote_tag"
Benoit Lavenier's avatar
Benoit Lavenier committed
70
    echo " - description: $description"
71
    result=`curl -H ''"$GITHUT_AUTH"'' -s $REPO_URL/releases -d '{"tag_name": "'"$remote_tag"'","target_commitish": "master","name": "'"$current"'","body": "'"$description"'","draft": false,"prerelease": '"$prerelease"'}'`
72
    #echo "$result"
Benoit Lavenier's avatar
Benoit Lavenier committed
73 74 75 76 77 78
    upload_url=`echo "$result" | grep -P "\"upload_url\": \"[^\"]+"  | grep -oP "https://[A-Za-z0-9/.-]+"`

    ###  Sending files
    echo "Uploading files... to $upload_url"
    dirname=`pwd`

79
    result=`curl -s -H ''"$GITHUT_AUTH"'' -H 'Content-Type: application/zip' -T "$dirname/cesium-plus-pod-assembly/target/cesium-plus-pod-$current-standalone.zip" "$upload_url?name=cesium-plus-pod-$current-standalone.zip"`
Benoit Lavenier's avatar
Benoit Lavenier committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    browser_download_url=`echo "$result" | grep -P "\"browser_download_url\":[ ]?\"[^\"]+" | grep -oP "\"browser_download_url\":[ ]?\"[^\"]+"  | grep -oP "https://[A-Za-z0-9/.-]+"`
    echo " - $browser_download_url"

    echo "-----------------------------------------"
    echo "Successfully uploading files to github !"

    ;;
  *)
    echo "Missing arguments"
    echo "Usage:"
    echo " > ./github.sh del|pre|rel <release_description>"
    echo "With:"
    echo " - del: delete existing release"
    echo " - pre: use for pre-release"
    echo " - rel: for full release"
    exit 1
    ;;
esac