github.com/haalcala/mattermost-server-change-repo@v0.0.0-20210713015153-16753fbeee5f/scripts/get_latest_release.sh (about)

     1  #!/usr/bin/env bash
     2  # $1 - repo to check against
     3  # $2 - release branch pattern to match
     4  # This script will check if the current repo has version pattern $2 ('release-5.20') in the branch name.
     5  # If it does it will check for a matching release version in the $1 repo and if found check for the newest dot version.
     6  # If the branch pattern isn't found than it will default to the newest release available in $1.
     7  REPO_TO_USE=$1
     8  BRANCH_TO_USE=$2
     9  
    10  BASIC_AUTH=""
    11  
    12  # If we find a github username and token, we use that.
    13  # In CI, these variables are available and useful to avoid rate limits which is
    14  # much more strict for unauthenticated requests.
    15  if [[ ! -z "$GITHUB_USERNAME" && ! -z "$GITHUB_TOKEN" ]];
    16  then
    17    BASIC_AUTH="--user $GITHUB_USERNAME:$GITHUB_TOKEN"
    18  fi
    19  
    20  LATEST_RELEASE=$(curl \
    21    --silent \
    22    $BASIC_AUTH \
    23    "https://api.github.com/repos/$REPO_TO_USE/releases/latest")
    24  
    25  RELEASES=$(curl \
    26    --silent \
    27    $BASIC_AUTH \
    28    "https://api.github.com/repos/$REPO_TO_USE/releases")
    29  
    30  LATEST_REL=$(echo "$LATEST_RELEASE" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
    31  
    32  DRAFT=$(echo "$LATEST_RELEASE" | grep '"draft":' | sed -E 's/.*: ([^,]+).*/\1/')
    33  
    34  PRERELEASE=$(echo "$RELEASES" | grep '"prerelease":' | sed -E 's/.*: ([^,]+).*/\1/')
    35  
    36  # Check if this is a release branch
    37  THIS_BRANCH=$(git rev-parse --abbrev-ref HEAD)
    38  #THIS_BRANCH="release-5.27"# - Used to test release logic on a non release branch
    39  
    40  if [[ "$THIS_BRANCH" =~ $BRANCH_TO_USE || $DRAFT =~ "true" ]]; then
    41    VERSION_REL=${THIS_BRANCH//$BRANCH_TO_USE/v}
    42    REL_TO_USE=$(echo "$RELEASES" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/' | sed -n "/$VERSION_REL/p" | sort -rV | head -n 1)
    43  elif [[ "$THIS_BRANCH"  =~ "master" ]]; then
    44    # Get the latest release even if its a pre-release
    45    REL_TO_USE=$(echo "$RELEASES" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/' | sort -rV | head -n 1)
    46  else
    47    REL_TO_USE=$LATEST_REL
    48  fi
    49  
    50  if [[ -z "$REL_TO_USE" ]]
    51  then
    52    echo "An error has occured trying to get the latest mmctl release. Aborting. Perhaps api.github.com is down, or you are being rate-limited.";
    53    echo "Set the GITHUB_USERNAME and GITHUB_TOKEN environment variables to the appropriate values to work around Github rate-limiting.";
    54    exit 1;
    55  else
    56    echo "$REL_TO_USE"
    57  fi