deploy-tools/src/repo_to_cs.sh
2025-02-10 01:48:22 +05:30

79 lines
2.2 KiB
Bash
Executable File

#!/bin/bash
### Functions
usage()
{
echo "usage: $0 [-m docker_image -t tag -r repolist] | [-h]"
}
main()
{
local IMAGE=$1
local TAG=$2
local REPOLIST=$3
KUST_FILES_LINES=$(grep -e "$IMAGE\s" $REPOLIST)
if [ -z "$KUST_FILES_LINES" ]
then
echo "Nothing to update";
exit;
fi
while IFS= read -r KUST_FILES_LINE; do
KUST_PATH=$(echo "$KUST_FILES_LINE" | tr -s " " | cut -d " " -f 2)
#Need to add $ after ${IMAGE} so partial names don't get matched. (Eg: z5-edge shoudln't match z5-edge-socket)
#In the kustomization file, the name "repo2.hub.gmetri.io/repo-name" might end with either a space OR and endline character. Handline both cases.
LINE_N1=$(grep -n -e "${IMAGE}$" -e "${IMAGE}\s" ${KUST_PATH} | cut -d ":" -f 1)
LINE_N2=$(expr "$LINE_N1" + "1")
#Replace repo verion in kustomization.yaml. -n is true only if the following argument is non empty
if [ -n "$LINE_N2" ]
then
echo "Replacement Op: sed -i -e \"${LINE_N2}s/newTag: .*/newTag: ${TAG}/\" \"$KUST_PATH\""
sed -i -e "${LINE_N2}s/newTag: .*/newTag: ${TAG}/" "$KUST_PATH"
git add $KUST_PATH;
fi
done <<< "$KUST_FILES_LINES"
VER=$(cat version)
NEW_VER=$(./drone/increment_semver.sh -p $VER)
echo "$NEW_VER" > version
CS_REPO_NAME=`node -p require\(\'./package.json\'\).name`
git add version
git commit -m "$NEW_VER: $IMAGE updated to $TAG"
git tag -a $NEW_VER -m "$NEW_VER: $IMAGE updated to $TAG"
}
### Starts here
while [ "$1" != "" ]; do
case $1 in
-m | --image ) shift
IMAGE=$1
;;
-t | --tag ) shift
TAG=$1
;;
-r | --repolist ) shift
REPOLIST=$1
;;
-h | --help ) usage
exit
;;
* ) usage
exit 1
esac
shift
done
if [[ -z $IMAGE || -z $TAG || -z $REPOLIST ]]
then
echo "Not enough arguments"
usage
exit
fi
main "$IMAGE" "$TAG" "$REPOLIST"