github.com/instana/go-sensor@v1.62.2-0.20240520081010-4919868049e1/release_action.sh (about)

     1  #!/bin/bash
     2  
     3  build_major() {
     4    # Only 2 scenarios:
     5    # - v1 was never released, so FOUND_VERSION_IN_TAG=0.0.0 and NEW_MAJOR_VERSION is empty. NEW_VERSION should be 1.0.0
     6    # - v2 or higher was never released, so FOUND_VERSION_IN_TAG=0.0.0 and NEW_MAJOR_VERSION is 2, 3, 4... NEW_VERSION should be 2.0.0, or higher
     7  
     8    MAJOR_VERSION=$(echo "$FOUND_VERSION_IN_TAG" | sed -En 's/([0-9]+)\.[0-9]+\.[0-9]+.*/\1/p')
     9  
    10    if [ "$MAJOR_VERSION" != "0" ]; then
    11      echo "Cannot release new major version '$NEW_MAJOR_VERSION' with existing tag $FOUND_VERSION_IN_TAG"
    12      exit 1
    13    fi
    14  
    15    if [ -z "$NEW_MAJOR_VERSION" ]; then
    16      NEW_VERSION="1.0.0"
    17    else
    18      NEW_VERSION="$NEW_MAJOR_VERSION.0.0"
    19    fi
    20  }
    21  
    22  build_minor() {
    23    # We abort the release if there is an attempt to release a minor version of a major v2 or higher that doesn't exist.
    24    # This may happen if the v2 folder exists, but the major v2 hasn't been released yet.
    25    if [ "$FOUND_VERSION_IN_TAG" = "0.0.0" ] && [ -n "$NEW_MAJOR_VERSION" ]; then
    26      echo "Cannot release a minor version of a major v2 or higher that doesn't exist"
    27      exit 1
    28    fi
    29  
    30    MINOR_VERSION=$(echo "$FOUND_VERSION_IN_TAG" | sed -En 's/[0-9]+\.([0-9]+)\.[0-9]+.*/\1/p')
    31    MAJOR_VERSION=$(echo "$FOUND_VERSION_IN_TAG" | sed -En 's/([0-9]+)\.[0-9]+\.[0-9]+.*/\1/p')
    32    MINOR_VERSION=$((MINOR_VERSION+1))
    33    NEW_VERSION="$MAJOR_VERSION.$MINOR_VERSION.0"
    34  }
    35  
    36  build_patch() {
    37    # We abort the release if there is an attempt to release a patch version of a major v2 or higher that doesn't exist.
    38    # This may happen if the v2 folder exists, but the major v2 hasn't been released yet.
    39    if [ "$FOUND_VERSION_IN_TAG" = "0.0.0" ] && [ -n "$NEW_MAJOR_VERSION" ]; then
    40      echo "Cannot release a patch version of a major v2 or higher that doesn't exist"
    41      exit 1
    42    fi
    43  
    44    PATCH_VERSION=$(echo "$FOUND_VERSION_IN_TAG" | sed -En 's/[0-9]+\.[0-9]+\.([0-9]+).*/\1/p')
    45    MINOR_VERSION=$(echo "$FOUND_VERSION_IN_TAG" | sed -En 's/[0-9]+\.([0-9]+)\.[0-9]+.*/\1/p')
    46    MAJOR_VERSION=$(echo "$FOUND_VERSION_IN_TAG" | sed -En 's/([0-9]+)\.[0-9]+\.[0-9]+.*/\1/p')
    47    PATCH_VERSION=$((PATCH_VERSION+1))
    48    NEW_VERSION="$MAJOR_VERSION.$MINOR_VERSION.$PATCH_VERSION"
    49  }
    50  
    51  IS_CORE="false"
    52  
    53  LIB_PATH=.
    54  
    55  if [ "$INSTANA_PACKAGE_NAME" = "." ]; then
    56    IS_CORE="true"
    57    echo "Releasing core module"
    58  else
    59    echo "Releasing $INSTANA_PACKAGE_NAME"
    60  fi
    61  
    62  if [ "$IS_CORE" = "false" ]; then
    63    cd instrumentation/"$INSTANA_PACKAGE_NAME" || exit
    64    LIB_PATH=instrumentation/$INSTANA_PACKAGE_NAME
    65  fi
    66  
    67  echo "lib path: $LIB_PATH"
    68  
    69  # Expected to find something like: instrumentation/instaredis/v1.5.0
    70  # This option will be used if the instrumentation has no v2 subfolder
    71  if [ "$IS_CORE" = "false" ]; then
    72    TAG_TO_SEARCH="$LIB_PATH/v[0-1].*"
    73  else
    74    TAG_TO_SEARCH="v[0-1].*"
    75  fi
    76  
    77  # Only relevant for instrumentations
    78  if [ "$IS_CORE" = "false" ]; then
    79    # Expected to identify packages with subfolders. eg: instrumentation/instaredis/v2
    80    NEW_VERSION_FOLDER=$(echo "$LIB_PATH" | grep -E "/v[2-9].*")
    81  
    82    echo "New version folder. eg: v2, v3...: $NEW_VERSION_FOLDER"
    83  
    84    # If NEW_VERSION_FOLDER has something we update TAG_TO_SEARCH
    85    if [ -n "$NEW_VERSION_FOLDER" ]; then
    86      # Expected to parse a version. eg: 2.5.0. Will extract 2 in the case of 2.5.0, which is the new major version
    87      NEW_MAJOR_VERSION=$(echo "$NEW_VERSION_FOLDER" | sed "s/.*v//")
    88  
    89      echo "New major version: $NEW_MAJOR_VERSION"
    90  
    91      # Expected to be tag name with major version higher than 1. eg: instrumentation/instaredis/v2.1.0
    92      TAG_TO_SEARCH="$LIB_PATH.*"
    93    fi
    94  fi
    95  
    96  echo "Tag to search: $TAG_TO_SEARCH"
    97  
    98  # git fetch --unshallow --tags
    99  FOUND_VERSION_IN_TAG=$(git tag -l "$TAG_TO_SEARCH" | sort -V | tail -n1 | sed "s/.*v//")
   100  
   101  echo "Version found in tags: $FOUND_VERSION_IN_TAG"
   102  
   103  # This works if the lib is new, and it was never instrumented.
   104  # But if it's a new version, eg: /v2, this will fail later. we need to fix it
   105  if [ -z "$FOUND_VERSION_IN_TAG" ]; then
   106    FOUND_VERSION_IN_TAG="0.0.0"
   107    echo "Version updated to: $FOUND_VERSION_IN_TAG, and new major version is $NEW_MAJOR_VERSION"
   108  fi
   109  
   110  if [ "$LIB_VERSION_TYPE" = "major" ]; then
   111    build_major
   112  elif [ "$LIB_VERSION_TYPE" = "minor" ]; then
   113    build_minor
   114  else
   115    build_patch
   116  fi
   117  
   118  echo "New version to release is: $NEW_VERSION"
   119  
   120  # Updates the minor version in version.go
   121  sed -i -E "s/[0-9]+\.[0-9]+\.[0-9]+/${NEW_VERSION}/" version.go | tail -1
   122  
   123  git config user.name "IBM/Instana/Team Go"
   124  git config user.email "github-actions@github.com"
   125  
   126  git add version.go
   127  git commit -m "Updated version.go to $NEW_VERSION"
   128  git push origin @
   129  
   130  # Tags to be created after version.go is merged to the main branch with the new version
   131  PATH_WITHOUT_V=$(echo "$LIB_PATH" | sed "s/\/v[0-9]*//")
   132  
   133  if [ "$IS_CORE" = "false" ]; then
   134    NEW_VERSION_TAG="$PATH_WITHOUT_V/v$NEW_VERSION"
   135  else
   136    NEW_VERSION_TAG="v$NEW_VERSION"
   137  fi
   138  
   139  echo "Releasing as draft: $RELEASE_AS_DRAFT"
   140  
   141  AS_DRAFT="-d"
   142  
   143  if [ "$RELEASE_AS_DRAFT" != "true" ]; then
   144    AS_DRAFT=""
   145  fi
   146  
   147  echo "RELEASE_VERSION=v$NEW_VERSION" >> "$GITHUB_OUTPUT"
   148  
   149  if [ "$IS_CORE" = "false" ]; then
   150    echo "RELEASE_PACKAGE=$INSTANA_PACKAGE_NAME" >> "$GITHUB_OUTPUT"
   151  else
   152    echo "RELEASE_PACKAGE=go-sensor" >> "$GITHUB_OUTPUT"
   153  fi
   154  
   155  echo "$GITHUB_TOKEN" > gh_token.txt
   156  gh auth login --with-token < gh_token.txt
   157  rm gh_token.txt
   158  
   159  git tag "$NEW_VERSION_TAG"
   160  git push origin "$NEW_VERSION_TAG"
   161  gh release create "$NEW_VERSION_TAG" $AS_DRAFT --title "$NEW_VERSION_TAG" --notes "New release $NEW_VERSION_TAG."