In my company I follow the following workflow ( I think it can be defined a kind of 'Continuous Integration' )
Workflow:
staging
/ master
/ production
)staging
or production
automatically deploy to staging/production server. We use git in the following way:
(suppose I'm working on the new functionality 'A')
I find quite boring/time consuming to git checkout ...
git pull ..
bla bla every time I have to deploy live/staging my work.
So I have produced the following bash script, the aim is to simply the process of deploying branch A into a single command deploy.sh live A
#!/bin/bash
function merge() {
SOURCE=$1
DEST=$2
echo ''
echo "--- Merging $SOURCE with $DEST ---"
echo "--> Checkout $DEST ..."
git checkout $DEST
echo "--> Pull $DEST ..."
git pull --ff-only origin $DEST
if [ $? -ne 0 ]
then
echo "ERROR: Could not PULL"
return 1
fi
echo "--> Merging $SOURCE with $DEST ..."
# --ff-only trigger errors if merge/pull is not possible
git merge --ff-only $SOURCE --no-edit
if [ $? -ne 0 ]
then
echo "ERROR: Could not MERGE"
return 1
fi
echo "--> Push $DEST ..."
git push origin $DEST
return 0
}
function deploy() {
MODE=$1
SOURCE_BRANCH=$2
echo ''
echo "### START ###"
echo ''
echo "--- Pull changes from Master ---"
git checkout $SOURCE_BRANCH
git pull --progress --no-edit --no-stat -v --progress origin master
merge $SOURCE_BRANCH 'staging'
status=$?
if [ $status -ne 0 ]
then
echo "ERROR: STEP 1" >&2
exit 1
fi
if [ $MODE = "live" ]
then
merge $SOURCE_BRANCH 'master'
status=$?
if [ $status -ne 0 ]
then
echo "ERROR: STEP 2"
return 1
fi
merge 'master' 'production'
status=$?
if [ $status -ne 0 ]
then
echo "ERROR: STEP 3"
return 1
fi
fi
echo ''
echo "### END ###"
echo ''
}
MODE=$1;
SOURCE_BRANCH=$2;
if [ -z "$MODE" -o -z "$SOURCE_BRANCH" ]
then
echo "Usage:"
echo ""
echo "MODE BRANCH_NAME (MODE: live|staging)"
else
if git show-ref --verify --quiet "refs/heads/$SOURCE_BRANCH";
then
deploy $MODE $SOURCE_BRANCH
else
echo ''
echo "Error: Branch $SOURCE_BRANCH not found"
fi
fi
The question:
I'm quite newby both on GIT and BASH scripting. So I would like to know if the above workflow/script is ok ? Any recommendation is welcome
( in the same time I'm happy to share this as resource )
if the script does what you need then it's good, and your bash skills will get better.
IMHO, the whole thing could be written in 10 lines and would be better but it's a question of taste.
Apart for the ifs, you got advice in the previous comments, there's no need for three lines to echo status, echo -e "\\n..." or printf, that's around 10% less lines.
checking and failing with an error can be done with ${v:?message}
the functions are not needed and neither peppering your code with return.
It's a step to go from pytonish/whateverish coding style to something more compact in bash. Not everybody likes it nor finds it good so you'll have to try for yourself.
as an example I rewrote a 250 lines of (overly clear) python in 5 lines of bash; the windows/python colleague thought it wasn't clear enough (but then he can't write bash) another 5 extra comment lines made it better for him; another colleague found it much better (not scrolling for ages was one reason).
You have to try for yourself and don't dismiss one style or the other before taking the time to appreciate each one.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.