Skip to content

Instantly share code, notes, and snippets.

@superjojo140
Last active February 8, 2024 12:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save superjojo140/97d06c4c46f8d50f251a83cc2397b68c to your computer and use it in GitHub Desktop.
Save superjojo140/97d06c4c46f8d50f251a83cc2397b68c to your computer and use it in GitHub Desktop.
Git-ftp cheatsheet (with auto tags)

Prequisites

See manual at: https://github.com/git-ftp/git-ftp/blob/master/man/git-ftp.1.md

Install git-ftp package: https://github.com/git-ftp/git-ftp/blob/master/INSTALL.md

Direct setup for linux:

curl https://raw.githubusercontent.com/git-ftp/git-ftp/master/git-ftp > /bin/git-ftp
chmod 755 /bin/git-ftp

Setup with scopes

Move to your local project root. Example setup for scope named productive

# FTP
git config git-ftp.productive.url ftp://ftpserver123.com:21/path/to/your/project
git config git-ftp.productive.user johndoe
git config git-ftp.productive.password s3cr3t

# SFTP with SSH key
git config git-ftp.productive.url sftp://ftpserver123.com:22/path/to/your/project
git config git-ftp.productive.user johndoe
git config git-ftp.productive.key ~/.ssh/id_rsa

Usage

git ftp init -s <scope-name>  # First usage
git ftp push -s <scope-name>  # Normal usage

Passphrase protected SSH-keys

git-ftp cannot ask you for a passphrase for your private key. You can store the passphrase in your ssh-agent as a workaround:

ssh-agent bash
ssh-add ~/.ssh/<YOUR_KEY_NAME>

Auto tags

git-ftp supports hooks, that means scripts that are automatically called before or after pushing code with git-ftp. I wrote a post-ftp-push hook to set git tags corresponding to the pushed scope. E.g. pushing a specific commit to testing scope will set a test-system tag on the pushed commit.

Create a text file called (it has no extension) .git/hooks/post-ftp-push

#!/bin/bash

remote="$1"
url="$2"
local_sha="$3"
remote_sha="$4"

case $remote in
    "test")
        tag="test-system"
        ;;
    "productive")
        tag="productive"
        ;;
    *)
        echo "[POST PUSH ($remote)] WARNING: Unknown git-ftp scope '$remote'. Using scope name as tag name"
        tag=$remote
        ;;
esac;

echo "-----------------"
echo "Updating files for the following commits:"
git log --pretty=format:%s $tag..HEAD

echo "-----------------"
echo "Handling deployment tags"
git tag -d "$tag"
git tag "$tag"
echo "[POST PUSH ($remote)] Removed tag '$tag' from old commits."
echo "[POST PUSH ($remote)] Added tag '$tag' to current commit $local_sha."

exit 0

Upload .version file with current commit id

Adding this pre-ftp-push hook to git's hooks folder allways writes the current commit id to a file called .version.

#!/bin/bash

local_sha="$3"
echo "Updating version hash in .version file"
echo -n "$local_sha" > .version

exit 0

Adding this line to the .git-ftp-include file, allways uploads the .version file to the remote server

.git-ftp-include

Hint: It might be usefull to add the .version file to your .gitignore file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment