This is an old revision of the document!
#### ### production.config.sh #### ####################### project depending config values ####################### ## # Server name or IP address where the code should be deployed # # Use public-key authentication! ## SERVER='188.40.71.216' ## # User name for server # # Use public-key authentication! ## USER='root' ## # Remote directory where the code should be deployed # # Use public-key authentication! ## REMOTE_DESTINATION='/var/www/magento.tagwork-one.de' ## # List of files and directories with relative paths to the git repository root which should # not be deployed (e. g. unnecessary configuration files) ## declare -a RM_LIST=( 'dev1.config.inc.php' 'test.config.inc.php' 't1_git_deployment' 'docs' ) ## # List of files and directories with relative paths to the git repository root which should # be renamed (e. g. necessary configuration files) ## declare -a MV_LIST=( 'production.config.inc.php config.inc.php' ) ## # List shell commands which should be executed afterwards ## declare -a COMMAND_LIST=( "find ${REMOTE_DESTINATION}/. -type f -print0 | xargs -0 chmod 664 {} 2>/dev/null" "find ${REMOTE_DESTINATION}/. -type d -print0 | xargs -0 chmod 775 {} 2>/dev/null" "find ${REMOTE_DESTINATION}/. -iname \*.sh -print0 | xargs -0 chmod +x {} 2>/dev/null" ) ############################ general config values ############################ ## # Set true/false to show/hide a list of modified files ## SHOW_MODIFIED=true ## # Git hash file ## HASH_FILE='t1_git_deployment_id' ## # Remote lock file ## LOCK_FILE='t1_git_deployment.lock' ### # THE MAGIC HAPPENS HERE ... ### echo -n "Store head commit hash to file in local git clone ... " CURRENT_HASH=`git log --format="%H" | head -n1` echo ${CURRENT_HASH} > ${HASH_FILE} echo -n "Getting remote commit hash ... " REMOTE_HASH=`scp -q ${USER}@${SERVER}:${REMOTE_DESTINATION}/${HASH_FILE} /dev/stdout 2> /dev/null` if test -z ${REMOTE_HASH}; then echo -n "Remote hash is empty, getting initial commit hash ... " INITIAL_HASH=`git log --format="%H" | tail -n1` if [ "${SHOW_MODIFIED}" == true ]; then echo -n "Getting list of files for initial commit ... " while read -r file; do MODIFIED_FILES+=("$file") done < <(git show --pretty="format:" --name-status ${INITIAL_HASH}) fi else if [ ${CURRENT_HASH} == ${REMOTE_HASH} ]; then echo "Remote hash and current local hash are identical (${REMOTE_HASH}) - nothing to do!" rm -fR ${WORKSPACE} echo 'Finished' exit 0 fi echo -n "Getting files to delete from server ... " while IFS= read -r -d $'\0' file; do DELETE_FILES+=("$file") done if [ "${SHOW_MODIFIED}" == true ]; then echo -n "Getting list of modified files ... " while IFS= read -r -d $'\0' file; do MODIFIED_FILES+=("$file") done fi fi echo -n "Removing unnecessary files from local git clone ... " for index in ${!RM_LIST[*]} ; do rm -fR ${RM_LIST[$index]} done #echo -n "Renaming files in local git clone ... " #for index in ${!MV_LIST[*]} ; do # mv ${MV_LIST[$index]} #done echo -n "Removing git specific files from local git clone ... " find . -name ".git*" -exec rm -fR {} \+ echo "done" echo -n "Check if deployment directory exists on server ... " if ssh ${USER}@${SERVER} "test -d ${REMOTE_DESTINATION}"; then echo "done" echo -n "Check if deployment directory is writeable on server ... " if ssh ${USER}@${SERVER} "test -w ${REMOTE_DESTINATION}"; then echo "done" echo -n "Check if deployment is currently active on server ... " if ssh ${USER}@${SERVER} "test -f ${REMOTE_DESTINATION}/${LOCK_FILE}"; then echo "failed" echo "Deployment in ${REMOTE_DESTINATION} on server ${SERVER} possibly in process - exiting!" rm -fR ${WORKSPACE} exit 1 else echo "done" ssh ${USER}@${SERVER} "touch ${REMOTE_DESTINATION}/${LOCK_FILE}" fi else echo "failed" echo "Deployment directory ${REMOTE_DESTINATION} on server ${SERVER} is not writeable - exiting!" rm -fR ${WORKSPACE} exit 1 fi else echo "done" echo -n "Creating deployment directory on server ... " ssh ${USER}@${SERVER} "mkdir -p ${REMOTE_DESTINATION}" if [ $? -ne 0 ]; then echo "failed" echo "Error creating deployment directory ${REMOTE_DESTINATION} on server ${SERVER} - exiting!" rm -fR ${WORKSPACE} exit 1 else echo "done" fi fi echo -n "Copy files to server ... " rsync --checksum --links --compress --recursive ${WORKSPACE}/ ${USER}@${SERVER}:${REMOTE_DESTINATION}/ echo "done" if test -z "${REMOTE_HASH}"; then if [ "${SHOW_MODIFIED}" == true ]; then echo "List all files for initial upload:" for FILE in "${MODIFIED_FILES[@]}"; do echo "${FILE}" done fi else echo -n "Removing files from server ... " for FILE in "${DELETE_FILES[@]}"; do ssh ${USER}@${SERVER} "rm -f \"${REMOTE_DESTINATION}/${FILE}\"" done echo "done" if [ "${SHOW_MODIFIED}" == true ]; then echo "List modified files between ${REMOTE_HASH} and ${CURRENT_HASH}:" STATUS='' for FILE in "${MODIFIED_FILES[@]}"; do if test -z ${STATUS}; then STATUS="$FILE" else echo "${STATUS} ${FILE}" STATUS='' fi done fi fi echo -n "Running commands on server ... " for COMMAND in "${COMMAND_LIST[@]}"; do ssh ${USER}@${SERVER} ${COMMAND} done echo -n "Remove local temporary directory ... " rm -fR ${WORKSPACE} echo -n "Remove lock file on server ... " ssh ${USER}@${SERVER} "rm -f ${REMOTE_DESTINATION}/${LOCK_FILE}" echo 'Danke DZI - und tschüss!'