Update tools/change-chart-version.sh to work from Jenkins 23/15423/1
authorgarciadeblas <gerardo.garciadeblas@telefonica.com>
Fri, 26 Sep 2025 08:11:36 +0000 (10:11 +0200)
committergarciadeblas <gerardo.garciadeblas@telefonica.com>
Fri, 26 Sep 2025 08:34:06 +0000 (10:34 +0200)
Change-Id: I2b31bf9a64b45fb5d158026c1f501d810997f512
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
tools/change-chart-version.sh

index 358cd74..acafe02 100755 (executable)
 #   limitations under the License.
 #
 
-if [ "$#" -ne 2 ]; then
-  echo "Usage: $0 <NEW_VERSION> <USER>"
-  echo "Example: $0 16.0.0 garciadeblas"
-  echo "Example: $0 15.0.7 vegall"
+if [ "$#" -lt 2 ] || [ "$#" -gt 3 ]; then
+  echo "Usage: $0 <NEW_VERSION> <GIT_USER> [<GIT_PASSWORD>]"
+  echo "    Example: $0 16.0.0 garciadeblas"
+  echo "    Example: $0 15.0.7 vegall"
+  echo "When <PASSWORD> is provided, it will be used for git https authentication. Otherwise, ssh authentication will be used."
   exit 1
 fi
 
 NEW_VERSION="$1"
-USER="$2"
-REPO_URL="ssh://$USER@osm.etsi.org:29418/osm/devops"
-# If the $NEW_VERSION == 15.0.1, the $BRANCH_NAME will be v15.0
+GIT_USER="$2"
+GIT_PASSWORD="${3:-}"
+
 BRANCH_NAME="v$(echo $NEW_VERSION | grep -oE '[0-9]+\.[0-9]+')"
 
-git clone $REPO_URL && (cd "devops" && curl https://osm.etsi.org/gerrit/tools/hooks/commit-msg > .git/hooks/commit-msg ; chmod +x .git/hooks/commit-msg)
-pushd devops
+BASE_FOLDER=$(mktemp -d change-chart-version.XXXXXX)
+pushd $BASE_FOLDER
 
-git checkout $BRANCH_NAME
+if [ -n "$GIT_PASSWORD" ]; then
+    REPO_URL="https://${GIT_USER}@osm.etsi.org/gerrit/a/osm/devops"
+    # Follow recommendation for user auth with git using a script git-creds.sh
+    cat << "EOF" > "${HOME}/git-creds.sh"
+#!/bin/sh
+if echo "$1" | grep -q '^Password'; then
+  echo "${GIT_PASSWORD}"
+else
+  echo "${GIT_USER}"
+fi
+exit 0
+EOF
+    chmod +x "${HOME}/git-creds.sh"
+else
+    REPO_URL="ssh://${GIT_USER}@osm.etsi.org:29418/osm/devops"
+fi
+
+echo "Cloning devops repo"
+if [ -n "$GIT_PASSWORD" ]; then
+    echo "Using https authentication"
+    GIT_USERNAME="${GIT_USER}" GIT_ASKPASS=~/git-creds.sh git clone "${REPO_URL}"
+else
+    echo "Using ssh authentication"
+    git clone "${REPO_URL}"
+fi
+cd "devops"
+curl https://osm.etsi.org/gerrit/tools/hooks/commit-msg > .git/hooks/commit-msg
+chmod +x .git/hooks/commit-msg
 
+git checkout $BRANCH_NAME
 sed -i -E "0,/^version: .*/s//version: \"$NEW_VERSION\"/" installers/helm/osm/Chart.yaml
 sed -i -E "0,/^appVersion: .*/s//appVersion: \"$NEW_VERSION\"/" installers/helm/osm/Chart.yaml
 
 git add installers/helm/osm/Chart.yaml
 git commit -s -m "Update chart version version to $NEW_VERSION"
-git push origin $BRANCH_NAME
+
+echo "Pushing changes to devops repo"
+if [ -n "$GIT_PASSWORD" ]; then
+    echo "Using https authentication"
+    GIT_USERNAME="${GIT_USER}" GIT_ASKPASS=~/git-creds.sh git push origin $BRANCH_NAME
+else
+    echo "Using ssh authentication"
+    git push origin $BRANCH_NAME
+fi
 
 commit=$(git show --summary | grep commit | awk '{print $2}')
 echo "The commit is $commit"
-popd
+cd ..
 rm -rf devops
 
+popd