When you work with a cheap host, you just have FTP account to push your file to host. It will be terrible if you have to pull all changed file to host via FTP. Because you must answer many stupid question as “what files I was change?”, “Where files I was change?”, “What is ftp account?” …
So I think that it is better if you write a script to handle all task and help you answer all question. The way is posted the below will run well in linux environment.
Firstly, we need a script to upload file via FTP. The script below requires CURL. You just need to change value of REMOTE_HOST, REMOTE_USER, REMOTE_PASSWD. It will work well.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
#!/bin/bash # ------------------------------------------------------------ # Defaults # ------------------------------------------------------------ REMOTE_PROTOCOL="ftp" REMOTE_HOST="YOUR_HOST" REMOTE_USER="USERNAME" REMOTE_PASSWD="PASSWORD" REMOTE_PATH="" REMOTE_CMD_OPTIONS="-s" SYNCROOT="" CURL_DISABLE_EPSV=0 declare -a CURL_ARGS declare -i ACTIVE_MODE=0 # ------------------------------------------------------------ # Constant Exit Error Codes # ------------------------------------------------------------ readonly ERROR_UPLOAD=4 SRC_FILE="$1" DEST_FILE="$2" set_default_curl_options() { OIFS="$IFS" IFS=" " CURL_ARGS=(${REMOTE_CMD_OPTIONS[@]}) IFS="$OIFS" CURL_ARGS+=(--globoff) if [ ! -z $REMOTE_USER ]; then CURL_ARGS+=(--user "$REMOTE_USER":"$REMOTE_PASSWD") else CURL_ARGS+=(--netrc) fi CURL_ARGS+=(-#) if [ $ACTIVE_MODE -eq 1 ]; then CURL_ARGS+=(-P "-") else if [ $CURL_DISABLE_EPSV -eq 1 ]; then CURL_ARGS+=(--disable-epsv) fi fi } check_exit_status() { if [ $? -ne 0 ]; then print_error_and_die "$1, exiting..." $2 fi } print_error_and_die() { echo "fatal: $1" } if [ -z $DEST_FILE ]; then DEST_FILE=$SRC_FILE fi if [ -n "$SYNCROOT" ]; then DEST_FILE=${DEST_FILE/$SYNCROOT/$REPLACE} fi set_default_curl_options CURL_ARGS+=(-T "$SRC_FILE") CURL_ARGS+=(--ftp-create-dirs) CURL_ARGS+=("$REMOTE_PROTOCOL://$REMOTE_HOST/${REMOTE_PATH}${DEST_FILE}") curl "${CURL_ARGS[@]}" check_exit_status "Could not upload file: '${REMOTE_PATH}$DEST_FILE'." $ERROR_UPLOAD |
After that, you write a script to list all file that needs to push to your FTP host. That script is similar to the script below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
#!bin/bash echo 'STAGING Starting...' expected_args=2 e_badargs=65 home_dir=$PWD/../ current_time=$(date "+%Y.%m.%d-%H.%M.%S") if [ $# -ne $expected_args ] then echo "Error: you should use command $0 old_tag new_tag" exit $e_badargs fi cd $home_dir old_tag="$1" if [ $old_tag = "" ] then echo "Error no tag" exit fi new_tag="$2" if [ $new_tag = "" ] then echo "Error no tag" exit fi old_commit=`git rev-list $old_tag|head -n 1` count_file=0 for file_change in `git diff --name-only $old_commit HEAD@{0}` do echo "Put $file_change ..." count_file=$((count_file+1)) bash $home_dir/deploy/ftp.sh $file_change /public_html/staging/$file_change done if [ $count_file -gt 0 ] then git tag -a $new_tag -m "Version $new_tag" git push origin $new_tag fi echo "DONE!!!" |
With the script above, you need to provide 2 tag name, and script will check all file changes between them. Finally, It calls fpt.sh script to push that files to FTP host.
You can see full source code in my Github: https://github.com/thanhson1085/git2ftp