【Mac編】Git-ftpの使い方

Git-ftpを使うと、コミット後にgit ftp pushコマンドを実行するだけでgitで管理しているファイルの差分をアップロードしてくれます。これを使えばレンタルサーバーなどFTPしか使えない場合にFTPクライアントソフトを使わずに済みます。

前提条件

  • M1 Mac mini
  • SFTPで接続する
  • 既にGitで管理していてコミット済みである

インストール

今回はMacにインストールします。
他OSにインストールする場合は公式ドキュメント(git-ftp/INSTALL.md at master · git-ftp/git-ftp)を参照ください。

XcodeとCommand line toolのインストール

※既にインストール済みの方は飛ばしてください。


xcode-select --install

インストール済みの場合、上記コマンドを実行するとxcode-select: error: command line tools are already installed, use "Software Update" to install updatesというメッセージが表示されます。

Git、Git-ftp、Brotliのインストール

Homebrewを使用してインストールします。


brew install git
brew install brotli
brew install git-ftp

バージョン確認


git --version
git version 2.31.1

brotli -V
brotli 1.0.9

git-ftp --version
git-ftp version 1.6.0

SFTPに対応する

macOSに搭載されているcurlのデフォルトバージョンはSFTPに対応していないため、SFTPをサポートしたcurlをインストールします。


brew install openssl
brew install libssh2

brew list openssl
/opt/homebrew/Cellar/openssl@1.1/1.1.1k/.bottle/etc/ (7 files)
/opt/homebrew/Cellar/openssl@1.1/1.1.1k/bin/c_rehash
/opt/homebrew/Cellar/openssl@1.1/1.1.1k/bin/openssl
/opt/homebrew/Cellar/openssl@1.1/1.1.1k/include/openssl/ (104 files)
/opt/homebrew/Cellar/openssl@1.1/1.1.1k/lib/libcrypto.1.1.dylib
/opt/homebrew/Cellar/openssl@1.1/1.1.1k/lib/libssl.1.1.dylib
/opt/homebrew/Cellar/openssl@1.1/1.1.1k/lib/engines-1.1/ (2 files)
/opt/homebrew/Cellar/openssl@1.1/1.1.1k/lib/pkgconfig/ (3 files)
/opt/homebrew/Cellar/openssl@1.1/1.1.1k/lib/ (4 other files)
/opt/homebrew/Cellar/openssl@1.1/1.1.1k/share/doc/ (3970 files)
/opt/homebrew/Cellar/openssl@1.1/1.1.1k/share/man/ (3970 files)

echo 'export PATH="/opt/homebrew/opt/openssl@1.1/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

openssl version                                                    
OpenSSL 1.1.1k  25 Mar 2021

brew install curl-openssl
echo 'export PATH="/opt/homebrew/opt/curl/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

curl --version
curl 7.77.0 (arm-apple-darwin20.5.0) libcurl/7.77.0 (SecureTransport) OpenSSL/1.1.1k zlib/1.2.11 brotli/1.0.9 zstd/1.5.0 libidn2/2.3.1 libssh2/1.9.0 nghttp2/1.44.0 librtmp/2.3 OpenLDAP/2.5.5
Release-Date: 2021-05-26
Protocols: dict file ftp ftps gopher gophers http https imap imaps ldap ldaps mqtt pop3 pop3s rtmp rtsp scp sftp smb smbs smtp smtps telnet tftp 
Features: alt-svc AsynchDNS brotli GSS-API HSTS HTTP2 HTTPS-proxy IDN IPv6 Kerberos Largefile libz MultiSSL NTLM NTLM_WB SPNEGO SSL TLS-SRP UnixSockets zstd

最後のcurl --versionコマンドを実行して出力された文字列のProtocolssftpが入っていればOKです。

Git-ftpのセットアップ

git管理ディレクトリに移動して下記コマンドでFTP情報を登録します。


git config git-ftp.user "[ユーザー名]"
git config git-ftp.url "sftp://[ホスト名]:[ポート番号]/[アップロードディレクトリ]"
git config git-ftp.key "[秘密鍵のパス]"

初回アップロード。
下記コマンドのどちらかのみ実行。


# ファイルをまだアップロードしていない場合。全てのファイルをアップロードします。
git ftp init

# 既にアップ済みの場合。git-ftp.logのみアップロードします。
git ftp catchup

次回以降のデプロイ

次回以降はいつも通りgit commit後にgit ftp pushコマンドを打つと差分がアップロードされます。


git ftp push

参考