Skip to content

Instantly share code, notes, and snippets.

@eghojansu
Last active November 13, 2017 13:27
Show Gist options
  • Save eghojansu/6fc12f3274868c6e84ca080cffc97787 to your computer and use it in GitHub Desktop.
Save eghojansu/6fc12f3274868c6e84ca080cffc97787 to your computer and use it in GitHub Desktop.
Deploy shell snippet (Created for symfony) --- customize if needed
#!/bin/bash
# Zipping Git Directory
# Usage:
# __THIS_FILE__ [ project_name ] [ version ]
#
# project_name -- default to directory name
# version -- default to latest version
#
# Timer from (http://www.linuxjournal.com/content/use-date-command-measure-elapsed-time)
#
# Elapsed time. Usage:
#
# t=$(timer)
# ... # do something
# printf 'Elapsed time: %s\n' $(timer $t)
# ===> Elapsed time: 0:01:12
#
#
#####################################################################
# If called with no arguments a new timer is returned.
# If called with arguments the first is used as a timer
# value and the elapsed time is returned in the form HH:MM:SS.
#
function timer()
{
if [[ $# -eq 0 ]]; then
echo $(date '+%s')
else
local stime=$1
etime=$(date '+%s')
if [[ -z "$stime" ]]; then stime=$etime; fi
dt=$((etime - stime))
ds=$((dt % 60))
dm=$(((dt / 60) % 60))
dh=$((dt / 3600))
printf '%d:%02d:%02d' $dh $dm $ds
fi
}
# current dir name
CD=${PWD##*/}
CF=${0##*/}
TMPROOT="$PWD/tmp"
TMP="$TMPROOT/$CD"
# default version
defaultVersion='0.1.0'
# Destination path
destinationPath='/home/fal/Temp'
# clear console
clear
# Get project name, default to current directory name
if [ -z "$1" ] ; then
projectName=$CD
else
projectName=$1
fi
echo "Current directory is $PWD"
echo " zipping this directory as '$projectName'..."
# using version
if [ -z "$2" ] ; then
# finding latest version
version=$(git tag --list | tail -1)
if [ -z "$version" ] ; then
# using default version
needToCheckout=false
version=$defaultVersion
else
# using latest version
needToCheckout=true
checkTag=$(echo $version | grep -oP '.*?(?=\-)')
if [ $(git tag --list "$checkTag") ]; then
# found stable version (no alpha/beta suffix)
version=$checkTag
fi
fi
else
# check given version
if [ ! $(git tag --list "$2") ]; then
# given version invalid
echo "Error: Given version was not exists"
exit
fi
# using given version
version=$2
needToCheckout=true
fi
# Zip location
destination="$destinationPath/$projectName-$version.zip"
echo " zipping to '$destination'"
# Check file
if [ -f $destination ] ; then
rm -f $destination
echo ' (zip file was exists and has been deleted)'
fi
# current branch
currentBranch=$(git branch --list | grep -e '^*' | cut -d' ' -f 2)
echo " current branch is $currentBranch"
if $needToCheckout; then
# Checking out version
echo " checking out $version"
git checkout -q $version
else
echo ' using current branch'
fi
# build it
t=$(timer)
printf ' building...'
# copy to tmp
if [ ! -d $TMPROOT ] ; then
# tmproot not exists
mkdir $TMPROOT
fi
if [ -d $TMP ] ; then
# thereis tmp folder
rm -r $TMP
fi
rsync -a \
--exclude='.idea' \
--exclude='.git' \
--exclude='tmp' \
--exclude='vendor' \
--exclude='tests' \
--exclude='node_modules' \
--exclude='bower_components' \
--exclude='var/cache/*' \
--exclude='var/logs/*' \
--exclude='var/sessions/*' \
--exclude='gulpfile.js' \
--exclude='package.json' \
--exclude='phpunit.*' \
--exclude='*.sublime-workspace' \
--exclude='web/app_dev.php' \
--exclude=$CF \
--include=".gitignore" \
--include=".gitkeep" \
$PWD $TMPROOT
# install vendor
composer install \
--quiet \
--no-interaction \
--no-dev \
--no-scripts \
--no-progress \
--no-suggest \
--optimize-autoloader \
--working-dir=$TMP
# cd to tmp
pushd $TMPROOT > /dev/null
# zip it
# Rules (please modify if rules below was not satisfy your needs)
zip \
-wsqr9 \
$destination $CD
printf 'done (elapsed time: %s)\n' $(timer $t)
# cd to previous dir
popd > /dev/null
if $needToCheckout; then
# Revert previous checkout
echo " checking out $currentBranch"
git checkout -q $currentBranch
fi
echo 'job done'
#!/bin/bash
################################################################################
# Usage : #
# build.sh \ #
# --version=GIT_TAG \ #
# --from=GIT_TAG_A #
# --to=GIT_TAG_B #
# #
# Requirement: git, composer #
################################################################################
DANGER='\e[0;31m'
INFO='\e[0;32m'
WARNING='\e[1;33m'
DEFAULT='\e[0m'
echo -e "${INFO}Build command start...${DEFAULT}"
gitCheck=$(git tag)
gitVersion=$(git --version)
composerVersion=$(composer --version)
currentDirname=${PWD##*/}
currentFilename=${0##*/}
projectName=$currentDirname
tempRootBase="/tmp/build_$currentDirname"
tempRoot="$tempRootBase/$projectName"
defaultVersion="0.1.0"
from_version=""
to_version=""
destinationPath="/home/fal/Temp"
buildMode="install"
needToCheckout=true
currentBranch=$(git branch --list | grep -e '^*' | cut -d' ' -f 2)
if [ -z $currentBranch ]
then
currentBranch="NOT-IN-ANY-BRANCH"
needToCheckout=false
fi
for i in "$@"
do
case $i in
--version=*)
version="${i#*=}"
shift # past argument=value
if [ ! $(git tag --list "$version") ]
then
# given version invalid
echo -e "Error: Given version ${WARNING}$version${DEFAULT} was not exists"
exit 0
fi
;;
--from=*)
from_version="${i#*=}"
shift # past argument=value
buildMode="patch"
;;
--to=*)
to_version="${i#*=}"
shift # past argument=value
buildMode="patch"
;;
esac
done
build_install() {
if [ -z $version ]
then
version=$(git tag --list | tail -1)
if [ -z "$version" ]
then
# using default version
needToCheckout=false
version=$defaultVersion
else
# using latest version
local checkTag=$(echo $version | grep -oP '.*?(?=\-)')
if [ $(git tag --list "$checkTag") ]
then
# found stable version (no alpha/beta suffix)
version=$checkTag
fi
fi
fi
echo "Building fresh install package..."
echo " Using $gitVersion"
echo " Using $composerVersion"
echo " Working dir $PWD"
echo -e " Zipping as ${WARNING}$projectName${DEFAULT} version ${WARNING}$version${DEFAULT}"
echo -e " Current branch ${WARNING}$currentBranch${DEFAULT}"
# Zip location
destination="$destinationPath/$projectName-$version.zip"
echo -e " Zipping to ${WARNING}$destination${DEFAULT}"
# Check file
if [ -f $destination ] ; then
rm -f $destination
echo ' (zip file was exists and has been deleted)'
fi
if $needToCheckout
then
# Checking out version
echo -e " checking out ${WARNING}$version${DEFAULT}"
git checkout -q $version
else
echo -e " ${INFO}using current branch${DEFAULT}"
fi
if [ ! -d $tempRootBase ] ; then
# tmproot not exists
mkdir --parents $tempRootBase
fi
if [ -d $tempRoot ] ; then
# thereis tmp folder
rm -r $tempRoot
fi
# build asset
echo -e " ${INFO}building assets...${DEFAULT}"
gulp build --silent
echo -e " ${INFO}done${DEFAULT}"
rsync -aq \
--exclude='.idea' \
--exclude='.git' \
--exclude='tmp' \
--exclude='var/cache/*' \
--exclude='var/logs/*' \
--exclude='var/sessions/*' \
--exclude='vendor' \
--exclude='node_modules' \
--exclude='bower_components' \
--exclude='features' \
--exclude='behat.yml' \
--exclude='tests' \
--exclude='web/bundles' \
--exclude='web/app_dev.php' \
--exclude='design' \
--exclude='app/config/parameters.yml' \
--exclude='TODO' \
--exclude='bin' \
--exclude='src/AppBundle/Command' \
--exclude='gulpfile.js' \
--exclude='gulpfile-config.js' \
--exclude='app/Resources/js' \
--exclude='app/Resources/scss' \
--exclude='app/Resources/resources' \
--exclude='package.json' \
--exclude='package-lock.json' \
--exclude='phpunit.*' \
--exclude='*.sublime-workspace' \
--exclude=$currentFilename \
--include=".gitignore" \
--include=".gitkeep" \
--include=".htaccess" \
$PWD $tempRootBase
# add current version
if [ -f "$tempRoot/VERSION" ]
then
rm "$tempRoot/VERSION"
fi
echo $version > "$tempRoot/VERSION"
# install dependency
echo -e " ${INFO}installing dependency...${DEFAULT}"
composer install \
--quiet \
--no-interaction \
--no-dev \
--no-scripts \
--no-progress \
--no-suggest \
--optimize-autoloader \
--working-dir=$tempRoot
echo -e " ${INFO}done${DEFAULT}"
# cd to tmp
pushd $tempRootBase > /dev/null
# zip vendor
zip -wsqr9 $destination $projectName
# cd to previous dir
popd > /dev/null
if $needToCheckout
then
# Revert previous checkout
echo -e " checking out ${WARNING}$currentBranch${DEFAULT}"
git checkout -q $currentBranch
fi
}
build_patch() {
echo "Building patch..."
echo " Using $gitVersion"
echo " Using $composerVersion"
echo " Working dir $PWD"
echo -e " Zipping as ${WARNING}$projectName${DEFAULT} patch ${WARNING}$from_version${DEFAULT} to ${WARNING}$to_version${DEFAULT}"
echo -e " Current branch ${WARNING}$currentBranch${DEFAULT}"
if [ -z $from_version ] || [ -z $to_version ]
then
echo -e " ${DANGER}ERROR!${DEFAULT} You need to supply from and to version"
exit 0
fi
# Zip location
destination="$destinationPath/$projectName-patch-$from_version-to-$to_version.zip"
echo -e " Zipping to ${WARNING}$destination${DEFAULT}"
# Check file
if [ -f $destination ] ; then
rm -f $destination
echo ' (zip file was exists and has been deleted)'
fi
# Checking out version
echo -e " checking out ${WARNING}$from_version${DEFAULT}"
git checkout -q $to_version
if [ -d $tempRoot ] ; then
rm -r $tempRoot
fi
mkdir --parents $tempRoot
# copy changes
cp --parents `git diff --name-only "$from_version..$to_version"` $tempRoot
# check assets changes
buildAssets=false
if [ $(ls "$tempRoot/app/Resources/js/*" 2> /dev/null | wc -l) -gt 0 ] || \
[ $(ls "$tempRoot/app/Resources/css/*" 2> /dev/null | wc -l) -gt 0 ] || \
[ $(ls "$tempRoot/app/Resources/resources/*" 2> /dev/null | wc -l) -gt 0 ]
then
buildAssets=true
fi
# check install vendor
installVendor=false
if [ $(ls "$tempRoot/composer.json" 2> /dev/null | wc -l) -gt 0 ] || \
[ $(ls "$tempRoot/composer.lock" 2> /dev/null | wc -l) -gt 0 ]
then
installVendor=true
fi
# remove files
rm -fR \
"$tempRoot/features" \
"$tempRoot/behat.yml" \
"$tempRoot/test" \
"$tempRoot/web/bundles" \
"$tempRoot/web/app_dev.php" \
"$tempRoot/design" \
"$tempRoot/app/config/parameters.yml" \
"$tempRoot/TODO" \
"$tempRoot/bin" \
"$tempRoot/src/AppBundle/Command" \
"$tempRoot/gulpfile.js" \
"$tempRoot/gulpfile-config.js" \
"$tempRoot/app/Resources/js" \
"$tempRoot/app/Resources/scss" \
"$tempRoot/app/Resources/resources" \
"$tempRoot/package.json" \
"$tempRoot/package-lock.json" \
"$tempRoot/phpunit.*" \
"$tempRoot/*.sublime-workspace" \
"$tempRoot/$currentFilename"
# build asset
if $buildAssets
then
echo -e " ${INFO}building assets...${DEFAULT}"
gulp build --silent
echo -e " ${INFO}done${DEFAULT}"
fi
# install dependency
if $installVendor
then
echo -e " ${INFO}installing dependency...${DEFAULT}"
composer install \
--quiet \
--no-interaction \
--no-dev \
--no-scripts \
--no-progress \
--no-suggest \
--optimize-autoloader \
--working-dir=$tempRoot
echo -e " ${INFO}done${DEFAULT}"
fi
# cd to tmp
pushd $tempRootBase > /dev/null
# zip vendor
zip -wsqr9 $destination $projectName
# cd to previous dir
popd > /dev/null
# Revert previous checkout
echo -e " checking out ${WARNING}$currentBranch${DEFAULT}"
git checkout -q $currentBranch
}
if [ $buildMode = "patch" ]
then
build_patch
else
build_install
fi
ellapsed="$SECONDS second"
if [ $SECONDS -gt 1 ]
then
ellapsed="${ellapsed}s"
elif [ $SECONDS -gt 60 ]
then
let minute=$SECONDS/60
ellapsed="$minute minute"
if [ $minute -gt 1 ]
then
ellapsed="${ellapsed}s"
fi
fi
echo -e "${INFO}Done (Ellapsed time: $ellapsed)${DEFAULT}"
#!/bin/bash
# Zipping Git Directory
# Usage:
# __THIS_FILE__ [ project_name ] [ version ]
#
# project_name -- default to directory name
# version -- default to latest version
#
# Timer from (http://www.linuxjournal.com/content/use-date-command-measure-elapsed-time)
#
# Elapsed time. Usage:
#
# t=$(timer)
# ... # do something
# printf 'Elapsed time: %s\n' $(timer $t)
# ===> Elapsed time: 0:01:12
#
#
#####################################################################
# If called with no arguments a new timer is returned.
# If called with arguments the first is used as a timer
# value and the elapsed time is returned in the form HH:MM:SS.
#
function timer()
{
if [[ $# -eq 0 ]]; then
echo $(date '+%s')
else
local stime=$1
etime=$(date '+%s')
if [[ -z "$stime" ]]; then stime=$etime; fi
dt=$((etime - stime))
ds=$((dt % 60))
dm=$(((dt / 60) % 60))
dh=$((dt / 3600))
printf '%d:%02d:%02d' $dh $dm $ds
fi
}
# current dir name
CD=${PWD##*/}
CF=${0##*/}
# default version
defaultVersion='0.1.0'
# Destination path
destinationPath='/home/fal/Temp'
# clear console
clear
# Get project name, default to current directory name
if [ -z "$1" ] ; then
projectName=$CD
else
projectName=$1
fi
echo "Current directory is $PWD"
echo " zipping this directory as '$projectName'..."
# using version
if [ -z "$2" ] ; then
# finding latest version
version=$(git tag --list | tail -1)
if [ -z "$version" ] ; then
# using default version
needToCheckout=false
version=$defaultVersion
else
# using latest version
needToCheckout=true
checkTag=$(echo $version | grep -oP '.*?(?=\-)')
if [ $(git tag --list "$checkTag") ]; then
# found stable version (no alpha/beta suffix)
version=$checkTag
fi
fi
else
# check given version
if [ ! $(git tag --list "$2") ]; then
# given version invalid
echo "Error: Given version was not exists"
exit
fi
# using given version
version=$2
needToCheckout=true
fi
# Zip location
destination=$destinationPath'/'$projectName'-'$version'.zip'
echo " zipping to '$destination'"
# Check file
if [ -f $destination ] ; then
rm -f $destination
echo ' (zip file was exists and has been deleted)'
fi
# current branch
currentBranch=$(git branch --list | grep -e '^*' | cut -d' ' -f 2)
echo " current branch is $currentBranch"
if $needToCheckout; then
# Checking out version
echo " checking out $version"
git checkout -q $version
else
echo ' using current branch'
fi
# cd to parent
pushd .. > /dev/null
# zip it
t=$(timer)
printf ' zipping...'
# Rules (please modify if rules below was not satisfy your needs)
zip \
-wsqr9 \
-b /tmp \
-x \
"*/.idea/**" \
"*/.git/**" \
"*/tests/**" \
"*/node_modules/**" \
"*/bower_components/**" \
"*/tests/**" \
"*/var/cache/**" \
"*/var/logs/**" \
"*/var/sessions/**" \
"*/gulpfile.js" \
"*/*.sublime-*" \
"*/*.lock" \
"*/*.dist" \
"*/package.json" \
"*/bower.json" \
"*/ftpsync.setting" \
"*/$CF" \
@ \
$destination $CD'/'
printf 'done (elapsed time: %s)\n' $(timer $t)
# cd to starting dir
popd > /dev/null
if $needToCheckout; then
# Revert previous checkout
echo " checking out $currentBranch"
git checkout -q $currentBranch
fi
echo 'job done'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment