github.com/ezbuy/gauge@v0.9.4-0.20171013092048-7ac5bd3931cd/build/publish_nightly_to_bintray.sh (about)

     1  #!/bin/sh
     2  
     3  # Copyright 2015 ThoughtWorks, Inc.
     4  
     5  # This file is part of Gauge.
     6  
     7  # Gauge is free software: you can redistribute it and/or modify
     8  # it under the terms of the GNU General Public License as published by
     9  # the Free Software Foundation, either version 3 of the License, or
    10  # (at your option) any later version.
    11  
    12  # Gauge is distributed in the hope that it will be useful,
    13  # but WITHOUT ANY WARRANTY; without even the implied warranty of
    14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15  # GNU General Public License for more details.
    16  
    17  # You should have received a copy of the GNU General Public License
    18  # along with Gauge.  If not, see <http://www.gnu.org/licenses/>.
    19  
    20  set -e
    21  
    22  if [ -z "$PACKAGE" ]; then
    23    echo "PACKAGE is not set"
    24    exit 1
    25  fi
    26  
    27  if [ -z "$PACKAGE_TYPE" ]; then
    28    echo "PACKAGE_TYPE is not set"
    29    exit 1
    30  fi
    31  
    32  if [ -z "$BINTRAY_USER" ]; then
    33    echo "BINTRAY_USER is not set"
    34    exit 1
    35  fi
    36  
    37  if [ -z "$BINTRAY_API_KEY" ]; then
    38    echo "BINTRAY_API_KEY is not set"
    39    exit 1
    40  fi
    41  
    42  if [ -z "$BINTRAY_PACKAGE" ]; then
    43      BINTRAY_PACKAGE="Nightly"
    44  fi
    45  
    46  if [ -z "$RENAME" ]; then
    47      RENAME=0
    48  fi
    49  
    50  # Use BINTRAY_REPO environment variable if package name is different from bintray repository's name.
    51  if [ -z "$BINTRAY_REPO" ]; then
    52      BINTRAY_REPO=$PACKAGE
    53  fi
    54  
    55  command -v jq >/dev/null 2>&1 || { echo >&2 "jq is not installed, aborting."; exit 1; }
    56  
    57  PACKAGE_FILE_PREFIX=$(echo $PACKAGE | tr '[:upper:]' '[:lower:]')
    58  
    59  function setVersion () {
    60      VERSION=$(ls $PACKAGE_FILE_PREFIX* | head -1 | sed "s/\.[^\.]*$//" | sed "s/$PACKAGE_FILE_PREFIX-//" | sed "s/-[a-z]*\.[a-z0-9_]*$//");
    61  }
    62  
    63  function renameNoVersion () {
    64      if [ "$NOVERSION" == "1" ]; then
    65          VERSION="latest"
    66          echo "Not checking for package version"
    67          for f in $PACKAGE_FILE_PREFIX*;
    68          do mv "$f" "`echo $f | sed s/$PACKAGE_FILE_PREFIX/$PACKAGE_FILE_PREFIX-$VERSION/`";
    69          done
    70      else
    71          if [ -z "$VERSION" ]; then
    72              echo "Could not determine $PACKAGE version"
    73              exit 1
    74          fi
    75      fi
    76  }
    77  
    78  function renameWithTimestamp () {
    79      if [ "$RENAME" == "1" ]; then
    80          CURR_DATE=$(date +"%Y-%m-%d")
    81  
    82          for f in $PACKAGE_FILE_PREFIX*;
    83          do mv "$f" "`echo $f | sed s/$VERSION/$VERSION.$PACKAGE_TYPE-$CURR_DATE/`";
    84          done
    85      fi
    86  }
    87  
    88  function renameToLowerCase () {
    89      for f in `ls`; do
    90          NEW_FILENAME=$(echo $f | tr '[:upper:]' '[:lower:]')
    91          if [ "$f" != "$NEW_FILENAME" ]; then
    92              mv "$f" "$NEW_FILENAME"
    93          fi
    94      done
    95  }
    96  
    97  function getPlatformFromFileName () {
    98      if [ "$NOPLATFORM" == "1" ]; then
    99          echo ""
   100      else
   101          PLATFORM=$(echo $1 | sed "s/$PACKAGE_FILE_PREFIX-//" | rev | cut -d '-' -f 1 | rev | cut -d '.' -f 1);
   102          echo "$PLATFORM/"
   103      fi
   104  }
   105  
   106  function bintrayUpload () {
   107      for i in `ls`; do
   108          PLATFORM=$( getPlatformFromFileName $i )
   109          URL="https://api.bintray.com/content/gauge/$BINTRAY_REPO/$BINTRAY_PACKAGE/$VERSION/$PLATFORM$i?publish=1&override=1"
   110  
   111          echo "Uploading to : $URL"
   112  
   113          RESPONSE_CODE=$(curl -T $i -u$BINTRAY_USER:$BINTRAY_API_KEY $URL -I -s -w "%{http_code}" -o /dev/null);
   114          if [[ "${RESPONSE_CODE:0:2}" != "20" ]]; then
   115              echo "Unable to upload, HTTP response code: $RESPONSE_CODE"
   116              exit 1
   117          fi
   118          echo "HTTP response code: $RESPONSE_CODE"
   119          if [[ $PLATFORM == "" ]]; then
   120              eval "PLATFORM_INDEPENDENT_FILE=https://dl.bintray.com/gauge/$BINTRAY_REPO/$i"
   121          elif [[ $i == *"x86_64"* ]]; then
   122              eval "${PLATFORM:0:${#PLATFORM}-1}_x86_64=https://dl.bintray.com/gauge/$BINTRAY_REPO/$PLATFORM$i"
   123          else
   124              eval "${PLATFORM:0:${#PLATFORM}-1}_x86=https://dl.bintray.com/gauge/$BINTRAY_REPO/$PLATFORM$i"
   125          fi
   126      done;
   127  }
   128  
   129  function bintraySetDownloads () {
   130      for i in `ls`; do
   131          PLATFORM=$( getPlatformFromFileName $i )
   132          URL="https://api.bintray.com/file_metadata/gauge/$BINTRAY_REPO/$PLATFORM$i"
   133  
   134          echo "Putting $i in $BINTRAY_REPO's download list"
   135          RESPONSE_CODE=$(curl -X PUT -d "{ \"list_in_downloads\": true }" -H "Content-Type: application/json" -u$BINTRAY_USER:$BINTRAY_API_KEY $URL -s -w "%{http_code}" -o /dev/null);
   136          if [[ "${RESPONSE_CODE:0:2}" != "20" ]]; then
   137              echo "Unable to put in download list, HTTP response code: $RESPONSE_CODE"
   138              exit 1
   139          fi
   140          echo "HTTP response code: $RESPONSE_CODE"
   141      done
   142  }
   143  
   144  function cleanOldNightlyVersions() {
   145      URL="https://api.bintray.com/packages/gauge/$BINTRAY_REPO/$BINTRAY_PACKAGE"
   146      versions=($(curl -X GET -H "Content-Type: application/json" -u$BINTRAY_USER:$BINTRAY_API_KEY $URL | jq -r '.versions'))
   147      for v in ${versions[@]:11}; do
   148          version=$(echo $v | sed -e 's/,//' -e 's/"//g')
   149          if [ $version !=  "]" ]; then
   150              echo "Deleting version: $version"
   151              DELETE_URL="$URL/versions/$version"
   152              RESPONSE_CODE=$(curl -X DELETE -H "Content-Type: application/json" -u$BINTRAY_USER:$BINTRAY_API_KEY $DELETE_URL -s -w "%{http_code}" -o /dev/null);
   153              if [[ "${RESPONSE_CODE:0:2}" != "20" ]]; then
   154                  echo "Unable to delete version : $v, HTTP response code: $RESPONSE_CODE"
   155                  exit 1
   156              fi
   157              echo "HTTP response code: $RESPONSE_CODE"
   158          fi
   159      done;
   160  }
   161  
   162  function snooze () {
   163      echo "\nSleeping for 30 seconds. Have a coffee..."
   164      sleep 30s;
   165      echo "Done sleeping\n"
   166  }
   167  
   168  function printMeta () {
   169      echo "Publishing package : $PACKAGE"
   170      echo "Version to be uploaded: $VERSION"
   171      echo "Bintray repository: $BINTRAY_REPO"
   172  }
   173  
   174  function updateRepo () {
   175      if [ "$UPDATE_INSTALL_JSON" != "1" ]; then
   176          return 0
   177      fi
   178      if [ -z "$GITHUB_SSH_PRIVATE_KEY" ]; then
   179          echo "GITHUB_SSH_PRIVATE_KEY is not set"
   180          exit 1
   181      fi
   182      eval $(ssh-agent) && echo -e "$GITHUB_SSH_PRIVATE_KEY" | ssh-add -
   183      git clone git@github.com:getgauge/gauge-nightly-repository.git
   184      cd gauge-nightly-repository
   185      if [[ $PLATFORM_INDEPENDENT_FILE != "" ]]; then
   186              windows_x86=$PLATFORM_INDEPENDENT_FILE
   187              windows_x86_64=$PLATFORM_INDEPENDENT_FILE
   188              linux_x86=$PLATFORM_INDEPENDENT_FILE
   189              linux_x86_64=$PLATFORM_INDEPENDENT_FILE
   190              darwin_x86=$PLATFORM_INDEPENDENT_FILE
   191              darwin_x86_64=$PLATFORM_INDEPENDENT_FILE
   192      fi
   193      versionInfo="[{\"version\": \"$VERSION\",\"gaugeVersionSupport\": {\"minimum\": \"0.6.1\",\"maximum\": \"\"},\"install\": {\"windows\": [],\"linux\": [],\"darwin\": []},\"DownloadUrls\": {\"x86\":
   194  {\"windows\": "\"$windows_x86\"", \"linux\": "\"$linux_x86\"",\"darwin\": "\"$darwin_x86\""},\"x64\": {\"windows\": "\"$windows_x86_64\"",\"linux\": "\"$linux_x86_64\"",\"darwin\": "\"$darwin_x86_64\""}}}]"
   195      if [ -z "$INSTALL_PLUGIN_JSON" ]; then
   196          echo "INSTALL_PLUGIN_JSON is not set"
   197          exit 1
   198      fi
   199  
   200      json=`cat $INSTALL_PLUGIN_JSON | jq ".versions=$versionInfo" $INSTALL_PLUGIN_JSON`
   201      echo $json | jq . > "$INSTALL_PLUGIN_JSON"
   202      git add "$INSTALL_PLUGIN_JSON"
   203      git commit -m "Updating nightly version for $INSTALL_PLUGIN_JSON"
   204      git push origin master
   205      cd ../
   206      rm -rf gauge-nightly-repository
   207  }
   208  
   209  renameToLowerCase
   210  setVersion
   211  renameNoVersion
   212  renameWithTimestamp
   213  setVersion
   214  printMeta
   215  bintrayUpload
   216  updateRepo
   217  snooze
   218  bintraySetDownloads
   219  cleanOldNightlyVersions