User Tools

Site Tools


jenkins

Jenkins deployment Scripts

###############
## Config BEGIN
############### 
 
##
# Server name or IP address where the code should be deployed
# 
# Use public-key authentication!
##
SERVER='knallimall.org'
 
##
# 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-live.knallimall.org/web'
 
##
# 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"
)
 
##
# Set true/false to show/hide a list of modified files
##
SHOW_MODIFIED=true
 
##
# Git hash file
##
HASH_FILE='t1_git_deployment_id'
 
#####################################################
## Config END - NO NEED TO CHANGE ANYTHING BELOW 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`
 
 
#######################
# Prepare file listings
#######################
 
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
 
##############################################################
# Housekeeping - delete/move files declared in RM_LIST/MV_LIST
##############################################################
 
echo -n "Removing unnecessary files from local git clone ... "
for index in ${!RM_LIST[*]} ; do
    rm -fR ${RM_LIST[$index]}
done
 
if [[ -z "${MV_LIST-unset}" ]]
	echo -n "Renaming files in local git clone ... "
	for index in ${!MV_LIST[*]} ; do
	   mv ${MV_LIST[$index]}
	done
fi
 
echo -n "Removing git specific files from local git clone ... "
find . -name ".git*" -exec rm -fR {} \+
 
#################################
# Check remote just before upload
#################################
 
echo -n "Check if deployment directory exists on server ... "
if ssh ${USER}@${SERVER} "test -d ${REMOTE_DESTINATION}"; then
    echo -n "Check if deployment directory is writeable on server ... "
    if ssh ${USER}@${SERVER} "test -w ${REMOTE_DESTINATION}"; then
        echo "done"
    else
        echo "failed"
        echo "Deployment directory ${REMOTE_DESTINATION} on server ${SERVER} is not writeable - exiting!"
        rm -fR ${WORKSPACE}
        exit 1
    fi
else
    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 -v ${WORKSPACE}/ ${USER}@${SERVER}:${REMOTE_DESTINATION}/
echo "done"
 
 
###################################################
# Comparing hashes
# [if] 		Initial Upload
# [else]	Delete files on remote according to git 
###################################################
 
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
 
#######################################
# Some housekeeping on local and remote
#######################################
 
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 'Danke DZI - und tschüss!'
jenkins.txt · Last modified: 2015/01/03 20:15 by admin