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
- Work locally in feature branches.
- Push your commits to GitHub.
- The
master
branch should always be deployable.
Deploy
- SSH into the server.
cd
to the Git repo root.git fetch
to view new commits on GitHub.git pull
to retrieve new commits from GitHub.
Post-deploy
- Set permissions as needed on server (manually or scripted).
- Trigger database update scripts.
- Clear caches and indexes.
Setup Git on the server
1. SSH into the server
ssh [email protected]
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 "[email protected]"
# 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
- Manage the SSH keys on your GitHub account.
- Add new SSH key.
- Write a good title like
{hostingCompany} {serverId} ({example.com})
. - Paste the public SSH key from the server.
- 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 [email protected]:{userName}/{projectName}.git .
The hard way
When another branch should be tracked/deployed.
cd /path/to/project
git init
git remote add origin [email protected]:{userName}/{projectName}.git
git fetch origin
git checkout --track origin/master
8. All done
Refer to the deployment workflow.