Deploy with Git

This is a semi-automated process to use Git for deployment. It’s not automatic, but you’ll be able to stop using FTP.

Workflow

Develop

  1. Work locally in feature branches.
  2. Push your commits to GitHub.
  3. The master branch should always be deployable.

Deploy

  1. SSH into the server.
  2. cd to the Git repo root.
  3. git fetch to view new commits on GitHub.
  4. git pull to retrieve new commits from GitHub.

Post-deploy

  1. Set permissions as needed on server (manually or scripted).
  2. Trigger database update scripts.
  3. Clear caches and indexes.

Setup Git on the server

1. SSH into the server

ssh username@example.com

2. Check Git config

git config --list

3. Update Git config (if needed)

git config --global user.name "{websiteName} Production Server"
git config --global user.email "your_email@example.com"

# Optional (see below)
# Note: you must `git init` your repo before setting this.
git config core.filemode false

When you manually set permissions on the server, Git wants to track those changes. Set core.filemode false on the server to ignore permissison changes. Reference

4. Get/create SSH keys on the server

Check the server for existing SSH keys.

ls -al ~/.ssh

If you don’t see id_rsa and id_rsa.pub, then generate a new SSH key.

5. Copy the public SSH key

# Copy to your computer's clipboard
pbcopy < ~/.ssh/id_rsa.pub

6. Add the public SSH key to GitHub

  1. Manage the SSH keys on your GitHub account.
  2. Add new SSH key.
  3. Write a good title like {hostingCompany} {serverId} ({example.com}).
  4. Paste the public SSH key from the server.
  5. Save the key.

7. Add the remote repository to the server

Both the approaches do the same thing:

The easy way

When master branch should be tracked/deployed.

cd /path/to/project
git clone git@github.com:{userName}/{projectName}.git .

The hard way

When another branch should be tracked/deployed.

cd /path/to/project
git init
git remote add origin git@github.com:{userName}/{projectName}.git
git fetch origin
git checkout --track origin/master

8. All done

Refer to the deployment workflow.

Reference