It is common among developers to deploy code using git. However, I have never done this before. I am (generally) familiar with git and source code management concepts, but using git for a deployment is something I always wanted to do. BitBucket (and I expect other repo services) allow users to add a deployment key to their repo. This key is read-only, so you can be confident when you upload it to your remote host, no malicious user can use it to modify your code.
These instructions are written for BitBucket (which is the remote repo that I use), but should apply in a general sense to all remote repos.
Generate a new public/private key pair
$ ssh-keygen -t rsa
Be sure to set a passphrase, for maximum security. After all, you will be uploading the private key to a 3rd party, so make sure a passphrase is required so anyone who accesses your key cannot access your code.
Upload public key to BitBucket as deployment key
Navigate to your repo on the BitBucket website. Under Navigation in the left-side bar, click Settings, Deployment keys. Click the Add key button, and follow the prompts.
Copy private key to host
$ scp ~/.ssh/deployment-key hostname:~/.ssh/deployment-key
Configure ssh identity on host
~/.ssh/config file on the host, using vi/vim/nano/emacs/whatever is available to configure ssh to use the private key when authenticating git requests with BitBucket.
Add the following line:
Host bitbucket.orgnIdentityFile ~/.ssh/deployment-key
Set permissions for private key on host
When I tried to clone my repo, Git on my host alerted me to a security concern that the private key had excessive privileges. Run the following command to ensure that the private key is only accessible to your user:
chmod 600 ~/.ssh/deployment-key
git clone email@example.com:account-name/repo-name.git local-directory-name
Note these instructions assume you want to deploy the master branch. If you want a different branch, then add the
-b branch-name flag to the clone command.
That’s it! Once the repo is cloned, you can simply run
git pull to grab the updates in BitBucket. Follow these instructions each time you wish to deploy code using git.