Friday, May 1, 2020

Improve bash script with parameter

# parameter should be the component name such as api, dashboard or ansibleagent etc
api=("src/api-engine/"
     "build_image/docker/common/api-engine")
dashboard=("src/dashboard/"
     "build_image/docker/common/dashboard")
ansibleagent=("src/agent/ansible/"
     "build_image/docker/agent/ansible")


cdir=$(pwd)
cd ~/hl/src/github.com/realcello
allchanges=$(git diff --name-only HEAD~15 HEAD~20)
cd $cdir

echo "$allchanges"
hasTopChanges=$(echo "$allchanges" | grep -v '/')
if [ ! -z "$hasTopChanges" ]; then
  echo 'Has changes at the root, need to proceed'
  exit 0
fi

comp=$1
compItems="${comp}[@]"
changesFound='False'
echo 'Items in '${comp}
for item in "${!compItems}"; do
  echo "   checking $item"
  if echo "${allchanges}" | grep -q "$item"
  then
    changesFound='True'
  fi
done

if [ "$changesFound" == 'True' ]; then
  echo 'Need to proceed'
else
  echo "##vso[task.complete result=Succeeded;]No changes found!"
fi

No comments:

Post a Comment