github.sh 5.04 KB
Newer Older
1 2 3 4 5 6 7
#!/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 \`master\` branch"
8
  exit 1
9 10
fi

11
### Get version to release
12 13 14 15
current=`grep -P "version\": \"\d+.\d+.\d+(\w*)" package.json | grep -m 1 -oP "\d+.\d+.\d+(\w*)"`
if [[ "_$current" == "_" ]]; then
  echo "Unable to read the current version in 'package.json'. Please check version format is: x.y.z (x and y should be an integer)."
  exit 1;
16
fi
17 18
echo "Current version: $current"

19
### Get repo URL
20
PROJECT_NAME=cesium
Benoit Lavenier's avatar
Benoit Lavenier committed
21
REPO="duniter/cesium"
22 23
REPO_API_URL=https://api.github.com/repos/$REPO
REPO_PUBLIC_URL=https://github.com/$REPO
24 25

###  get auth token
26
GITHUB_TOKEN=`cat ~/.config/${PROJECT_NAME}/.github`
27 28 29
if [[ "_$GITHUB_TOKEN" != "_" ]]; then
    GITHUT_AUTH="Authorization: token $GITHUB_TOKEN"
else
30
    echo "ERROR: Unable to find github authentication token file: "
31
    echo " - You can create such a token at https://github.com/settings/tokens > 'Generate a new token'."
32 33
    echo " - Then copy the token and paste it in the file '~/.config/${PROJECT_NAME}/.github' using a valid token."
    exit 1
34 35
fi

36 37
case "$1" in
  del)
38 39
    result=`curl -i "$REPO_API_URL/releases/tags/v$current"`
    release_url=`echo "$result" | grep -P "\"url\": \"[^\"]+"  | grep -oP "$REPO_API_URL/releases/\d+"`
40
    if [[ $release_url != "" ]]; then
41
        echo "Deleting existing release..."
42
        curl -H 'Authorization: token $GITHUB_TOKEN'  -XDELETE $release_url
43 44 45 46
    fi
  ;;

  pre|rel)
47
    if [[ "_$2" != "_" ]]; then
48 49 50 51 52 53 54

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

55 56 57 58
      description=`echo $2`
      if [[ "_$description" = "_" ]]; then
          description="Release v$current"
      fi
59 60

      result=`curl -s -H ''"$GITHUT_AUTH"'' "$REPO_API_URL/releases/tags/v$current"`
61
      release_url=`echo "$result" | grep -P "\"url\": \"[^\"]+" | grep -oP "https://[A-Za-z0-9/.-]+/releases/\d+"`
62 63 64
      if [[ "_$release_url" != "_" ]]; then
        echo "Deleting existing release... $release_url"
        result=`curl -H ''"$GITHUT_AUTH"'' -s -XDELETE $release_url`
65 66 67
        if [[ "_$result" != "_" ]]; then
            error_message=`echo "$result" | grep -P "\"message\": \"[^\"]+" | grep -oP ": \"[^\"]+\""`
            echo "Delete existing release failed with error$error_message"
68
            exit 1
69 70 71
        fi
      else
        echo "Release not exists yet on github."
72 73 74
      fi

      echo "Creating new release..."
75 76
      echo " - tag: v$current"
      echo " - description: $description"
77
      result=`curl -H ''"$GITHUT_AUTH"'' -s $REPO_API_URL/releases -d '{"tag_name": "v'"$current"'","target_commitish": "master","name": "'"$current"'","body": "'"$description"'","draft": false,"prerelease": '"$prerelease"'}'`
78
      #echo "DEBUG - $result"
79
      upload_url=`echo "$result" | grep -P "\"upload_url\": \"[^\"]+"  | grep -oP "https://[A-Za-z0-9/.-]+"`
80

81 82 83 84 85 86 87
      if [[ "_$upload_url" = "_" ]]; then
        echo "Failed to create new release for repo $REPO."
        echo "Server response:"
        echo "$result"
        exit 1
      fi

88
      ###  Sending files
Benoit Lavenier's avatar
Benoit Lavenier committed
89
      echo "Uploading files to ${upload_url} ..."
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
      dirname=$(pwd)

      ZIP_FILE="$dirname/platforms/web/build/${PROJECT_NAME}-v$current-web.zip"
      if [[ -f "${ZIP_FILE}" ]]; then
        result=$(curl -s -H ''"$GITHUT_AUTH"'' -H 'Content-Type: application/zip' -T "${ZIP_FILE}" "${upload_url}?name=${PROJECT_NAME}-v${current}-web.zip")
        browser_download_url=$(echo "$result" | grep -P "\"browser_download_url\":[ ]?\"[^\"]+" | grep -oP "\"browser_download_url\":[ ]?\"[^\"]+"  | grep -oP "https://[A-Za-z0-9/.-]+")
        ZIP_SHA256=$(sha256sum "${ZIP_FILE}")
        echo " - ${browser_download_url}  | SHA256 Checksum: ${ZIP_SHA256}"
      else
        echo " - ERROR: Web release (ZIP) not found! Skipping."
      fi

      APK_FILE="${dirname}/platforms/android/build/outputs/apk/release/android-release.apk"
      if [[ -f "${APK_FILE}" ]]; then
        result=$(curl -s -H ''"$GITHUT_AUTH"'' -H 'Content-Type: application/vnd.android.package-archive' -T "${APK_FILE}" "${upload_url}?name=${PROJECT_NAME}-v${current}-android.apk")
        browser_download_url=$(echo "$result" | grep -P "\"browser_download_url\":[ ]?\"[^\"]+" | grep -oP "\"browser_download_url\":[ ]?\"[^\"]+"  | grep -oP "https://[A-Za-z0-9/.-]+")
        APK_SHA256=$(sha256sum "${APK_FILE}")
        echo " - ${browser_download_url}  | SHA256 Checksum: ${APK_SHA256}"
      else
        echo "- ERROR: Android release (APK) not found! Skipping."
      fi
111

112 113 114 115
      echo "-----------------------------------------"
      echo "Successfully uploading files !"
      echo " -> Release url: ${REPO_PUBLIC_URL}/releases/tag/v${current}"
      exit 0
116 117 118
    else
      echo "Wrong arguments"
      echo "Usage:"
119
      echo " > ./github.sh del|pre|rel <release_description>"
120
      echo "With:"
121
      echo " - del: delete existing release"
122 123
      echo " - pre: use for pre-release"
      echo " - rel: for full release"
124
      exit 1
125 126 127 128
    fi
    ;;
  *)
    echo "No task given"
129
    exit 1
130 131
    ;;
esac